Download presentation
Presentation is loading. Please wait.
Published byDina Ellis Modified over 9 years ago
1
Main Index Contents 11 Main Index Contents Week 4 – Stacks
2
Main Index Contents 2 VectorList Access an element at position/index i Direct access, V[i] Has to iterate from a known position, may use distance functiondistance Insert or remove an element at positions other than end Cause to relocate elements No relocation, just rebuild the link Sequential search an element with specific value, return the position/index Sequentially search
3
Main Index Contents 3 3 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative Containers VectorStackSet, Multiset DequeQueue Map, Mutltimap ListPriority Queue
4
Main Index Contents 4 Stacks A stack is a sequence of items that are accessible at only one end of the sequence.stack Last in, first out, (LIFO)
5
Main Index Contents 5 Pushing/Popping a Stack Because a pop removes the item that last added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.
6
Main Index Contents 66 Main Index Contents CLASS stack Operations stack(); Create an empty stack bool empty(); Check whether the stack is empty. Return true if it is empty and false otherwise.
7
Main Index Contents 77 Main Index Contents CLASS stack Operations void pop(); Remove the item from the top of the stack. Precondition:The stack is not empty. Postcondition:Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top.
8
Main Index Contents 88 Main Index Contents CLASS stack Operations int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition:The stack is not empty.
9
What would be the output on screen?
10
Since stack is specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container. Stack does not have iterators. How do we remove/insert an element at positions other than top? How do we search for an element with specific value?
11
Main Index Contents 11 Main Index Contents Uncoupling Stack Elements
12
Main Index Contents 12 Main Index Contents Uncoupling Stack Elements
13
Main Index Contents 13 Main Index Contents Uncoupling Stack Elements
14
Main Index Contents 14 Main Index Contents Uncoupling Stack Elements
15
Main Index Contents 15 Main Index Contents Uncoupling Stack Elements
16
Main Index Contents 16 Main Index Contents Uncoupling Stack Elements
18
Applications of Stack Backtracking in a Maze: We start from one point, there are several paths. Suppose we choose a random path. After following a certain path, we realize that the path we have chosen is wrong. So we need to find a way by which we can return to the beginning of that path. This is done by pushing that points that we have reached into a stack. We can pop the last point from the stack and thus return to the last point and continue our quest to find the right path.
19
19 Underlying storage structure Container classes The storage structure should be selected so that the container operations can be implemented efficiently.
20
Stack implementation stacksstacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements (pushed/popped from the "back", which is known as the top of the stack). The underlying container may be any of the standard container class templates (vector, list, deque). As lone as the container could support the following operations:vectorlistdeque empty size back push_back pop_back
21
Stack implementation template class miniStack { public: miniStack(); void push(const T& item); void pop(); T& top(); const T& top() const; bool empty(); int size() const; private: vector stackVector; };
24
Main Index Contents Reading Chapter 3.6 Next week: Queue, Tree and etc… No HW due this weekend! HW3 is due next Friday. (Submission before Thursday will receive 20 additional points) 24
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.