CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1
Readings 2 Reading Section 3.1 ADT (recall, lecture 1): Abstract Data Type (ADT): Mathematical description of an object with set of operations on the object. Section 3.2 The List ADT
List ADT 3 What is a List? Ordered sequence of elements A 1, A 2, …, A N Elements may be of arbitrary type, but all are of the same type Common List operations are: Insert, Find, Delete, IsEmpty, IsLast, FindPrevious, First, Kth, Last, Print, etc.
Simple Examples of List Use 4 Polynomials x x 85 Unbounded Integers Text “ This is an example of text ”
List Implementations 5 Two types of implementation: Array-Based Pointer-Based
List: Array Implementation 6 Basic Idea: Pre-allocate a big array of size MAX_SIZE Keep track of current size using a variable count Shift elements when you have to insert or delete ANAN …A4A4 A3A3 A2A2 A1A1 MAX_SIZE-1count-1…3210
List: Array Implementation 7 FEDCBA MAX_SIZE Insert Z in kth position EDCZBA MAX_SIZE F 6
Array List Insert Running Time 8 Running time for N elements? On average, must move half the elements to make room – assuming insertions at positions are equally likely Worst case is insert at position 0. Must move all N items one position before the insert This is O(N) running time. Probably too slow
Review Big Oh Notation 9 T(N) = O(f(N)) if there are positive constants c and n 0 such that: T(N) n 0 T(N) = O(N) linear
List: Pointer Implementation 10 Basic Idea: Allocate little blocks of memory (nodes) as elements are added to the list Keep track of list by linking the nodes together Change links when you want to insert or delete Value NULL L node ValueNext node Next
© 2005 Pearson Addison- Wesley. All rights reserved 4-11 Preliminaries Figure 4.1 a) A linked list of integers; b) insertion; c) deletion
© 2005 Pearson Addison- Wesley. All rights reserved 4-12 Pointer-Based Linked Lists A node in a linked list is usually a struct struct Node{ int item; Node *next; }; A node is dynamically allocated Node *p; p = new Node; Figure 4.6 A node
© 2005 Pearson Addison- Wesley. All rights reserved 4-13 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
© 2005 Pearson Addison- Wesley. All rights reserved 4-14 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 reserved 4-15 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 reserved 4-16 Displaying the Contents of a Linked List Figure 4.9 The effect of the assignment cur = cur->next
© 2005 Pearson Addison- Wesley. All rights reserved 4-17 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 reserved 4-18 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 reserved 4-19 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 reserved 4-20 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 reserved 4-21 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 reserved 4-22 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 reserved 4-23 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 reserved 4-24 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 reserved 4-25 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 reserved 4-26 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 reserved 4-27 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
Pointer Implementation Issues 28 Whenever you break a list, your code should fix the list up as soon as possible Draw pictures of the list to visualize what needs to be done Pay special attention to boundary conditions: Empty list Single item – same item is both first and last Two items – first, last, but no middle items Three or more items – first, last, and middle items
Pointer List Insert Running Time 29 Running time for N elements? Insert takes constant time (O(1)) Does not depend on input size Compare to array based list which is O(N)
© 2005 Pearson Addison-Wesley. All rights reserved 4-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 reserved 4-31 Circular Linked Lists Figure 4.26 A circular linked list with an external pointer to the last node
© 2005 Pearson Addison- Wesley. All rights reserved 4-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 reserved 4-33 Doubly Linked Lists Figure 4.28 A doubly linked list
© 2005 Pearson Addison- Wesley. All rights reserved 4-34 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 reserved 4-35 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 reserved 4-36 Summary Each pointer in a linked list is a pointer to the next node in the list 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
© 2005 Pearson Addison- Wesley. All rights reserved 4-37 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
Assignment 1 Add to the LList class implementation a member function to reverse the order of the elements on the list. Your algorithm should run in Θ (n) time for a list of n elements. 38