1 Chapter 16 Linked Structures Dale/Weems/Headington.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 4 ADT Sorted List.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 17: Linked Lists.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
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.
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.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Dynamic Structures & Arrays.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Structures Using C++ 2E
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
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 © 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 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
1 Data Structures and Algorithms Pointers and Dynamic Data.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
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.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: 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.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
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.
1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
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.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
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.
Chapter 14 Dynamic Data and Linked Lists
C++ Programming:. Program Design Including
Pointers and Linked Lists
CSCI-255 LinkedList.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 13 Applied Arrays: Lists and Strings
Chapter 4 Linked Lists.
Traversing a Linked List
Linked lists.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
Chapter 13 Applied Arrays: Lists and Strings
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
Linked lists.
Chapter 16-3 Linked Structures
Presentation transcript:

1 Chapter 16 Linked Structures Dale/Weems/Headington

2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and Deletion of Elements in a Dynamic Linked List l Specification of a Dynamic Linked Sorted List l Insertion and Deletion of Elements in a Dynamic Linked Sorted List

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

4 To implement the List ADT The programmer must 1) choose a concrete data representation for the list, and 2) implement the list operations

Array-Based Sorted List Operations Transformers n Insert n Delete Observers n IsEmpty n IsFull n Length n IsPresent n Print change state observe state 5

6 Array-based class SortedList IsFull Length IsPresent Delete IsEmpty Insert Print Private data: length data [ 0 ] [ 1 ] [ 2 ] [MAX_LENGTH-1] SortedList

7 // SPECIFICATION FILE ARRAY-BASED SORTED LIST( slist.h ) const int MAX_LENGTH = 50 ; typedef int ItemType ; class SortedList { public : // public member functions SortedList ( ) ;// constructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int Length ( ) const ; // returns length of list void Insert ( ItemType item ) ; void Delete ( ItemType item ) ; bool IsPresent( ItemType item ) const ; void Print ( ) ; private :// private data members int length ; // number of values currently stored ItemTypedata[MAX_LENGTH] ; void BinSearch ( ItemType item, bool& found, int& position ) const ; } ; 7

8 How to Implement a List l use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed l use a linked list (to avoid excessive data movement from insertions and deletions) not necessarily stored in contiguous memory locations

9 Implementation Possibilities for a List ADT List Linked list Built-in array Built-in dynamic data and pointers Built-in array of structs

10 A Linked List l 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’

11 Dynamic Linked List head “Ted” “Irv” “Lee” l 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

12 Nodes can be located anywhere in memory l the link member holds the memory address of the next node in the list head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

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

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

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

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

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

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

19 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

20 //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

21 //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

22 //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

23 //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

24 //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

25 //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

26 //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

27 //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

28 //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

29 //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

30 //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. 31

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

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

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

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

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

37 Inserting a Node at the Front of a List char item = ‘B’; NodePtr 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 38

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

40 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’

41 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’

42 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’

43 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’

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

ADT SortedList2 Operations Transformers n InsertTop n Insert n DeleteTop n Delete Observers n Print n IsEmpty change state observe state 45

46 // SPECIFICATION FILE DYNAMIC-LINKED SORTED LIST( slist2.h ) typedef int ItemType ; // Type of each component // is simple type or string type struct NodeType { ItemType item ;// Pointer to person’s name NodeType* link ;// link to next node in list } ; typedef NodeType* NodePtr; 46 struct NodeType

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

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

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

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

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

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

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

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

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

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

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

58 void SortedList2 :: Insert( /* in */ ItemType 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 { NodePtr currPtr ; NodePtr prevPtr ; NodePtr location ; location = new NodeType ; newNodePtr->link = 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 ; } 58

59 void SortedList2 :: DeleteTop ( /* out */ ItemType& 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 { NodePtr tempPtr = head ; // obtain item and advance head item = head->info ; head = head->link ; delete tempPtr ; } 59

60 void SortedList2 :: Delete ( /* in */ ItemType 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 { NodePtr delPtr ; NodePtr 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 ; } 60

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