C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

Chapter 22 Implementing lists: linked implementations.
DATA STRUCTURES USING C++ Chapter 5
Data Structures Using C++
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Review Learn about linked lists
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 4 ADT Sorted List.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
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 © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
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.
1 Data Structures and Algorithms Pointers and Dynamic Data.
Data Structures Using Java1 Chapter 4 Linked Lists.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
Data Structures Using C++1 Chapter 5 Linked Lists.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 3)
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
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.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
  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.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
Chapter 14 Dynamic Data and Linked Lists
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Pointers and Linked Lists
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Chapter 4 Linked Lists.
Traversing a Linked List
Linked lists.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Chapter 16 Linked Structures
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
Linked lists.
Chapter 16-3 Linked Structures
Presentation transcript:

C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists

C++ Programming: From Problem Analysis to Program Design, Second Edition2 Objectives In this chapter you will: Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list Learn how to construct a doubly linked list

C++ Programming: From Problem Analysis to Program Design, Second Edition3 Introduction Data can be organized and processed sequentially using an array, called a sequential list Problems with an array −Array size is fixed −Unsorted array: searching for an item is slow −Sorted array: insertion and deletion is slow

C++ Programming: From Problem Analysis to Program Design, Second Edition4 Linked Lists Linked list: collection of components (nodes) Every node (except last) has address of next node Every node has two components Address of the first node of the list is stored in a separate location, called head or first

C++ Programming: From Problem Analysis to Program Design, Second Edition5 A Linked List a linked list is a list in which the order of the components is determined by an explicit link member in each node the nodes are struct s--each node contains a component member and also a link member that gives the location of the next node in the list head ‘X’ ‘C’ ‘L’

C++ Programming: From Problem Analysis to Program Design, Second Edition6 Dynamic Linked List head “Ted” “Irv” “Lee” in a dynamic linked list, nodes are linked together by pointers, and an external pointer (or head pointer) points to the first node in the list

C++ Programming: From Problem Analysis to Program Design, Second Edition7 Nodes can be located anywhere in memory the link member holds the memory address of the next node in the list head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

8 // Type DECLARATIONS struct NodeType { char info; NodeType* link; } // Variable DECLARATIONS NodeType* head; NodeType* ptr; 8 Declarations for a Dynamic Linked List. info. link ‘A’ 6000

9 9 Pointer Dereferencing and Member Selection. info. link ‘A’ 6000 ptr. info. link ‘A’ 6000 *ptr ptr. info. link (*ptr).info ptr->info ‘A’ 6000

10 ptr is a pointer to a node. info. link ‘A’ 6000 ptr

11 *ptr is the entire node pointed to by ptr ptr. info. link ‘A’ 6000 *ptr

12 ptr->info is a node member ptr. info. link ptr->info (*ptr).info // equivalent ‘A’ 6000

13 ptr->link is a node member ptr. info. link ptr->link (*ptr).link // equivalent ‘A’ 6000

C++ Programming: From Problem Analysis to Program Design, Second Edition14 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head

C++ Programming: From Problem Analysis to Program Design, Second Edition15 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition16 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition17 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition18 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition19 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition20 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition21 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition22 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition23 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition24 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

C++ Programming: From Problem Analysis to Program Design, Second Edition25 //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout info ; // Or, do something else with node *ptr ptr = ptr->link ; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

Using Operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated. The dynamically allocated object exists until the delete operator destroys it. 26

27 Inserting a Node at the Front of a List char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item

28 Inserting a Node at the Front of a List char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location

29 Inserting a Node at the Front of a List char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location

30 Inserting a Node at the Front of a List char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’

31 Inserting a Node at the Front of a List char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’

32 Inserting a Node at the Front of a List char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’

The object currently pointed to by the pointer is deallocated, and the pointer is considered undefined. The object’s memory is returned to the free store. Using Operator delete 33

34 Deleting the First Node from the List NodeType* tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr

35 Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

36 Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

37 Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

38 Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head item ‘X’ ‘C’ ‘L’ tempPtr ‘B’

39 What is a List? a list is a varying-length, linear collection of homogeneous elements linear means each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

40 // SPECIFICATION FILE DYNAMIC-LINKED SORTED LIST( slist2.h ) struct NodeType { char item ;// Data (char to keep example simple) NodeType* link ;// link to next node in list } ; 40 struct NodeType

41 // SPECIFICATION FILE DYNAMIC-LINKED SORTED LIST( slist2.h ) class SortedList2 { public : bool IsEmpty ( ) const ; void Print ( ) const ; void InsertTop ( /* in */ char item ) ; void Insert ( /* in */ char item ) ; void DeleteTop ( /* out */ char& item ) ; void Delete ( /* in */ char item ); SortedList2 ( ) ;// Constructor ~SortedList2 ( ) ;// Destructor SortedList2 ( const SortedList2& otherList ) ; // Copy-constructor private : NodeType* head; } ; 41

42 class SortedList2 Print ~SortedList2 Insert InsertTop SortedList2 IsEmpty Delete ‘C’ ‘L’ ‘X’ Private data: head DeleteTop

43 Insert Algorithm what will be the algorithm to Insert an item into its proper place in a sorted linked list? that is, for a linked list whose elements are maintained in ascending order?

44 Insert algorithm for SortedList2 find proper position for the new element in the sorted list using two pointers prevPtr and currPtr, where prevPtr trails behind currPtr obtain a node for insertion and place item in it insert the node by adjusting pointers

45 Implementing SortedList2 Member Function Insert // DYNAMIC LINKED LIST IMPLEMENTATION (slist2.cpp) void SortedList2 :: Insert ( /* in */ char item ) // PRE: item is assigned && List components in ascending order // POST: item is in List && List components in ascending order {. }

46 Inserting ‘S’ into a Sorted List ‘C’ ‘L’ ‘X’ Private data: head prevPtr currPtr

47 Finding Proper Position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: head prevPtr currPtr NULL

48 ‘C’ ‘L’ ‘X’ Private data: head prevPtr currPtr Finding Proper Position for ‘S’

49 ‘C’ ‘L’ ‘X’ Private data: head prevPtr currPtr Finding Proper Position for ‘S’

50 ‘C’ ‘L’ ‘X’ Private data: head prevPtr currPtr Inserting ‘S’ into Proper Position ‘S’

51 // IMPLEMENTATION DYNAMIC-LINKED SORTED LIST (slist2.cpp) SortedList2 ::SortedList2 ( ) // Constructor // Post: head == NULL { head = NULL ; } SortedList2 :: ~SortedList2 ( ) // Destructor // Post: All linked nodes deallocated { char temp ; // keep deleting top node while ( !IsEmpty ) DeleteTop ( temp ); } 51

52 void SortedList2 :: Insert( /* in */ char item ) // Pre: item is assigned && list components in ascending order // Post:new node containing item is in its proper place // && list components in ascending order { NodeType* currPtr ; NodeType* prevPtr ; NodeType* location ; location = new NodeType ; location->info = item; prevPtr = NULL ; currPtr = head ; while ( currPtr != NULL && item > currPtr->info ) {prevPtr = currPtr ; // advance both pointers currPtr = currPtr->link ; } location->link = currPtr ; // insert new node here if ( prevPtr == NULL ) head = location ; else prevPtr->link = location ; } 52

53 void SortedList2 :: DeleteTop ( /* out */ char& item ) // Pre: list is not empty && list elements in ascending order // Post: item == element of first list entry // && node containing item is no longer in linked list // && list elements in ascending order { NodeType* tempPtr = head ; // obtain item and advance head item = head->info ; head = head->link ; delete tempPtr ; } 53

54 void SortedList2 :: Delete ( /* in */ char item ) // Pre: list is not empty && list elements in ascending order // && item == component member of some list node // Post: item == element of first list entry // && node containing first occurrence of item is no longer // in linked list && list elements in ascending order { NodeType* delPtr ; NodeType* currPtr ;// Is item in first node? if ( item == head->info ) { delPtr = head ;// If so, delete first node head = head->link ; } else {// search for item in rest of list currPtr = head ; while ( currPtr->link->info != item ) currPtr = currPtr->link ; delPtr = currPtr->link ; currPtr->link = currPtr->link->link ; } delete delPtr ; } 54

55 class SortedList2 Print ~SortedList2 Insert InsertTop SortedList2 IsEmpty Delete ‘C’ ‘L’ ‘X’ Private data: head DeleteTop

C++ Programming: From Problem Analysis to Program Design, Second Edition56 Another Linked List Linked list above has four nodes Address of first node is stored in head Each node has two components: −Info: stores the info −Link: stores address of the next node

C++ Programming: From Problem Analysis to Program Design, Second Edition58 Doubly Linked Lists Doubly linked list: every node has next and back pointers Can be traversed in either direction

C++ Programming: From Problem Analysis to Program Design, Second Edition59 Struct for Doubly Linked List struct NodeType { int item ;// Data NodeType* next;// link to next node in list NodeType* back; // link to previous node in list } ;

C++ Programming: From Problem Analysis to Program Design, Second Edition60 Delete a Node Consider the list shown in the figure below

C++ Programming: From Problem Analysis to Program Design, Second Edition62 Programming Example A new video store in your neighborhood is about to open However, it does not have a program to keep track of its videos and customers The store managers want someone to write a program for their system so that the video store can operate

C++ Programming: From Problem Analysis to Program Design, Second Edition63 Programming Example (continued) The program should be able to perform the following operations: −Rent a video; that is, check out a video −Return, or check in, a video −Create a list of videos owned by the store −Show the details of a particular video −Print a list of all videos in the store −Check whether a particular video is in the store −Maintain a customer database −Print list of all videos rented by each customer

C++ Programming: From Problem Analysis to Program Design, Second Edition64 Programming Example (continued) There are two major components of the video store: −A video −A customer You need to maintain two lists: 1.A list of all videos in the store 2.A list of all customers of the store

C++ Programming: From Problem Analysis to Program Design, Second Edition65 Part 1: Video Component (continued) The common things associated with a video are: −Name of the movie −Names of the stars −Name of the producer −Name of the director −Name of the production company −Number of copies in store

C++ Programming: From Problem Analysis to Program Design, Second Edition68 Video List This program requires us to −Maintain a list of all videos in the store −Add a new video to our list Use a linked list to create a list of videos

C++ Programming: From Problem Analysis to Program Design, Second Edition71 Part 2: Customer Component Primary characteristics of a customer: −First name −Last name −Account number −List of rented videos

C++ Programming: From Problem Analysis to Program Design, Second Edition73 Basic Operations Basic operations on personType: −Print the name −Set the name −Show the first name −Show the last name

C++ Programming: From Problem Analysis to Program Design, Second Edition74 Basic Operations (continued) Basic operations on an object of the type customerType are: −Print name, account #, and number of rentals −Set name, account #, and number of rentals −Rent a video, add video to the list −Return a video, delete video from the list −Show account #

C++ Programming: From Problem Analysis to Program Design, Second Edition75 Main Program The data in the input file is in the form: video title movie star1 movie star2 movie producer movie director movie production co. number of copies.

C++ Programming: From Problem Analysis to Program Design, Second Edition76 Algorithm of the Main Function Open the input file, exit if not found createVideoList: create the list of videos displayMenu: show the menu While not done, perform various tasks

C++ Programming: From Problem Analysis to Program Design, Second Edition77 CreateVideoList 1.Read data and store in a video object 2.Insert video in list 3.Repeat steps 1 and 2 for each video in file

C++ Programming: From Problem Analysis to Program Design, Second Edition78 displayMenu Select one of the following 1.Check whether a particular video is in the store 2.Check out a video 3.Check in a video 4.Check whether a particular video is in the store 5.Print the titles of all videos 6.Print a list of all videos 9.Exit

C++ Programming: From Problem Analysis to Program Design, Second Edition79 Summary A linked list is a list of items (nodes) Order of the nodes is determined by the address, called a link, stored in each node The pointer to a linked list is called head or first A linked list is a dynamic data structure The list length is the number of nodes

C++ Programming: From Problem Analysis to Program Design, Second Edition80 Summary Insertion and deletion does not require data movement; only the pointers are adjusted A (single) linked list is traversed in only one direction Search of a linked list is sequential The head pointer is fixed on first node Traverse: use a pointer other than head

C++ Programming: From Problem Analysis to Program Design, Second Edition81 Summary Doubly linked list −Every node has two links: next and previous −Can be traversed in either direction −Item insertion and deletion require the adjustment of two pointers in a node