Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures: A Pseudocode Approach with C
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Linking Objects Together References are particularly important in computer science because they make it possible to represent the relationship among objects.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 Object-Oriented Design Inheritance and Class Examples.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
More Linking Up with Linked Lists Chapter 11 5/19/2015 Adopted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++,
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
10/07/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #05:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Chapter 16-2 Linked Structures
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

Chapter 4 Linked Lists

© 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size Data must be shifted during insertions and deletions –Linked list is able to grow in size as needed Does not require the shifting of items during insertions and deletions

© 2005 Pearson Addison-Wesley. All rights reserved4-3 Preliminaries Figure 4.1 a) A linked list of integers; b) insertion; c) deletion

© 2005 Pearson Addison-Wesley. All rights reserved4-4 Pointers A pointer contains the location, or address in memory, of a memory cell –Initially undefined, but not NULL –A statically allocated pointer declaration int *p; –A dynamically allocated pointer variable p = new int;

© 2005 Pearson Addison-Wesley. All rights reserved4-5 Pointers The expression, *p, denotes the memory cell to which p points The & address-of operator places the address of a variable into a pointer variable –p = &x; Figure 4.2 A pointer to an integer

© 2005 Pearson Addison-Wesley. All rights reserved4-6 Pointers The delete operator returns dynamically allocated memory to the system for reuse, and leaves the variable undefined –delete p ; –A pointer to a deallocated memory cell is possible and dangerous Assign the pointer q the value in p q = p;

© 2005 Pearson Addison-Wesley. All rights reserved4-7 Pointers Figure 4.3 (a) declaring pointer variables; (b) pointing to statically allocating memory; (c) assigning a value; (d) allocating memory dynamically; (e) assigning a value

© 2005 Pearson Addison-Wesley. All rights reserved4-8 Pointers Figure 4.3 (f) copying a pointer; (g) allocating memory dynamically and assigning a value; (h) assigning NULL to a pointer variable; (i) deallocating memory

© 2005 Pearson Addison-Wesley. All rights reserved4-9 Dynamic Allocation of Arrays Use the new operator to allocate an array dynamically An array name is a pointer to the array’s first element The size of a dynamically allocated array can be increased double* oldArray = anArray; anArray = new double[2*arraySize];

© 2005 Pearson Addison-Wesley. All rights reserved4-10 Pointer-Based Linked Lists A node in a linked list is usually a struct struct Node { int item Node *next; }; //end struct A node is dynamically allocated Node *p; p = new Node; Figure 4.6 A node

© 2005 Pearson Addison-Wesley. All rights reserved4-11 Pointer-Based Linked Lists The head pointer points to the first node in a linked list If head is NULL, the linked list is empty Executing the statement the statement head=new Node before head=NULL will result in a lost cell

© 2005 Pearson Addison-Wesley. All rights reserved4-12 Pointer-Based Linked Lists Figure 4.7 A head pointer to a list Figure 4.8 A lost cell

© 2005 Pearson Addison-Wesley. All rights reserved4-13 Displaying the Contents of a Linked List Reference a node member with the -> operator p->item; A traverse operation visits each node in the linked list –A pointer variable cur keeps track of the current node for (Node *cur = head; cur != NULL; cur = cur->next) cout item << endl;

© 2005 Pearson Addison-Wesley. All rights reserved4-14 Displaying the Contents of a Linked List Figure 4.9 The effect of the assignment cur = cur->next

© 2005 Pearson Addison-Wesley. All rights reserved4-15 Deleting a Specified Node from a Linked List Deleting an interior node prev->next=cur->next; Deleting the first node head=head->next; Return deleted node to system cur->next = NULL; delete cur; cur=NULL;

© 2005 Pearson Addison-Wesley. All rights reserved4-16 Deleting a Specified Node from a Linked List Figure 4.10 Deleting a node from a linked list Figure 4.11 Deleting the first node

© 2005 Pearson Addison-Wesley. All rights reserved4-17 Inserting a Node into a Specified Position of a Linked List To insert a node between two nodes newPtr->next = cur; prev->next = newPtr; Figure 4.12 Inserting a new node into a linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-18 Inserting a Node into a Specified Position of a Linked List To insert a node at the beginning of a linked list newPtr->next = head; head = newPtr; Figure 4.13 Inserting at the beginning of a linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-19 Inserting a Node into a Specified Position of a Linked List Inserting at the end of a linked list is not a special case if cur is NULL newPtr->next = cur; prev->next = newPtr; Figure 4.14 Inserting at the end of a linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-20 Inserting a Node into a Specified Position of a Linked List Determining the point of insertion or deletion for a sorted linked list of objects for(prev = NULL, cur= head; (cur != null)&& (newValue > cur->item); prev = cur, cur = cur->next;

© 2005 Pearson Addison-Wesley. All rights reserved4-21 A Pointer-Based Implementation of the ADT List Public methods –isEmpty –getLength –insert –remove –retrieve Private method –find Private Data Members –head –Size Local variables to member functions –cur –prev

© 2005 Pearson Addison-Wesley. All rights reserved4-22 Constructors and Destructors Default constructor initializes size and head Copy constructor allows a deep copy –Copies the array of list items and the number of items A destructor is required for dynamically allocated memory

© 2005 Pearson Addison-Wesley. All rights reserved4-23 Comparing Array-Based and Pointer-Based Implementations Size –Increasing the size of a resizable array can waste storage and time Storage requirements –Array-based implementations require less memory than a pointer-based ones

© 2005 Pearson Addison-Wesley. All rights reserved4-24 Comparing Array-Based and Pointer-Based Implementations Access time –Array-based: constant access time –Pointer-based: the time to access the i th node depends on i Insertion and deletions –Array-based: require shifting of data –Pointer-based: require a list traversal

© 2005 Pearson Addison-Wesley. All rights reserved4-25 Saving and Restoring a Linked List by Using a File Use an external file to preserve the list between runs Do not write pointers to a file, only data Recreate the list from the file by placing each item at the end of the list –Use a tail pointer to facilitate adding nodes to the end of the list –Treat the first insertion as a special case by setting the tail to head

© 2005 Pearson Addison-Wesley. All rights reserved4-26 Passing a Linked List to a Function A function with access to a linked list’s head pointer has access to the entire list Pass the head pointer to a function as a reference argument Figure 4.22 A head pointer as a value argument

© 2005 Pearson Addison-Wesley. All rights reserved4-27 Processing Linked Lists Recursively Recursive strategy to display a list –Write the first node of the list –Write the list minus its first node Recursive strategies to display a list backward –writeListBackward strategy Write the last node of the list Write the list minus its last node backward

© 2005 Pearson Addison-Wesley. All rights reserved4-28 Processing Linked Lists Recursively –writeListBackward2 strategy Write the list minus its first node backward Write the first node of the list Recursive view of a sorted linked list –The linked list to which head points is a sorted list if head is NULL or head->next is NULL or head->item next->item, and head->next points to a sorted linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-29 Objects as Linked List Data Data in a linked list node can be an instance of a class typedef ClassName ItemType; struct Node { ItemType item; Node *next; }; //end struct Node *head;

© 2005 Pearson Addison-Wesley. All rights reserved4-30 Circular Linked Lists Last node references the first node Every node has a successor No node in a circular linked list contains NULL Figure 4.25 A circular linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-31 Dummy Head Nodes Dummy head node –Always present, even when the linked list is empty –Insertion and deletion algorithms initialize prev to reference the dummy head node, rather than NULL Figure 4.27 A dummy head node

© 2005 Pearson Addison-Wesley. All rights reserved4-32 Doubly Linked Lists Each node points to both its predecessor and its successor Circular doubly linked list –precede pointer of the dummy head node points to the last node –next reference of the last node points to the dummy head node –No special cases for insertions and deletions

© 2005 Pearson Addison-Wesley. All rights reserved4-33 Doubly Linked Lists Figure 4.29 (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

© 2005 Pearson Addison-Wesley. All rights reserved4-34 Doubly Linked Lists To delete the node to which cur points (cur->precede)->next = cur->next; (cur->next)->precede = cur->precede; To insert a new node pointed to by newPtr before the node pointed to by cur newPtr->next = cur; newPtr->precede = cur->precede; cur->precede = newPtr; newPtr->precede->next = newPtr;

© 2005 Pearson Addison-Wesley. All rights reserved4-35 Application: Maintaining an Inventory Operations on the inventory –List the inventory in alphabetical order by title (L command) –Find the inventory item associated with title (I, M, D, O, and S commands) –Replace the inventory item associated with a title (M, D, R, and S commands) –Insert new inventory items (A and D commands)

© 2005 Pearson Addison-Wesley. All rights reserved4-36 The C++ Standard Template Library The STL contains class templates for some common ADTs, including the list class The STL provides support for predefined ADTs through three basic items –Containers are objects that hold other objects –Algorithms act on containers –Iterators provide a way to cycle through the contents of a container

© 2005 Pearson Addison-Wesley. All rights reserved4-37 Summary The C++ new and delete operators enable memory to be dynamically allocated and recycled Each pointer in a linked list is a pointer to the next node in the list Array-based lists use an implicit ordering scheme; pointer-based lists use an explicit ordering scheme

© 2005 Pearson Addison-Wesley. All rights reserved4-38 Summary Algorithms for insertions and deletions in a linked list involve traversing the list and performing pointer changes –Inserting a node at the beginning of a list and deleting the first node of a list are special cases A class that allocates memory dynamically needs an explicit copy constructor and destructor

© 2005 Pearson Addison-Wesley. All rights reserved4-39 Summary Recursion can be used to perform operations on a linked list In a circular linked list, the last node points to the first node Dummy head nodes eliminate the special cases for insertion into and deletion from the beginning of a linked list