Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 ADTs Stack and Queue.

Similar presentations


Presentation on theme: "Chapter 5 ADTs Stack and Queue."— 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 A stack is a LIFO “last in, first out” structure.
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(); 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()) 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 ‘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 [MAX_ITEMS-1] . [ 2 ] [ 1 ] items [ 0 ]

12 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 [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 [MAX_ITEMS-1] . [ 2 ] [ 1 ] items [ 0 ] ‘V’

14 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 [MAX_ITEMS-1] . [ 2 ] [ 1 ] ‘C’ items [ 0 ] ‘V’

15 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 [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 [MAX_ITEMS-1] . [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’

17 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 [MAX_ITEMS-1] . [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’0

18 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 [MAX_ITEMS-1] . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’

19 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 [MAX_ITEMS-1] . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’

20 Tracing Client Code ‘K’ 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 [MAX_ITEMS-1] . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’

21 Tracing Client Code ‘K’ 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 [MAX_ITEMS-1] . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’

22 Tracing Client Code ‘K’ 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 [MAX_ITEMS-1] . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’

23 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 [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 [MAX_ITEMS-1] . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’

25 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 [MAX_ITEMS-1] . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’

26 End of Trace ‘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 [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 class StackType ItemType is char StackType Top IsEmpty ‘C’ ‘V’ IsFull
Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘C’ ‘V’

29 class StackType ItemType is float StackType Top IsEmpty IsFull Push
Pop Push IsFull IsEmpty Private data: topPtr ~StackType

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

31 ‘V’ Tracing Client Code 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’ 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 ‘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’);

34 Tracing Client Code ‘V’ Private data: topPtr char letter = ‘V’;
‘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 ‘V’ Private data: topPtr char letter = ‘V’;
‘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 ‘V’ Private data: topPtr char letter = ‘V’;
‘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 ‘S’ Private data: topPtr char letter = ‘V’;
‘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 ‘S’ Private data: topPtr char letter = ‘V’;
‘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 ‘C’ 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’);

40 Tracing Client Code ‘C’ 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’);

41 Tracing Client Code ‘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 ‘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’ Private data: topPtr char letter = ‘V’;
‘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
‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’

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

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

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

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

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

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

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

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

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

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

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

57 Implementing Pop / Top 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. return topPtr->info;

58 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;

59 Example of a Queue

60 A queue is a FIFO “first in, first out” structure.
What is a Queue? 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). A queue is a FIFO “first in, first out” structure.

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

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

63 Public Interface of QueType
typedef char ItemType; class QueType { public: QueType(int max); // max is the size of the queue. QueType(); // Default size of 500. ~QueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); void Dequeue(ItemType& item);

64 SIMPLE ARRAY IMPLEMENTATION
class QueType QueType Private Data: front rear maxQue 5 items ~QueType Enqueue ‘C’‘X’ ‘J’ RESERVED Dequeue . items [0] [1] [2] [3] [4]

65 What is the private part of class QueType for this design?
What troubles do we encounter?

66 DYNAMIC ARRAY IMPLEMENTATION
class QueType QueType Private Data: front rear maxQue 5 items ~QueType Enqueue ‘C’ ‘X’ ‘J’ RESERVED Dequeue . items [0] [1] [2] [3] [4]

67 What is the private part of class QueType for this design?
Does this design have the same problems as the previous design?

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

69 What is the private part of class QueType for this design?

70 CountedQueType inherits from QueType
// DERIVE CLASS CountedQueType FROM BASE CLASS QueType class CountedQueType : QueType; { public: CountedQueType( ); void Enqueue( ItemType newItem ); void Dequeue( ItemType& item ); int GetLength( ) const; // Returns number of items on the counted queue. private: int length; };

71 What additional fields does CountedQueType need?
What functions must be changed?

72


Download ppt "Chapter 5 ADTs Stack and Queue."

Similar presentations


Ads by Google