lecture5

lecture5 - Wednesday,January20 th Stacks Queues Butfirst...

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

View Full Document Right Arrow Icon
Wednesday, January 20 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 A stack is an ADT that holds a collection of items (like  int s) where the elements  are always added to one end. 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. Just like a stack of plates, the last item pushed onto the top of a stack is the  first item to be removed.
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? last in first out
Background image of page 4
Stacks Question What type of data structure can we use to  implement our stack?  class Stack   // stack of  ints { public:    Stack();   // c’tor    void push( int  i);     int  pop();    bool is_empty(void);     int peek_top(); private:    . .. };  void main(void) { Stack is; is.push(10); is.push(20); ... } Answer: How about an  array  and a  counter variable  to track  where the top of the stack is?
Background image of page 5

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

View Full DocumentRight Arrow Icon
Implementing a Stack const int SIZE = 100; class Stack { public: Stack() { } void push(int val) { } int pop() { ... private: }; m_top = 0;  int m_stack[ SIZE ];    int m_top; Let’s use an array to hold our  stack items. This stack may hold a maximum  of 100 items. We’ll use a simple  int  to keep  track of where the next item  should be added  to the stack. To initialize our stack, we’ll  specify that the  first item  should  go in the  0 th  slot  of the array. if (m_top >= SIZE) cout << “Argh!”; Let’s make sure we never over-fill  (overflow) our stack! m_stack[m_top] = val; Place our  new value  in the  next  open slot  of the array…  m_top   specifies where that is! m_top += 1; Update the location where our  next item  should be placed in the  array. if (m_top == 0) cout << “Argh!”; We can’t pop an item from our  stack if it’s empty!  Tell the user! m_top -= 1; Since  m_top  points to where our  next item  will be pushed Let’s  decrement it  to point it to where the  current top item  is!
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/10/2012 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 38

lecture5 - Wednesday,January20 th Stacks Queues Butfirst...

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

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