lecture5

lecture5 - 1 Wednesday, J anuar y 19 Stacks Queues th 2 But...

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

View Full Document Right Arrow Icon
1 Wednesday, January 19 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
2 But first…
Background image of page 2
3 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
4 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
5 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); 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
6 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! return m_stack[m_top]; Extract the value from the top of the stack and return it to the user.
Background image of page 6
7 Stacks const int SIZE = 100; class Stack { public: Stack() { m_top = 0; } bool push(int val) { if (m_top >= SIZE) cout << “Argh!”; m_stack[m_top] = val; m_top += 1; } int pop() { if (m_top == 0) cout << “Argh!”; m_top -= 1; return m_stack[m_top]; } ... private: int m_stack[SIZE]; int m_top; }; void main(void) { Stack is; int a; is.push(5); is.push(10); a = is.pop(); cout << a; is.push(7); } is m_top m_stack 0 1 2 ... 99 0 a 5 1 10 2 1 10 7 0 >= 100? m_stack[ 0 ] = 5 1 >= 100? m_stack[ 1 ] = 10 2 == 0?? return m_stack[ 1 ] So we return 10 7 1 >= 100? 1 ] = 7 5 0 7 The first item we push will be placed in m_stack[ 0 ].
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

lecture5 - 1 Wednesday, J anuar y 19 Stacks Queues th 2 But...

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

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