1 / 72

Understanding Stack Data Structure: Operations and Implementations

Explore the concept of a stack data structure, its operations, and implementations in C++. Learn about stack operations like MakeEmpty, IsEmpty, IsFull, Push, Pop, and Top in this informative guide. Includes code snippets and examples.

fujita
Download Presentation

Understanding Stack Data Structure: Operations and Implementations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 Pop Observers IsEmpty IsFull • change state • observe state

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

  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: • top • [MAX_ITEMS-1] • . • . • . • [ 2 ] • [ 1 ] • items [ 0 ] • StackType • IsEmpty • IsFull • Push • Pop • Top

  11. Tracing Client Code • letter • ‘V’ • char letter = ‘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(0)} • Private data: • top • [MAX_ITEMS-1] • . • . • . • [ 2 ] • [ 1 ] • items [ 0 ]

  12. Tracing Client Code • letter • ‘V’ • char letter = ‘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(0)} • Private data: • top -1 • [MAX_ITEMS-1] • . • . • . • [ 2 ] • [ 1 ] • items [ 0 ]

  13. Tracing Client Code • ‘V’ • letter • char letter = ‘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(0)} • Private data: • top 0 • [MAX_ITEMS-1] • . • . • . • [ 2 ] • [ 1 ] • items [ 0 ] ‘V’

  14. Tracing Client Code • ‘V’ • char letter = ‘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(0)} • letter • Private data: • top 1 • [MAX_ITEMS-1] • . • . • . • [ 2 ] • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  15. Tracing Client Code • ‘V’ • char letter = ‘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(0)} • letter • Private data: • top 2 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘S’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  16. Tracing Client Code • ‘V’ • letter • char letter = ‘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(0)} • Private data: • top 2 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘S’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  17. Tracing Client Code • ‘V’ • char letter = ‘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(0)} • letter • Private data: • top 1 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘S’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’0

  18. Tracing Client Code • ‘V’ • char letter = ‘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(0)} • letter • Private data: • top 2 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  19. Tracing Client Code • ‘V’ • char letter = ‘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(0)} • letter • Private data: • top 2 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  20. Tracing Client Code • ‘K’ • char letter = ‘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(0)} • letter • Private data: • top 2 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  21. Tracing Client Code • ‘K’ • char letter = ‘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(0)} • letter • Private data: • top 1 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  22. Tracing Client Code • ‘K’ • char letter = ‘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(0)} • letter • Private data: • top 1 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  23. Tracing Client Code • ‘C’ • char letter = ‘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(0)} • letter • Private data: • top 0 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  24. Tracing Client Code • ‘C’ • letter • char letter = ‘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(0)} • Private data: • top 0 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  25. Tracing Client Code • ‘V’ • char letter = ‘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(0)} • letter • Private data: • top -1 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

  26. End of Trace • ‘V’ • char letter = ‘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(0)} • letter • Private data: • top -1 • [MAX_ITEMS-1] • . • . • . • [ 2 ] ‘K’ • [ 1 ] ‘C’ • items [ 0 ] ‘V’

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

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

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

  30. ItemType is string • Private data: • topPtr • StackType • cat dog • class StackType • Top • IsEmpty • IsFull • Push • Pop • ~StackType

  31. Tracing Client Code • ‘V’ • letter • char letter = ‘V’; • StackType myStack; • myStack.Push(letter); • myStack.Push(‘C’); • myStack.Push(‘S’); • If (!myStack.IsEmpty() ) • { • letter = myStack.Top( ); • myStack.Pop(); • } • myStack.Push(‘K’);

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

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

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

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

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

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

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

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

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

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

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

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

  44. // 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; };

  45. Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; • ‘X’ ‘C’ ‘L’ • topPtr • newItem • ‘B’

  46. Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; • ‘X’ ‘C’ ‘L’ • topPtr • newItem • ‘B’ • location

  47. Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; • ‘X’ ‘C’ ‘L’ • topPtr • ‘B’ • newItem • location

  48. Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; • ‘X’ ‘C’ ‘L’ • topPtr • newItem • ‘B’ • ‘B’ • location

  49. Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; • ‘X’ ‘C’ ‘L’ • topPtr • newItem • ‘B’ • ‘B’ • location

  50. Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; • topPtr • newItem • ‘B’ • ‘X’ ‘C’ ‘L’ • ‘B’ • location

More Related