Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 17- 1.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures: A Pseudocode Approach with C
Linked Lists
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
C++ is Fun – Part 14 at Turbine/Warner Bros.! Russell Hanson.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
Data Structures Using C++ 2E
Reference: Vinu V Das, Principles of Data Structures using C and C++
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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 &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other 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 © 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.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
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
18 Chapter Stacks and Queues
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
List ADT & Linked Lists.
Chapter 4 Linked Lists.
Linked lists.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
CSCI 3333 Data Structures Linked Lists.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Chapter 18: Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Doubly Linked List Implementation
Recursive Linked List Operations
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
Doubly Linked List Implementation
Linked lists.
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 17- 1

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Linked Lists 17

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to the Linked List ADT 17.1

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures NULL list head

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Introduction to the Linked List ADT References may be addresses or array indices Data structures can be added to or removed from the linked list during execution NULL list head newNode

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Linked Lists vs. Arrays and Vectors Linked lists can grow and shrink as needed, unlike arrays, which have a fixed size Linked lists can insert a node between other nodes easily NULL list head

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Linked List Organization Linked list contains 0 or more nodes: Has a list head to point to first node Last node points to NULL NULL list head

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Empty List If a list currently contains 0 nodes, it is the empty list In this case the list head points to NULL NULL list head

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Declaring a Node Declare a node: struct ListNode { int data; ListNode *next; }; No memory is allocated at this time

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Defining a Linked 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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide NULL Pointer Is used to indicate end-of-list Should always be tested for before using a pointer: ListNode *p; while (p != NULL)... Can also test the pointer itself: while (!p)... // same meaning // as above

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Linked List Operations 17.2

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Contents of NumberList.h 1 // Specification file for the NumberList class 2 #ifndef NUMBERLIST_H 3 #define NUMBERLIST_H 4 5 class NumberList 6 { 7 private: 8 // Declare a structure for the list 9 struct ListNode 10 { 11 double value; // The value in this node 12 ListNode *next; // To point to the next node 13 }; ListNode *head; // List head pointer 16

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide public: 18 // Constructor 19 NumberList() 20 { head = NULL; } // Destructor 23 ~NumberList(); // Linked list operations 26 void appendNode(double); 27 void insertNode(double); 28 void deleteNode(double); 29 void displayList() const; 30 }; 31 #endif Contents of NumberList.h (Continued)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Create a New Node Allocate memory for the new node: newNode = new ListNode; Initialize the contents of the node: newNode->value = num; Set the pointer field to NULL : newNode->next = NULL; newNode 23 NULL newNode 23

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Appending a Node Add a node to the end of the list Basic process: Create the new node (as already described) Add node to the end of the list: If list is empty, set head pointer to this node Else, traverse the list to the end set pointer of last node to point to new node

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Appending a Node NULL list head newNode 23 NULL nodePtr New node created, end of list located

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Appending a Node list head newNode 23 NULL nodePtr New node added to end of list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide void NumberList::appendNode(double num) 12 { 13 ListNode *newNode; // To point to a new node 14 ListNode *nodePtr; // To move through the list // Allocate a new node and store num there. 17 newNode = new ListNode; 18 newNode->value = num; 19 newNode->next = NULL; // If there are no nodes in the list 22 // make newNode the first node. 23 if (!head) C++ code for Appending a Node

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide head = newNode; 25 else // Otherwise, insert newNode at end. 26 { 27 // Initialize nodePtr to head of list. 28 nodePtr = head; // Find the last node in the list. 31 while (nodePtr->next) 32 nodePtr = nodePtr->next; // Insert newNode as the last node. 35 nodePtr->next = newNode; 36 } 37 } C++ code for Appending a Node (Continued)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Inserting a Node into a Linked List Used to maintain a linked list in order 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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Inserting a Node into a Linked List NULL list head newNode 17 NULL nodePtrpreviousNode New node created, correct position located

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Traversing a Linked List Visit each node in a linked list: display contents, validate data, etc. Basic process: set a pointer to the contents of the head pointer while pointer is not NULL process data go to the next node by setting the pointer to the pointer field of the current node in the list end while

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Deleting a Node Used to remove a node from a linked list If list uses dynamic memory, then delete node from memory Requires two pointers: one to locate the node to be deleted, one to point to the node before the node to be deleted

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Deleting a Node NULL list head nodePtrpreviousNode Locating the node containing 13

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

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 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 If the list uses dynamic memory, then free the node’s memory Set the list head to NULL

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A Linked List Template 17.3

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide A Linked List Template When declaring a linked list, must specify the type of data to be held in each node Using templates, can declare a linked list that can hold data type determined at list definition time See LinkedList.h (versions 1 and 2) and Program 17-5

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variations of the Linked List 17.4

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

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The STL list Container 17.5

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 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 See Table 17-1 for a list of member functions