Stack - CSS342: Stacks and Compilers Professor: Munehiro...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CSS342: Stacks and Compilers 1 CSS342: Stacks and Compilers Professor: Munehiro Fukuda
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Stacks and Compilers 2 Topics Basic concepts of stacks Stack implementation Applying to language processing Example 1: Balance-symbol checker Example 2: Algebraic expressions Calculator design (lab/home work)
Background image of page 2
CSS342: Stacks and Compilers 3 Concepts of Stack Stack of cafeteria dishes Inherently unfair in day-by-day situations Useful problem-solving techniques in Computer Science Using a backspace key to correct typos Checking for balanced braces push pop abcc ddde ef a d d d c c b e e f a c b d a c b a c c b d d d e a c b push pop push pushpush pushpushpushpush pop pop pop pushpush Basic Concepts
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Stacks and Compilers 4 Stack Specification template<class Object> class stack { public: stack(); bool empty( ) const; void push( const Object &newItem ); void pop( ); void pop( Object &stackTop ); // not in STL const Object top( ) const; } a b c c a pop (): remove from the top push to the top pop(stacktop): remove and get the top item retrieve the top item but do not remove it Axiom: (aStack.push(newItem)).pop() is equal to aStack Basic Concepts
Background image of page 4
CSS342: Stacks and Compilers 5 STL Class stack #include <stack> stack(); Creates an empty stack. You can convert a list into a new stack. See your text page 249 (Sec. 7.6.2) Example: stack<int>, vector<int>> aStack; stack<int> bStack; bool empty( ) const; Returns if the stack is empty. Example: while ( aStack.empty( ) == false ) { … } size_type size( ) const; Returns the number of elements. Size_type is an integral type. You can compare it with an integer. Example: while (aStack.size( ) > 0) { … } T &top( ); Returns a reference to the top of the stack. Example: while( !aStack.empty() ) { cout << aStack.top() << endl; aStack.pop(); } void pop(); Remove the top item from the stack. Example: See the top( ) example. Adds an item with value to the top of the stack. Example: stack<int> aStack; for( int i=0; i < 10; i++ ) aStack.push(i); Basic Concepts
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Stacks and Compilers 6 STL Class stack Example #include <stack> #include <iostream> using namespace std; int main( ) { stack <int> aStack; int item; for ( int j = 0; j < 5; j++ ) aStack.push( j ); while ( !aStack.empty( ) ) { cout << aStack.top( ) << endl; aStack.pop( ); } } Results: 4 3 2 1 0 Basic Concepts
Background image of page 6
CSS342: Stacks and Compilers 7 Stack Implementation Array-based implementation Pointer-based implementation Linked list reuse Data member Inheritance Implementation
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Stacks and Compilers 8 An Array-Based Implementation k ….. g ….. c b a top 0 1 2 k (MAX_STACK-1) template <class Object> class stack { public: stack(); bool isEmpty( ) const; void push( const Object &newItem ); void pop( ); void pop( const Object &stackTop ); const Object top( ) const; private: vector<Object> theArray; //Object theArray[MAX_STACK–1]; in an ordinary array int top; } Implementation
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 12/20/2009.

Page1 / 30

Stack - CSS342: Stacks and Compilers Professor: Munehiro...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online