Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is a Stack? n 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.

Similar presentations


Presentation on theme: "What is a Stack? n 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."— Presentation transcript:

1 What is a Stack? n 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. n A stack is a “last in, first out” or LIFO LIFO structure.

2 Stack ADT Operations Transformers n makeEmpty -- Sets stack to an empty state. n push (ItemType newItem) -- Error if stack is full; otherwise adds newItem to the top of the stack. n pop (ItemType & item) -- Error if stack is empty; otherwise removes the item at the top of the stack and returns it in item. Observers n isEmpty -- Determines whether the stack is currently empty. n isFull -- Determines whether the stack is currently full. 2

3 // 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

4 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

5 Stack Client Code n printStack –iteratively –recursively n reverseStack –iteratively –recursively n reverseUnsortedList using Stack 5

6 DYNAMIC ARRAY IMPLEMENTATION class StackType Private Data: top 2 stackSize 5 stackArray 50 43 80 stackArray [0] stackArray[1] stackArray[2] stackArray[3] stackArray[4] StackType makeEmpty pop push isFull isEmpty ~StackType

7 // IMPLEMENTATION FILE (Stack.cpp) #include “Stack.h” #include 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; } 7

8 bool StackType::isEmpty( ) const { return ( top == -1 ); } bool StackType::isFull( ) const { return ( top == stackSize - 1 ); } StackType:: ~StackType ( ) { delete [ ] stackArray; } 8

9 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); } 9

10 charletter = ‘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); Tracing Client Code Private data: top stackArray [ stackSize-1 ]. [ 2 ] [ 1 ] stackArray [ 0 ] letter ‘V’

11 charletter = ‘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 Private data: top stackArray [ stackSize-1 ]. [ 2 ] K [ 1 ] C stackArray [ 0 ] V letter ‘V’

12 12 Client Code n printStack (iterative or recursive) n copyStack (iterative) n reverseStack n Applications of stacks –Run-time activation of records –Post-fix, pre-fix and infix evaluation of expressions –Backtracking searches (mazes)

13 What is a Class Template? 16.3 n A class template allows the compiler to generate multiple versions of a class type by using type parameters. n The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets,.

14 StackType numStack; top 3 [stackSize-1]. [ 3 ] 789 [ 2 ] -56 [ 1 ] 132 stackArray [ 0 ] 5670 14 ACTUAL PARAMETER

15 StackType nameStack; top 3 [stackSize-1]. [ 3 ] Bradley [ 2 ] Asad [ 1 ] Rodrigo stackArray [ 0 ] Max 15 ACTUAL PARAMETER

16 // CLASS TEMPLATE DEFINITION #include "ItemType.h" // for ItemType if it is a class!!! const int N = 100; template // 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; }; 16

17 // SAMPLE CLASS MEMBER FUNCTIONS template // formal parameter list StackType ::StackType( ) { top = -1; } template // formal parameter list void StackType ::push ( ItemType newItem ) { if (!IsFull()) { top++; items[top] = newItem;// STATIC ARRAY IMPLEMENTATION } 17

18 Using class templates n The actual parameter to the template is a data type. Any type can be used, either built-in or user-defined. n When using class templates, both the specification and implementation should be located in the same file, instead of in separate.h and.cpp files. 18


Download ppt "What is a Stack? n 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."

Similar presentations


Ads by Google