180 likes | 413 Views
What is a Stack?. Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only at the top of the stack. A stack is a “last in, first out” or LIFO LIFO structure. Stack ADT Operations. Transformers
E N D
What is a Stack? • Logical (or ADT) level:A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only at the top of the stack. • A stack is a “last in, first out” or LIFOLIFO structure.
Stack ADT Operations Transformers • makeEmpty -- Sets stack to an empty state. • push (ItemType newItem)-- Error if stack is full; otherwise adds newItem to the top of the stack. • pop (ItemType & item) -- Error if stack is empty; otherwise removes the item at the top of the stack and returns it in item. Observers • isEmpty -- Determines whether the stack is currently empty. • isFull -- Determines whether the stack is currently full. 2
// SPECIFICATION FILE: StackType.h #include "ItemType.h" // for class ItemType definition using namespace std; const int N = 100; class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. StackType(int); // Paramterized constructor. // POST: Stack is created and empty. ~StackType ( ); //PRE: Stack is created //POST: Stack is destroyed void makeEmpty( ); // PRE: None. // POST: Stack is empty. bool isEmpty() const; // PRE: Stack has been initialized. // POST: Function value = (stack is empty) 3
bool isFull( ) const; // PRE: Stack has been initialized. // POST: Function value = (stack is full) void push( ItemType newItem ); // PRE: Stack has been initialized and is not full. // POST: If stack is full, an exception is thrown; // otherwise newItem is at the top of the // stack. void pop( ItemType& item ); // PRE: Stack has been initialized and is not empty. // POST: If stack if empty, an exception is thrown; // otherwise top element has been removed from // stack.item is a copy of removed element. private: ItemType * stackArray; int stackSize; int top; }; 4
DYNAMIC ARRAY IMPLEMENTATION class StackType StackType ~StackType Private Data: top 2 stackSize 5 stackArray 50 43 80 makeEmpty stackArray [0] stackArray[1] stackArray[2] stackArray[3] stackArray[4] isEmpty isFull push pop
// IMPLEMENTATION FILE (Stack.cpp) #include “Stack.h” #include <iostream> using namespace std; StackType::StackType( ) { stackSize = N; stackArray = new ItemType [stackSize]; top = -1; } StackType::StackType( int size ) { stackSize = size; stackArray = new ItemType [stackSize]; top = -1; } void StackType::makeEmpty() { top = -1; } 6
bool StackType::isEmpty( ) const { return ( top == -1 ); } bool StackType::isFull() const { return ( top == stackSize - 1 ); } StackType:: ~StackType ( ) { delete [ ] stackArray; } 7
void StackType::push ( ItemType newItem ) { if (!isFull()) { top++; stackArray[top] = newItem; } else { cout << “The stack is full.\n”; exit(1); } } void StackType::pop ( ItemType& item ) { if (!isEmpty()) { item = stackArray[top]; top--; } else { cout << “The stack is empty.\n”; exit(1); } } 8
Tracing Client Code letter ‘V’ Private data: top stackArray [stackSize-1] . . . [ 2 ] [ 1 ] stackArray [ 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);
End of Trace letter ‘V’ Private data: top stackArray [stackSize-1] . . . [ 2 ] K [ 1 ] C stackArray [ 0 ] V 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);
Client Code • printStack (iterative or recursive) • copyStack (iterative) • reverseStack • Applications of stacks • Run-time activation of records • Post-fix, pre-fix and infix evaluation of expressions • Backtracking searches (mazes)
What is a Class Template? 16.3 • A class template allows the compiler to generate multiple versions of a class type by using type parameters. • The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets, < >.
ACTUAL PARAMETER top 3 [stackSize-1] . . . [ 3 ] 789 [ 2 ] -56 [ 1 ] 132 stackArray [ 0 ] 5670 StackType<int> numStack; 13
ACTUAL PARAMETER top 3 [stackSize-1] . . . [ 3 ] Bradley [ 2 ] Asad [ 1 ] Rodrigo stackArray [ 0 ] Max StackType<string> nameStack; 14
// CLASS TEMPLATE DEFINITION #include "ItemType.h" // for ItemType if it is a class!!! const int N = 100; template <class ItemType>// formal parameter list class StackType { public: StackType( ); StackType(int); ~StackType ( ); void makeEmpty( ); bool isEmpty() const; bool isFull( ) const; void push( ItemType newItem ); void pop( ItemType & item ); private: ItemType * stackArray; int stackSize; int top; }; 15
// SAMPLE CLASS MEMBER FUNCTIONS template<class ItemType>// formal parameter list StackType<ItemType>::StackType( ) { top = -1; } template<class ItemType>// formal parameter list void StackType<ItemType>::push ( ItemType newItem ) { if (!IsFull()) { top++; items[top] = newItem; // STATIC ARRAY IMPLEMENTATION } } 16
Using class templates • The actual parameterto the templateis a data type. Any type can be used, either built-in or user-defined. • When using class templates, both the specification and implementation should be located in the same file, instead of in separate .h and .cpp files. 17