{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Stack - CSS342 Stacks and Compilers Professor Munehiro...

Info icon This preview shows pages 1–10. 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
Image of page 1

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

View Full Document Right 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)
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
Image of page 3

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

View Full Document Right 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
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. void push(const T& value) 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
Image of page 5

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

View Full Document Right 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
Image of page 6
CSS342: Stacks and Compilers 7 Stack Implementation Array-based implementation Pointer-based implementation Linked list reuse Data member Inheritance Implementation
Image of page 7

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

View Full Document Right 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
Image of page 8
CSS342: Stacks and Compilers 9 k ….. g ….. c b a top 0 1 2 k theArray.Size( ) - 1 pop(Object &topItem) push(const Object &newItem) Stack( ) : top( -1), theArray( 1 ) { } if (top == theArray.size( ) - 1) theArray.resize( theArray.size( ) * 2 + 1 ); item[++top] = newItem; if (top < 0) throw “empty stack”; else { topItem = theArray[top]; --top; } An Array-Based Implementation Implementation
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern