Array Based Stack
Array Based Stack
Logical level : What do these composite objects all have in common ? Stack is an abstract data type in which elements are added and removed from only one end, i.e., the top. A last in, first out (LIFO) structure.
2
What is a stack?
It is an ordered group of homogeneous items. Items are added to and removed from the top of the stack LIFO property: Last In, First Out
The last item added would be the first to be removed
Stack Operations
Stack Specification
typedef int ItemType; const int MAX_ITEMS =100;
#include "ItemType.h" // Must be provided by the user of the class // Contains definitions for MAX_ITEMS and ItemType class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; };
Stack Implementation
StackType::StackType() { top = -1; } O(1)
O(1)
O(1)
O(1)
O(1)
Stack overflow
Stack underflow
an empty stack.
Array-Based Implementation
bool StackType::IsEmpty() const
bool StackType::IsFull() const { return ( top == MAX_ITEMS-1); } ItemType StackType::Top() const { if (IsEmpty()) throw EmptyStack(); return (items[top]); }
14
15
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push(K); while (!charStack.IsEmpty( )) charStack.Pop(letter);
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter); charStack.Push(K); while (!charStack.IsEmpty( )) charStack.Pop(letter)
Private data:
top
-1
[MAX_ITEMS-1]
. . .
[2] [1]
items [ 0 ]
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter); charStack.Push(K); while (!charStack.IsEmpty( )) charStack.Pop(letter) V
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop(letter ); charStack.Push(K); C V while (!charStack.IsEmpty( )) charStack.Pop(letter)
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter); S C V charStack.Push(K); while (!charStack.IsEmpty( )) charStack.Pop(letter)
Private data:
top
[MAX_ITEMS-1]
. . .
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter); S C V charStack.Push(K); while (!charStack.IsEmpty( )) charStack.Pop(letter)
Private data:
top
[MAX_ITEMS-1]
. . .
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter);
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
S
C V
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter);
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter);
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter);
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( letter);
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
[MAX_ITEMS-1]
. . .
[2]
[1] items [ 0 ]
K
C V
Templates
Templates allow the compiler to generate multiple
versions of a class type (or a function) by allowing parameterized types.
myStack.Push(35); yourStack.Push(584.39);
Function templates
The definitions of the member functions must be
rewritten as function templates.
template<class ItemType> StackType<ItemType>::StackType() { top = -1; } template<class ItemType> void StackType<ItemType>::MakeEmpty() { top = -1; }
template<class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
StackType.cpp separately from the application of the class (i.e., client code)! Compiler needs to know the data type of the stack to instantiate the class! But ... the data type is provided in the clients code!
Compiling templates
We must compile StackType.cpp and client code together!
(1) append StackType.cpp at the end of StackType.h or include StackType.cpp at the end of StackType.h (2) include StackType.h in clients code (3) Compile client code
delete [ ] items;
}
infix: 2+5 postfix: 2 5 + Expressions are evaluated from left to right. Precedence rules and parentheses are never needed!
Stack
{
ItemType item; StackType tempStack; while (!Stack.IsEmpty()) { Stack.Pop(item); if (item==oldItem) tempStack.Push(newItem); else tempStack.Push(item); } while (!tempStack.IsEmpty()) { tempStack.Pop(item); Stack.Push(item); } }
tempStack
3 2 1
1 5 3
Stack
3
oldItem = 2 newItem = 5