Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Data Structures: A Pseudocode Approach with C
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Summary of lectures (1 to 11)
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
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.
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 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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Chapter 18: Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Recursive Linked List Operations
Chapter 17: Linked Lists.
Lecture 14 Linked Lists CSE /26/2018.
Presentation transcript:

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 17-2 NULL list head

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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. 17-3

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 17-4 NULL list head Add or delete node

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 of a node in the middle of the list is very efficient 17-5 NULL list head

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 17-6 data pointer

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Empty List A list with no nodes is called the empty list In this case the list head is set to NULL 17-7 NULL list head

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C++ Implementation Implementation of nodes requires a structure containing a pointer to a structure of the same type: struct ListNode { int data; ListNode *next; }; 17-8

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C++ Implementation Nodes can be equipped with constructors: struct ListNode { int data; ListNode *next; ListNode(int d, ListNode* p=NULL) {data = d; next = p;} }; 17-9

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17.2 Linked List Operations Basic operations: –add 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 17-11

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Creating a Node ListNode *p; int num = 23; p = new ListNode(num); NULL 23 p

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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); 17-13

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Appending an Item list head p NULL List originally has 5, 13. p locates the last node, then a node with a new item, 23, is added

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inserting a Node Used to insert an item into a sorted list, keeping the list sorted. Two possibilities: –Insertion is at the head of the list (because item at head is already greater than item being inserted, or because list is empty –Insertion is after an existing node in a non- empty list 17-15

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inserting a Node at Head of a List Test to see if –head pointer is null, or –node value pointed at by head is greater than value to be inserted Must test in this order: unpredictable results if second test is attempted on an empty list Create new node, set its next pointer to head, then point head to it 17-16

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inserting a Node in Body of a List 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 17-17

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inserting a Node into a Linked List NULL list head nodePtrpreviousNode Correct position located Item to insert num

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inserting a Node into a Linked List NULL list head nodePtrpreviousNode New node created and inserted in order in the linked list

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 17-20

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Traversing a Linked List NULL list head 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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Removing an Element 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 17-22

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Deleting a Node NULL list head nodePtrpreviousNode Locating the node containing 13 Contents of node to be deleted: 13

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Deleting a Node NULL list head nodePtrpreviousNode Adjusting pointer around the node to be deleted

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Deleting a Node NULL list head 519 nodePtrpreviousNode Linked list after deleting the node containing 13

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 Finally, set the list head to NULL 17-26

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 17-27

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 17-28

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursive Linked List Operations To find the length (number of elements) 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 17-29

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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); } 17-30

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Other Recursive Linked List Operations General design considerations: Base case is often when the list is empty Recursive case often involves the use of the tail of the list (i.e., the list without the head). Since the tail has one fewer entry than the list that was passed in to this call, the recursion eventually stops

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17.5 Variations of the Linked List 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 NULL

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variations of the Linked List circular linked list: the last node in the list points back to the first node in the list, not to NULL list head 51319

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 17-34