Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.

Similar presentations


Presentation on theme: "1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex."— Presentation transcript:

1 1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

2 2 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 stack items can take place only at the top of the stack. l A stack is a LIFO “last in, first out” structure.

3 Stack ADT Operations l MakeEmpty -- Sets stack to an empty state. l IsEmpty -- Determines whether the stack is currently empty. l IsFull -- Determines whether the stack is currently full. l Push (ItemType newItem) -- Adds newItem to the top of the stack. l Pop (ItemType& item) -- Removes the item at the top of the stack and returns it in item. 3

4 ADT Stack Operations Transformers n MakeEmpty n Push n Pop Observers n IsEmpty n IsFull change state observe state 4

5 5 Another Stack Implementation l One advantage of an ADT is that the kind of implementation used can be changed. l The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter. l Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.

6 6 Tracing Client Code letter ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’);

7 7 Tracing Client Code letter ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr NULL

8 8 Tracing Client Code letter charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘V’

9 9 Tracing Client Code letter charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘C’ ‘V’ ‘V’

10 10 Tracing Client Code letter charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘S’ ‘C’ ‘V’ ‘V’

11 11 Tracing Client Code letter charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘S’ ‘C’ ‘V’ ‘V’

12 12 Tracing Client Code letter charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘C’ ‘V’ ‘S’

13 13 Tracing Client Code letter charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘K’ ‘C’ ‘V’ ‘S’

14 14 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK #include "bool.h" #include "ItemType.h" // for ItemType template struct NodeType { ItemType info; NodeType * next; } 14 Dynamically Linked Implementation of Stack. info. next ‘A’ 6000

15 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK continued template class StackType { public: StackType( ); // constructor // Default constructor. // POST: Stack is created and empty. void MakeEmpty( ); // PRE: None. // POST: Stack is empty. bool IsEmpty( ) const; // PRE: Stack has been initialized. // POST: Function value = (stack is empty) bool IsFull( ) const; // PRE: Stack has been initialized. // POST: Function value = (stack is full) 15

16 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK continued void Push( ItemType item ); // PRE: Stack has been initialized. // Stack is not full. // POST: newItem is at the top of the stack. void Pop( ItemType& item ); // PRE: Stack has been initialized. // Stack is not empty. // POST: Top element has been removed from stack. // item is a copy of removed element. ~StackType( ); // destructor // PRE: Stack has been initialized. // POST: Memory allocated for nodes has been // deallocated. private: NodeType * topPtr ; }; 16

17 17 class StackType StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType

18 18 Implementing a Stack as a Linked Structure

19 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK continued // member function definitions for class StackType template StackType ::StackType( ) // constructor { topPtr = NULL; } template void StackType ::IsEmpty( ) const // Returns true if there are no elements // on the stack; false otherwise { return ( topPtr == NULL ); } 19

20 20 Push( ) l Allocate space for new item l Put new item into the allocated space l Put the allocated space into the stack

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

22 22 Adding newItem to the stack newItem = ‘B’; NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem

23 23 Adding newItem to the stack newItem = ‘B’; NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location

24 24 Adding newItem to the stack newItem = ‘B’; NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location

25 25 Adding newItem to the stack newItem = ‘B’; NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’

26 26 Adding newItem to the stack newItem = ‘B’; NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’

27 27 Adding newItem to the stack newItem = ‘B’; NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’

28 28 template void StackType ::Push ( ItemType newItem ) // Adds newItem to the top of the stack. { NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; } 28 Implementing Push

29 29 Pop( ) l Set item to Info (top node) l Unlink the top node from the stack l Deallocate the old top node

30 The object currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store. Using operator delete 30

31 31 Deleting item from the stack NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr

32 32 Deleting item from the stack NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

33 33 Deleting item from the stack NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

34 34 Deleting item from the stack NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

35 35 Deleting item from the stack NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘X’ ‘C’ ‘L’ tempPtr ‘B’

36 36 template void StackType ::Pop ( ItemType& item ) // Removes element at the top of the stack and // returns it in item. { NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } 36 Implementing Pop

37 template bool StackType ::IsFull( ) const // Returns true if there is no room for // another NodeType node on the free store; // false otherwise. { location = new NodeType ; if ( location == NULL ) return true; else { delete location; return false; } 37

38 38 Why is a destructor needed? When a local stack variable goes out of scope, the memory space for data member topPtr is deallocated. But the nodes that topPtr points to are not automatically deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.

39 template void StackType ::MakeEmpty( ) // Post: Stack is empty; all elements deallocated. { NodeType * tempPtr;; while ( topPtr != NULL ) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } template StackType ::~StackType( ) // destructor { MakeEmpty( ); } 39

40 40 What is a Queue? l Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). l A queue is a FIFO “first in, first out” structure.

41 Queue ADT Operations l MakeEmpty -- Sets queue to an empty state. l IsEmpty -- Determines whether the queue is currently empty. l IsFull -- Determines whether the queue is currently full. l Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. l Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 41

42 ADT Queue Operations Transformers n MakeEmpty n Enqueue n Dequeue Observers n IsEmpty n IsFull change state observe state 42

43 43 class QueType QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’‘Z’ ‘T’

44 // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); // CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR bool IsEmpty( ) const; bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); void MakeEmpty( ); private: NodeType * qFront; NodeType * qRear; }; 44

45 // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued // member function definitions for class QueType template QueType ::QueType( ) // CONSTRUCTOR { qFront = NULL; qRear = NULL; } template bool QueType ::IsEmpty( ) const { return ( qFront == NULL ) } 45

46 template void QueType ::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Pre: Queue has been initialized. // Queue is not full. // Post: newItem is at rear of queue. { NodeType * ptr; ptr = new NodeType ; ptr->info = newItem; ptr->next = NULL; if ( qRear == NULL ) qFront = ptr; else qRear->next = ptr; qRear = ptr; } 46

47 template void QueType ::Dequeue( ItemType& item ) // Removes element from from front of queue // and returns it in item. // Pre: Queue has been initialized. // Queue is not empty. // Post: Front element has been removed from queue. // item is a copy of removed element. { NodeType * tempPtr; tempPtr = qFront; item = qFront->info; qFront = qFornt->next; if ( qFront == NULL ) qRear = NULL; delete tempPtr; } 47

48 48 What is a List? l A list is a homogeneous collection of elements, with a linear relationship between elements. l That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.

49 49 Implementing the Unsorted List as a Linked Structure

50 ADT Unsorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators n ResetList n GetNextItem change state observe state process all 50

51 #include “ItemType.h” // unsorted.h... template class UnsortedType { public : // LINKED LIST IMPLEMENTATION UnsortedType ( ) ; ~UnsortedType ( ) ; void MakeEmpty ( ) ; bool IsFull ( ) const ; int LengthIs ( ) const ; void RetrieveItem ( ItemType& item, bool& found ) ; void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item ) ; void ResetList ( ); void GetNextItem ( ItemType& item ) ; private : NodeType * listData; int length; NodeType * currentPos; } ; 51

52 52 class UnsortedType MakeEmpty ~UnsortedType DeleteItem. InsertItem UnsortedType RetrieveItem GetNextItem ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos

53 // LINKED LIST IMPLEMENTATION ( unsorted.cpp ) #include “itemtype.h” template UnsortedType ::UnsortedType ( ) // constructor // Pre: None. // Post:List is empty. { length = 0 ; listData = NULL; } template int UnsortedType ::LengthIs ( ) const // Post: Function value = number of items in the list. { return length; } 53

54 template void UnsortedType ::RetrieveItem( ItemType& item, bool& found ) // Pre: Key member of item is initialized. // Post:If found, item’s key matches an element’s key in the list and a copy // of that element has been stored in item; otherwise, item is unchanged. { bool moreToSearch ; NodeType * location ; location = listData ; found = false ; moreToSearch = ( location != NULL ) ; while ( moreToSearch && !found ) { if ( item == location->info ) // match here { found = true ; item = location->info ; } else // advance pointer { location = location->next ; moreToSearch = ( location != NULL ) ; } 54

55 template void UnsortedType ::InsertItem ( ItemType item ) // Pre: list is not full and item is not in list. // Post: item is in the list; length has been incremented. { NodeType * location ; // obtain and fill a node location = new NodeType ; location->info = item ; location->next = listData ; listData = location ; length++ ; } 55

56 56 Inserting ‘B’ into an Unsorted List ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos

57 57 location = new NodeType ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

58 58 location->info = item ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

59 59 location->next = listData ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

60 60 listData = location ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos item location ‘B’

61 61 length++ ; ‘X’ ‘C’ ‘L’ Private data: length 4 listData currentPos item location ‘B’

62 62 Implementing the Sorted List as a Linked Structure

63 63 class SortedType MakeEmpty ~SortedType DeleteItem. InsertItem SortedType RetrieveItem GetNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos

64 64 InsertItem algorithm for Sorted Linked List l Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location. l Obtain a node for insertion and place item in it. l Insert the node by adjusting pointers. l Increment length.

65 65 Implementing SortedType member function InsertItem // LINKED LIST IMPLEMENTATION (sorted.cpp) #include “ItemType.h” template void SortedType :: InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. item is not in list. // List is sorted by key member. // Post: item is in the list. List is still sorted. {. }

66 66 InsertItem() Set location to listData Set predLoc to NULL Set moreToSearch to (location != NULL) WHILE moreToSearch SWITCH (item.ComparedTo(location->info)) case GREATER : Set predLoc to location Set location to location->next Set moreToSearch to (location != NULL) case LESS: Set moreToSearch to false Set newNode to the address of a newly allocated node Set newNode->info to item Set newNode->next to location Set predLoc->next to newNode Increament length

67 67 Inserting ‘S’ into a Sorted List ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location moreToSearch

68 68 Finding proper position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location NULL moreToSearch true

69 69 ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location Finding proper position for ‘S’ moreToSearch true

70 70 ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos predLoc location Finding proper position for ‘S’ moreToSearch false

71 71 ‘C’ ‘L’ ‘X’ Private data: length 4 listData currentPos predLoc location Inserting ‘S’ into proper position moreToSearch false ‘S’

72 72 Summary Array-based or linked representation for  stacks  queues  unsorted lists  sorted lists Variations:  doubly linked lists  circular lists  …


Download ppt "1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex."

Similar presentations


Ads by Google