Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 (Part 1) ADT Stack 1 Fall 2010. 2 Stacks TOP OF THE STACK.

Similar presentations


Presentation on theme: "Chapter 5 (Part 1) ADT Stack 1 Fall 2010. 2 Stacks TOP OF THE STACK."— Presentation transcript:

1 Chapter 5 (Part 1) ADT Stack 1 Fall 2010

2 2 Stacks TOP OF THE STACK

3 3 Definition of Stack 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. A stack is a LIFO “last in, first out” structure.

4 Stack ADT Operations MakeEmpty -- Sets stack to an empty state. IsEmpty -- Determines whether the stack is currently empty. IsFull -- Determines whether the stack is currently full. Push (ItemType newItem) -- Throws exception if stack is full; otherwise adds newItem to the top of the stack. Pop -- Throws exception if stack is empty; otherwise removes the item at the top of the stack. ItemType Top -- Throws exception if stack is empty; otherwise returns a copy of the top item 4

5 ADT Stack Operations Transformers Push Pop MakeEmpty Observers IsEmpty IsFull Top change state observe state 5

6 6 // Class specification for Stack ADT in file StackType.h class FullStack // Exception class thrown by // Push when stack is full {}; class EmptyStack // Exception class thrown by // Pop and Top when stack is empty {}; #include "ItemType.h" class StackType { public: StackType( ); // Class constructor. bool IsFull () const; // Function: Determines whether the stack is full. // Pre: Stack has been initialized // Post: Function value = (stack is full) 6

7 7 bool IsEmpty() const; // Function: Determines whether the stack is empty. // Pre: Stack has been initialized. // Post: Function value = (stack is empty) void Push( ItemType item ); // Function: Adds newItem to the top of the stack. // Pre: Stack has been initialized. // Post: If (stack is full), FullStack exception is thrown; // otherwise, newItem is at the top of the stack. void Pop(); // Function: Removes top item from the stack. // Pre: Stack has been initialized. // Post: If (stack is empty), EmptyStack exception is thrown; // otherwise, top element has been removed from stack. ItemType Top(); // Function: Returns a copy of top item on the stack. // Pre: Stack has been initialized. // Post: If (stack is empty), EmptyStack exception is thrown; // otherwise, top element has been removed from stack. private: int top; ItemType items[MAX_ITEMS]; }; 7

8 8 // File: StackType.cpp #include "StackType.h" #include StackType::StackType( ) { top = -1; } bool StackType::IsEmpty() const { return(top = = -1); } bool StackType::IsFull() const { return (top = = MAX_ITEMS-1); } 8

9 9 void StackType::Push(ItemType newItem) { if( IsFull() ) throw FullStack(); top++; items[top] = newItem; } void StackType::Pop() { if( IsEmpty() ) throw EmptyStack(); top--; } ItemType StackType::Top() { if (IsEmpty()) throw EmptyStack(); return items[top]; } 9

10 10 Class Interface Diagram StackType class StackType Pop Push IsFull IsEmpty Private data: top [MAX_ITEMS-1]. [ 2 ] [ 1 ] items [ 0 ] Top

11 11 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();} Tracing Client Code Private data: top [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] letter ‘V’

12 12 Tracing Client Code letter ‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

13 13 Tracing Client Code letter ‘V’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

14 14 Tracing Client Code letter ‘V’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

15 15 Tracing Client Code letter ‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

16 16 Tracing Client Code letter ‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

17 17 Tracing Client Code letter ‘V’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop( );}

18 18 Tracing Client Code letter ‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

19 19 Tracing Client Code letter ‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

20 20 Tracing Client Code letter ‘K’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

21 21 Tracing Client Code letter ‘K’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

22 22 Tracing Client Code letter ‘K’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

23 23 Tracing Client Code letter ‘C’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

24 24 Tracing Client Code letter ‘C’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

25 25 Tracing Client Code letter ‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

26 26 End of Trace letter ‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop();}

27 27 Another Stack Implementation One advantage of an ADT is that the kind of implementation used can be changed. A dynamic array implementation of the stack has a weakness -- the maximum size of the stack must be specified. Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.

28 Stack ADT Operations IsEmpty -- Determines whether the stack is currently empty. IsFull -- Determines whether the stack is currently full. Push (ItemType newItem) -- Adds newItem to the top of the stack. Pop -- Removes the item at the top of the stack. Top – Returns a copy of top items 28

29 ADT Stack Operations Transformers Push Pop Observers IsEmpty IsFull Top change state observe state 29

30 30 ItemType is char class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘C’ ‘V’

31 31 class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType 23.4 -7.9 ItemType is float

32 32 ItemType is StrType class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType cat dog

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

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

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

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

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

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

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

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

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

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

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

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

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

46 46 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK struct NodeType; //Forward declaration class StackType { public: //Identical to previous implementation private: NodeType* topPtr; };. struct NodeType { ItemType info; NodeType* next; }; 46

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

48 48 A Single Node.info.next ‘D ’ The user’s data Pointer to the next node in the stack

49 49 Node terminology

50 50 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

51 51 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

52 52 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

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

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

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

56 56 void StackType::Push ( ItemType newItem ) // Adds newItem to the top of the stack. { if (IsFull()) throw FullStack(); else { NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; } 56 Implementing Push

57 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 57

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

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

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

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

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

63 63 void StackType::Pop()// Remove top item from Stack. { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr ->next; delete tempPtr; } ItemType StackType::Top() // Returns a copy of the top item in the stack. { if (IsEmpty()) throw EmptyStack(); else return topPtr->info; } 63 Implementing Pop / Top

64 64 Implementing IsFull bool StackType::IsFull() const // Returns true if there is no room for another // ItemType on the free store; false otherwise { NodeType* location; try { location = new NodeType; delete location; return false; } catch(std::bad_alloc exception) { return true; }

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

66 66 Implementing the Destructor stackType::~StackType() // Post: stack is empty; // All items have been deallocated. { NodeType* tempPtr; while (topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr-> next; delete tempPtr; }


Download ppt "Chapter 5 (Part 1) ADT Stack 1 Fall 2010. 2 Stacks TOP OF THE STACK."

Similar presentations


Ads by Google