840 likes | 1.01k Views
Chapter 5 ADTs Stack and Queue. Stacks of Boxes and Books. TOP OF THE STACK. TOP OF THE STACK. 2. Definition of Stack. Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements),
E N D
Chapter 5 ADTs Stack and Queue
Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK 2
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. • One advantage of an ADT is that the implementation can be changed without the program using it knowing about it. 3
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 (ItemTypenewItem) -- 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
ADT Stack Operations Transformers • Push • Pop Observers • IsEmpty • IsFull • Top change state observe state 5
class StackType { public: StackType( ); boolIsFull () const; boolIsEmpty() const; void Push( ItemType item ); void Pop(); ItemTypeTop(); private: int top; ItemType items[MAX_ITEMS]; }; 6
// File: StackType.cpp #include "StackType.h" #include <iostream> StackType::StackType( ) { top = -1; } boolStackType::IsEmpty() const { return(top == -1); } boolStackType::IsFull() const { return (top == MAX_ITEMS-1); } 7
void StackType::Push(ItemTypenewItem) { if( IsFull() ) throw FullStack(): top++; items[top] = newItem; } void StackType::Pop() { if( IsEmpty() ) throw EmptyStack(); top--; } ItemTypeStackType::Top() { if (IsEmpty()) throw EmptyStack(); return items[top]; } 8
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 9
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 ] 10
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 ] 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 0 [MAX_ITEMS-1] . . . [ 2 ] [ 1 ] items [ 0 ] ‘V’ 12
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’ 13
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’ 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 2 [MAX_ITEMS-1] . . . [ 2 ] ‘S’ [ 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 1 [MAX_ITEMS-1] . . . [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’ 16
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’ 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 2 [MAX_ITEMS-1] . . . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ 18
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’ 19
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’ 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 1 [MAX_ITEMS-1] . . . [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ 21
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’ 22
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’ 23
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’ 24
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’ 25
StackType Top Private data: topPtr IsEmpty ‘C’‘V’ IsFull Push Pop ~StackType class StackType ItemType is char 26
StackType Top Private data: topPtr IsEmpty IsFull Push Pop ~StackType class StackType ItemType is float 23.4 -7.9 27
StackType Top Private data: topPtr IsEmpty cat dog IsFull Push Pop ~StackType class StackType ItemType is string 28
Tracing Client Code ‘V’ letter char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 29
‘V’ letter Tracing Client Code Private data: topPtrNULL char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 30
letter ‘V’ Tracing Client Code Private data: topPtr ‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 31
‘V’ letter Private data: topPtr Tracing Client Code ‘C’‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 32
letter ‘V’ Private data: topPtr Tracing Client Code ‘S’ ‘C’‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 33
letter ‘V’ Private data: topPtr Tracing Client Code ‘S’‘C’ ‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 34
letter ‘S’ Private data: topPtr Tracing Client Code ‘C’‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 35
letter ‘S’ Private data: topPtr Tracing Client Code ‘C’‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 36
letter ‘C’ Private data: topPtr Tracing Client Code ‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 37
letter ‘C’ Private data: topPtr Tracing Client Code ‘V’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 38
letter ‘V’ Private data: topPtr Tracing Client Code char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 39
letter ‘V’ Private data: topPtr Tracing Client Code char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 40
‘V’ letter Private data: topPtr Tracing Client Code ‘K’ char letter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); while (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’); 41
// DYNAMICALLY LINKED IMPLEMENTATION OF STACK StructNodeType; //Forward declaration class StackType { public: //Identical to previous implementation private: NodeType* topPtr; }; . . . StructNodeType { ItemType info; NodeType* next; }; 42
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; 43
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; location 44
‘X’‘C’‘L’ topPtr Adding newItem to the stack ‘B’ newItem newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; location 45
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; ‘B’ location 46
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; ‘B’ location 47
topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; ‘X’‘C’‘L’ ‘B’ location 48
Implementing Push void StackType::Push ( ItemTypenewItem ) // 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? 49
topPtr Deleting item from the stack item NodeType* tempPtr; topPtr = topPtr->next; delete tempPtr; ‘B’‘X’‘C’‘L’ tempPtr 50