lecture5 - stacks and queues

lecture5 - stacks and queues - Wednesday, January 14 th...

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

View Full Document Right Arrow Icon
Wednesday, January 14 th   Stacks  Queues
Background image of page 1

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

View Full DocumentRight Arrow Icon
But first…
Background image of page 2
The Stack: A Useful ADT Stack : an ADT that works like a stack of magazines or a stack of plates in a  cafeteria.  Stack operations:  put something on top of the stack ( PUSH  remove the top item ( POP  look at the top item, without removing it  check to see if the stack is empty   We can have a stack of any type of variable we like: ints, Squares, floats, strings, etc.
Background image of page 3

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

View Full DocumentRight Arrow Icon
The Stack I can… Push 5 on the stack. 5 Push -3 on the stack. -3 Push 9 on the stack. 9 Pop the top of the stack. Push 4 on the stack. 4 Pop the top of the stack Pop the top of the stack Pop the top of the stack Note:  You can only access the top item of the stack, since the other  items are covered. Note : The stack is called a  Last- In-First-Out  data structure.  Can you figure out why?
Background image of page 4
Stacks Question : What type of data structures can we  used to implement a stack?  typedef int DataType; class Stack  { public:    Stack();   // c’tor    bool push(DataType i);     bool pop(DataType &v);    bool is_empty(void);     bool peek_top(DataType &v); private:    . .. };  void main(void) { Stack is; int n = 30, a; is.push(10); is.push(20); is.peek_top(a); cout << a; is.push(n); is.pop(a); cout << a; is.push(15); is.pop(a); cout << a; is.pop(a); cout << a; is.pop(a); cout << a; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Stacks const int SIZE = 100; class Stack { public: Stack() { bool push(DataType val) { if (m_top >= SIZE) return(false); m_stack[m_top] = val; m_top += 1; return(true); } bool pop(DataType &v) { if (m_top == 0) return(false); m_top -= 1; v = m_stack[m_top]; return(true); } ... private: DataType m_stack[SIZE]; int m_top; }; void main(void) { Stack is; int a; is.push(5); is.push(10); is.pop(a); cout << a; is.push(7); } is m_top m_stack 0 1 2 ... 99 0 a 5 1 10 2 1 10 7 ] = 5 ] = 10 v 2 == 0?? v = m_stack[ 1 ] So… v = 10 7 1 >= 100? m_stack[ 1 ] = 7 5 7
Background image of page 6
Common Uses for Stacks  Stacks are one of the most USEFUL data structures in Computer Science.  They can be used for:   Evaluating postfix (Reverse Polish Notation) expressions   Converting from infix to postfix   All kinds of searching algorithms   To simulate recursion 
Background image of page 7

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

View Full DocumentRight Arrow Icon
Stacks Stacks are so popular that the C++ people actually wrote one for you. It’s in the  Standard Template Library (STL)! #include <iostream> #include <stack> using namespace std; main() { stack < int > istack; // stack of ints istack. push (10); istack. push (20); cout << istack. top (); // get top value istack. pop (); // kill top value if (istack. empty () == false) cout << istack. size (); } Note : The STL  pop()   command simply  removes the top item  from the stack,  but  doesn’t return it .
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 note was uploaded on 01/20/2011 for the course CS cs32 taught by Professor John during the Spring '09 term at UCLA.

Page1 / 32

lecture5 - stacks and queues - Wednesday, January 14 th...

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