1 / 17

What is a Stack?

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

chiko
Télécharger la présentation

What is a Stack?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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

  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. 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

  6. // 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

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

  8. 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

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

  10. 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);

  11. 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)

  12. 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, < >.

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

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

  15. // 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

  16. // 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

  17. 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

More Related