Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 Lists

2 Chapter 17 Starting Out with C++: Early Objects 5/e slide 2 © 2006 Pearson Education. All Rights Reserved Topics 17.1 Introduction to the Linked List ADT 17.2 Linked List Operations 17.3 A Linked List Template 17.4 Recursive Linked List Operations 17.5 Variations of the Linked List 17.6 The STL list Container

3 Chapter 17 Starting Out with C++: Early Objects 5/e slide 3 © 2006 Pearson Education. All Rights Reserved 17.1 Introduction to the Linked List ADT Linked list: a sequence of data structures (nodes) with each node containing a pointer to its successor The last node in the list has its successor pointer set to NULL NULL list head

4 Chapter 17 Starting Out with C++: Early Objects 5/e slide 4 © 2006 Pearson Education. All Rights Reserved Linked List Terminology The node at the beginning is called the head of the list The entire list is identified by the pointer to the head node, this pointer is called the list head.

5 Chapter 17 Starting Out with C++: Early Objects 5/e slide 5 © 2006 Pearson Education. All Rights Reserved Linked Lists Nodes can be added or removed from the linked list during execution Addition or removal of nodes can take place at beginning, end, or middle of the list NULL list head Add or delete node

6 Chapter 17 Starting Out with C++: Early Objects 5/e slide 6 © 2006 Pearson Education. All Rights Reserved Linked Lists vs. Arrays and Vectors Linked lists can grow and shrink as needed, unlike arrays, which have a fixed size Unlike vectors, insertion or removal in the middle of the list is very efficient NULL list head

7 Chapter 17 Starting Out with C++: Early Objects 5/e slide 7 © 2006 Pearson Education. All Rights Reserved Node Organization A node contains: –data: one or more data fields – may be organized as structure, object, etc. –a pointer that can point to another node data pointer

8 Chapter 17 Starting Out with C++: Early Objects 5/e slide 8 © 2006 Pearson Education. All Rights Reserved Linked List Organization Linked list contains 0 or more nodes: Has a list head to point to first node Successor pointer for the last node is set to NULL NULL list head

9 Chapter 17 Starting Out with C++: Early Objects 5/e slide 9 © 2006 Pearson Education. All Rights Reserved Empty List A list with no nodes is called the empty list In this case the list head is set to NULL NULL list head

10 Chapter 17 Starting Out with C++: Early Objects 5/e slide 10 © 2006 Pearson Education. All Rights Reserved C++ Implementation Implementation of nodes requires a structure containing a pointer to a structure of the same type: struct ListNode { int data; ListNode *next; };

11 Chapter 17 Starting Out with C++: Early Objects 5/e slide 11 © 2006 Pearson Education. All Rights Reserved C++ Implementation Nodes can be equipped with constructors: struct ListNode { int data; ListNode *next; ListNode(int d, ListNode* p=0) {data = d; next = p;} };

12 Chapter 17 Starting Out with C++: Early Objects 5/e slide 12 © 2006 Pearson Education. All Rights Reserved Creating an Empty List Define a pointer for the head of the list: ListNode *head = NULL; Head pointer initialized to NULL to indicate an empty list NULL head

13 Chapter 17 Starting Out with C++: Early Objects 5/e slide 13 © 2006 Pearson Education. All Rights Reserved 17.2 Linked List Operations Basic operations: –append a node to the end of the list –insert a node within the list –traverse the linked list –delete a node –delete/destroy the list

14 Chapter 17 Starting Out with C++: Early Objects 5/e slide 14 © 2006 Pearson Education. All Rights Reserved Creating a Node ListNode *p; int num = 23; p = new ListNode(num); NULL 23 p

15 Chapter 17 Starting Out with C++: Early Objects 5/e slide 15 © 2006 Pearson Education. All Rights Reserved Appending an Item To add an item to the end of the list: –If the list is empty, set head to a new node containing the item head = new ListNode(num); –If the list is not empty, move a pointer p to the last node, then add a new node containing the item p->next = new ListNode(num);

16 Chapter 17 Starting Out with C++: Early Objects 5/e slide 16 © 2006 Pearson Education. All Rights Reserved Appending an Item list head 51323 p p locates last node, new item, 23, is added NULL List originally has 5, 13

17 Chapter 17 Starting Out with C++: Early Objects 5/e slide 17 © 2006 Pearson Education. All Rights Reserved Inserting a Node Used to insert an item into a sorted list, keeping the list sorted. Requires two pointers to traverse the list: –pointer to locate the node with data value greater than that of node to be inserted –pointer to 'trail behind' one node, to point to node before point of insertion New node is inserted between the nodes pointed at by these pointers

18 Chapter 17 Starting Out with C++: Early Objects 5/e slide 18 © 2006 Pearson Education. All Rights Reserved Inserting a Node into a Linked List NULL list head 51319 17 nodePtrpreviousNode Correct position located Item to inserted

19 Chapter 17 Starting Out with C++: Early Objects 5/e slide 19 © 2006 Pearson Education. All Rights Reserved Inserting a Node into a Linked List NULL list head 51319 17 nodePtrpreviousNode New node created and inserted in order in the linked list

20 Chapter 17 Starting Out with C++: Early Objects 5/e slide 20 © 2006 Pearson Education. All Rights Reserved Traversing a Linked List List traversals visit each node in a linked list to display contents, validate data, etc. Basic process of traversal: set a pointer to the head pointer while pointer is not NULL process data set pointer to the successor of the current node end while

21 Chapter 17 Starting Out with C++: Early Objects 5/e slide 21 © 2006 Pearson Education. All Rights Reserved Traversing a Linked List NULL list head 51319 nodePtr nodePtr points to the node containing 5, then the node containing 13, then the node containing 19, then points to NULL, and the list traversal stops

22 Chapter 17 Starting Out with C++: Early Objects 5/e slide 22 © 2006 Pearson Education. All Rights Reserved Deleting a Node Used to remove a node from a linked list Requires two pointers: one to locate the node to be deleted, one to point to the node before the node to be deleted

23 Chapter 17 Starting Out with C++: Early Objects 5/e slide 23 © 2006 Pearson Education. All Rights Reserved Deleting a Node NULL list head 51319 nodePtrpreviousNode Locating the node containing 13

24 Chapter 17 Starting Out with C++: Early Objects 5/e slide 24 © 2006 Pearson Education. All Rights Reserved Deleting a Node NULL list head 51319 nodePtrpreviousNode Adjusting pointer around the node to be deleted

25 Chapter 17 Starting Out with C++: Early Objects 5/e slide 25 © 2006 Pearson Education. All Rights Reserved Deleting a Node NULL list head 519 nodePtrpreviousNode Linked list after deleting the node containing 13

26 Chapter 17 Starting Out with C++: Early Objects 5/e slide 26 © 2006 Pearson Education. All Rights Reserved Destroying a Linked List Must remove all nodes used in the list To do this, use list traversal to visit each node For each node, –Unlink the node from the list –Free the node’s memory Set the list head to NULL

27 Chapter 17 Starting Out with C++: Early Objects 5/e slide 27 © 2006 Pearson Education. All Rights Reserved 17.3 A Linked List Template A linked list template can be written by replacing the type of the data in the node with a type parameter, say T.

28 Chapter 17 Starting Out with C++: Early Objects 5/e slide 28 © 2006 Pearson Education. All Rights Reserved 17.4 Recursive Linked List Operations A non-empty linked list consists of a head node followed by the rest of the nodes The rest of the nodes form a linked list that is called the tail of the original list

29 Chapter 17 Starting Out with C++: Early Objects 5/e slide 29 © 2006 Pearson Education. All Rights Reserved Recursive Linked List Operations Many linked list operations can be broken down into the smaller problems of processing the head of the list and then recursively operating on the tail of the list

30 Chapter 17 Starting Out with C++: Early Objects 5/e slide 30 © 2006 Pearson Education. All Rights Reserved Recursive Linked List Operations To find the length of a list –If the list is empty, the length is 0 (base case) –If the list is not empty, find the length of the tail and then add 1 to obtain length of original list

31 Chapter 17 Starting Out with C++: Early Objects 5/e slide 31 © 2006 Pearson Education. All Rights Reserved Recursive Linked List Operations To find the length of a list int length(ListNode *myList) { if (myList == NULL) return 0; else return 1 + length(myList->next); }

32 Chapter 17 Starting Out with C++: Early Objects 5/e slide 32 © 2006 Pearson Education. All Rights Reserved 17.5 Variations of the Linked List Other linked list organizations: –doubly-linked list: each node contains two pointers: one to the next node in the list, one to the previous node in the list NULL list head 51319 NULL

33 Chapter 17 Starting Out with C++: Early Objects 5/e slide 33 © 2006 Pearson Education. All Rights Reserved Variations of the Linked List Other linked list organizations: –circular linked list: the last node in the list points back to the first node in the list, not to NULL list head 51319

34 Chapter 17 Starting Out with C++: Early Objects 5/e slide 34 © 2006 Pearson Education. All Rights Reserved 17.6 The STL list Container Template for a doubly linked list Member functions for –locating beginning, end of list: front, back, end –adding elements to the list: insert, merge, push_back, push_front –removing elements from the list: erase, pop_back, pop_front, unique

35 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 Lists


Download ppt "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."

Similar presentations


Ads by Google