chapter 5
play

Chapter 5 ADTs Stack and Queue Stacks of Coins and Bills Stacks - PowerPoint PPT Presentation

Chapter 5 ADTs Stack and Queue Stacks of Coins and Bills Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition


  1. Chapter 5 ADTs Stack and Queue

  2. Stacks of Coins and Bills

  3. Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK

  4. • 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.

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

  6. ADT Stack Operations Transformers • Push change state • Pop � � � � Observers � � • IsEmpty � • IsFull observe state � � � �

  7. class StackType { public: � StackType( ); bool IsFull () const; What are the bool IsEmpty() const; pre and post void Push( ItemType item ); conditions? void Pop(); � private: ItemType Top(); private: int top; ItemType items[MAX_ITEMS]; };

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

  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]; }

  10. Class Interface Diagram 
 (Memory reversed to better illustrate concept) StackType class Private data: � StackType top � � IsEmpty [MAX_ITEMS-1] � IsFull . . � . Push [ 2 ] � [ 1 ] � Pop items [ 0 ] Top

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Recommend


More recommend