stack is empty bool int stackempty const return top index 1 Check if the int

Stack is empty bool int stackempty const return top

This preview shows page 7 - 13 out of 21 pages.

stack is empty bool int stack::empty( ) const { return (top index == -1); } // Check if the int stack is full bool int stack::full( ) const { return (top index == BUFFER SIZE-1); } // Give the number of data currently stored int int stack::size( ) const { return top index+1; } // Retrieve the value of the top item int int stack::top( ) const { if ( !empty( ) ) return data[top index]; cerr "Warning: Stack is empty; can’t retrieve any data!" endl; exit(-1); } { lixin, mak, stavrosp } @cse.ust.hk COMP2011 (Fall 2013) p.7
Image of page 7
Stack of int Data — Mutators #include int-stack.h / * File: int-stack2.cpp * / / ***** MUTATOR member functions ***** / void int stack::push( int x) // Add a new item to the top of the stack { if ( !full( ) ) data[++top index] = x; else { cerr "Error: Stack is full; can’t add (" x ")!" endl; exit(-1); } } void int stack::pop( ) // Remove the top item from the stack { if ( !empty( ) ) --top index; else { cerr "Error: Stack is empty; can’t remove any data!" endl; exit(-1); } } { lixin, mak, stavrosp } @cse.ust.hk COMP2011 (Fall 2013) p.8
Image of page 8
Stack of int Data — Test Program #include int-stack.h / * File: int-stack-test.cpp * / void print stack info( const int stack& s) { cout "No. of data currently on the stack = " s.size( ) " \ t" ; if ( !s.empty( ) ) cout "Top item = " s.top( ); cout endl "Empty: " boolalpha s.empty( ); cout " \ t \ t" "Full: " boolalpha s.full( ) endl; } int main( void ) { int stack a; print stack info(a); a.push(4); print stack info(a); a.push(15); print stack info(a); a.push(26); print stack info(a); a.push(37); print stack info(a); a.pop( ); print stack info(a); a.push(48); print stack info(a); a.push(59); print stack info(a); return 0; } / * compile: g ++ -L. -o int-stack-test int-stack-test.cpp -lintstack * / { lixin, mak, stavrosp } @cse.ust.hk COMP2011 (Fall 2013) p.9
Image of page 9
Example: Decimal to Binary Conversion — Illustration e.g., 26 (10) = 11010 (2) Algorithm to convert N (10) = M (2) : Step 1 : divide N by 2 successively Step 2 : each time push the remainder onto a stack Step 3 : print the answer by popping the stack successively { lixin, mak, stavrosp } @cse.ust.hk COMP2011 (Fall 2013) p.10
Image of page 10
Example: Decimal to Binary Conversion #include int-stack/int-stack.h / * File: decimal2binary.cpp * / int main( void ) // Convert + ve decimal number to binary number using an stack { int stack a; int x, number; while (cin number) { // Conversion: decimal to binary for (x = number; x > 0; x /= 2) a.push(x % 2); // Print a binary that is stored on a stack cout number "(base 10) = " ; while ( !a.empty( ) ) { cout a.top( ); a.pop( ); } cout "(base 2)" endl; } return 0; } // Compile: g ++ -o decimal2binary -Lint-stack decimal2binary.cpp -lintstack { lixin, mak, stavrosp } @cse.ust.hk COMP2011 (Fall 2013) p.11
Image of page 11
Example: Balanced Parentheses — Illustration e.g., [()][()()]() is balanced but [(]) is not.
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 21 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes