stacks
DESCRIPTION
Stacks. Chapter 8. Objectives. In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list Discover stack applications Learn how to use a stack to remove recursion. Stacks. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/1.jpg)
Stacks
Chapter 8
![Page 2: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/2.jpg)
![Page 3: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/3.jpg)
3
Objectives
In this chapter, you will:• Learn about stacks• Examine various stack operations• Learn how to implement a stack as an array• Learn how to implement a stack as a linked list• Discover stack applications• Learn how to use a stack to remove recursion
C++ Programming: Program Design Including Data Structures, Fifth
Edition
![Page 4: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/4.jpg)
Stacks• Stack: list of homogenous elements
– Addition and deletion occur only at one end, called the top of the stack
– Example: in a cafeteria, the second tray can be removed only if first tray has been removed
– Last in first out (LIFO) data structure • Operations:
– Push: to add an element onto the stack – Pop: to remove an element from the stack
![Page 5: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/5.jpg)
Stacks
• Linear list.• One end is called top.• Other end is called bottom.• Additions to and removals from the top end only.
top
bottom
![Page 6: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/6.jpg)
Various types of stacks
![Page 7: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/7.jpg)
Stack Of Cups
• Add a cup to the stack.
bottom
top
C
A
B
D
E
F
• Remove a cup from new stack.• A stack is a LIFO list.
bottom
top
C
A
B
D
E
![Page 8: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/8.jpg)
The Abstract Class stack
template<class T>class stack { public: virtual ~stack() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& top() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0;};
![Page 9: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/9.jpg)
Derive From A Linear List Class
• Class arrayList• Class chain
![Page 10: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/10.jpg)
– stack top is either left end or right end of linear list– empty() => arrayList::empty()– size() => arrayList::size()– top() => get(0) or get(size() - 1)
0 1 2 3 4 5 6
a b c d e
Derive From arrayList
![Page 11: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/11.jpg)
Derive From arrayList
• when top is left end of linear list–push(theElement) => insert(0, theElement)–pop() => erase(0)–use left end of list as top of stack
0 1 2 3 4 5 6
a b c d e
![Page 12: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/12.jpg)
Derive From arrayList
–when top is right end of linear list• push(theElement) => insert(size(), theElement)• pop() => erase(size()-1)
–use right end of list as top of stack
0 1 2 3 4 5 6
a b c d e
![Page 13: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/13.jpg)
Derive From Chain
–stack top is either left end or right end of linear list
–empty() => chain::empty()–size() => chain::size()
a b c d e
NULL
firstNode
![Page 14: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/14.jpg)
Derive From Chain
a b c d e
NULL
firstNode
– when top is left end of linear list top() => get(0)push(theElement) => insert(0, theElement)pop() => erase(0)
![Page 15: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/15.jpg)
Derive From Chain
a b c d enull
firstNode
– when top is right end of linear list• top() => get(size() - 1)• push(theElement) => insert(size(), theElement)• pop() => erase(size()-1)•use left end of list as top of stack
![Page 16: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/16.jpg)
Derive From arrayList
template<class T>class derivedArrayStack : private arrayList<T>, public stack<T>{ public: // code for stack methods comes here };
![Page 17: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/17.jpg)
Constructor
derivedArrayStack(int initialCapacity = 10) : arrayList<T> (initialCapacity) {}
![Page 18: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/18.jpg)
empty() And size()
bool empty() const{return arrayList<T>::empty();}
int size() const{return arrayList<T>::size();}
0 1 2 3 4 5 6
a b c d e
![Page 19: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/19.jpg)
top()
T& top(){ if (arrayList<T>::empty()) throw stackEmpty(); return get(arrayList<T>::size() - 1);}
0 1 2 3 4 5 6
a b c d e
![Page 20: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/20.jpg)
push(theElement)
void push(const T& theElement){insert(arrayList<T>::size(), theElement);}
0 1 2 3 4 5 6
a b c d e
![Page 21: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/21.jpg)
pop()
void pop(){ if (arrayList<T>::empty()) throw stackEmpty(); erase(arrayList<T>::size() - 1);}
0 1 2 3 4 5 6
a b c d e
![Page 22: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/22.jpg)
template<class T>class derivedArrayStack : private arrayList<T>,public stack<T>{ public: derivedArrayStack(int initialCapacity = 10) :arrayList<T>(initialCapacity) {} bool empty() const {return arrayList<T>::empty();} int size() const {return arrayList<T>::size();} T& top() { if (arrayList<T>::empty()) throw stackEmpty(); return get(arrayList<T>::size() - 1); } void pop() { if (arrayList<T>::empty()) throw stackEmpty(); erase(arrayList<T>::size() - 1); } void push(const T& theElement) {insert(arrayList<T>::size(), theElement);}};
![Page 23: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/23.jpg)
Evaluation• Merits of deriving from arrayList
– Code for derived class is quite simple and easy to develop.
– Code is expected to require little debugging.– Code for other stack implementations such as a
linked implementation are easily obtained.• Just replace private arrayList<T> with private chain<T>• For efficiency reasons we must also make changes to use
the left end of the list as the stack top rather than the right end.
![Page 24: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/24.jpg)
Code From Scratch• Use a 1D array stack whose data type is T.
– same as using array element in arrayList• Use an int variable stackTop.
– Stack elements are in stack[0:stackTop].– Top element is in stack[stackTop].– Bottom element is in stack[0].– Stack is empty iff stackTop = -1.– Number of elements in stack is stackTop + 1.
![Page 25: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/25.jpg)
Code From Scratchtemplate class<T>class arrayStack : public stack<T>{ public: // public methods come here private: int stackTop; // current top of stack int arrayLength; // stack capacity T *stack; // element array};
![Page 26: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/26.jpg)
Constructor
template<class T>arrayStack<T>::arrayStack(int initialCapacity){// Constructor. if (initialCapacity < 1) {// code to throw an exception comes here } arrayLength = initialCapacity; stack = new T[arrayLength]; stackTop = -1;}
![Page 27: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/27.jpg)
push(…)
template<class T>void arrayStack<T>::push(const T& theElement){// Add theElement to stack. if (stackTop == arrayLength - 1) {// code to double capacity coms here } // add at stack top stack[++stackTop] = theElement;}
0 1 2 3 4
a b c d e
top
![Page 28: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/28.jpg)
pop()
void pop(){ if (stackTop == -1) throw stackEmpty(); stack[stackTop--].~T(); // destructor for T}
0 1 2 3 4
a b c d e
top
![Page 29: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/29.jpg)
Linked Stacktemplate<class T>class linkedStack : public stack<T>{ public: linkedStack(int initialCapacity = 10) {stackTop = NULL; stackSize = 0;} ~linkedStack(); bool empty() const {return stackSize == 0;} int size() const {return stackSize;} T& top() { if (stackSize == 0) throw stackEmpty();
return stackTop->element; } void pop(); void push(const T& theElement);private: chainNode<T>* stackTop; // pointer to stack top int stackSize; // number of elements in stack};
![Page 30: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/30.jpg)
Push()
template<class T>void linkedStack<T>::push(const T& theElement) { stackTop = new chainNode<T>(theElement, stackTop); stackSize++; }
![Page 31: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/31.jpg)
Pop()template<class T>void linkedStack<T>::pop(){// Delete top element. if (stackSize == 0) throw stackEmpty();
chainNode<T>* nextNode = stackTop->next; delete stackTop; stackTop = nextNode; stackSize--;}
![Page 32: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/32.jpg)
Implement stack
• derivedArrayStack.h• derivedLinkedStack.h• arrayStack.h• linkedStack.h
![Page 33: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/33.jpg)
Stack in C++
![Page 34: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/34.jpg)
Applications
![Page 35: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/35.jpg)
Parentheses Matching• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
– Output pairs (u,v) such that the left parenthesis at position u is matched with the right parenthesis at v.• (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
• (a+b))*((c+d)– (0,4)– right parenthesis at 5 has no matching left parenthesis– (8,12)– left parenthesis at 7 has no matching right parenthesis
![Page 36: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/36.jpg)
Parentheses Matching• scan expression from left to right• when a left parenthesis is encountered, add its
position to the stack• when a right parenthesis is encountered, remove
matching position from stack
![Page 37: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/37.jpg)
Example• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1 2
![Page 38: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/38.jpg)
Example• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1
(2,6)
![Page 39: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/39.jpg)
Example• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1
(2,6) (1,13)15
![Page 40: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/40.jpg)
Example• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1
(2,6) (1,13) (15,19)21
![Page 41: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/41.jpg)
Example• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1
(2,6) (1,13) (15,19) (21,25)27
![Page 42: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/42.jpg)
Example• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1
(2,6) (1,13) (15,19) (21,25)(27,31) (0,32)
• and so on
![Page 43: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/43.jpg)
Homework 2.1
• Write a program that look for matched pairs of parentheses and matched pairs of brackets ([]) and report a nesting problem.
• For example: in the string (a+[b*(c-d)+f]), the output should be (0,14),(3,13),(6,10).
![Page 44: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/44.jpg)
Towers Of Hanoi/Brahma
A B C1234
• 64 gold disks to be moved from tower A to tower C• each tower operates as a stack• cannot place big disk on top of a smaller one
![Page 45: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/45.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C123
![Page 46: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/46.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C12
3
![Page 47: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/47.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C1 2 3
![Page 48: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/48.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C1 2
3
![Page 49: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/49.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C
123
![Page 50: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/50.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C
123
![Page 51: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/51.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C
12
3
![Page 52: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/52.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/BrahmaA B C
123
• 7 disk moves
![Page 53: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/53.jpg)
Recursive Solution
A B C1
• n > 0 gold disks to be moved from A to C using B• move top n-1 disks from A to B using C
![Page 54: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/54.jpg)
Recursive Solution
A B C1
• move top disk from A to C
![Page 55: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/55.jpg)
Recursive Solution
A B C1
• move top n-1 disks from B to C using A
![Page 56: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/56.jpg)
Recursive Solution
A B C1
• moves(n) = 0 when n = 0• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
![Page 57: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/57.jpg)
Homework 2.2• 2.2.1: Write a recursive version of program
to solve n-disk Tower of Hanoi problem• 2.2.2: determine the maximum number of
disk you can solve using recursive and stack programs.
![Page 58: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/58.jpg)
Rat In A Maze
![Page 59: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/59.jpg)
Rat In A Maze
• Move order is: right, down, left, up• Block positions to avoid revisit.
![Page 60: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/60.jpg)
Rat In A Maze
• Move order is: right, down, left, up• Block positions to avoid revisit.
![Page 61: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/61.jpg)
Rat In A Maze
• Move backward until we reach a square from which a forward move is possible.
![Page 62: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/62.jpg)
Rat In A Maze
• Move down.
![Page 63: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/63.jpg)
Rat In A Maze
• Move left.
![Page 64: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/64.jpg)
Rat In A Maze
• Move down.
![Page 65: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/65.jpg)
Rat In A Maze
• Move backward until we reach a square from which a forward move is possible.
![Page 66: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/66.jpg)
Rat In A Maze
• Move backward until we reach a square from which a forward move is possible.
• Move downward.
![Page 67: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/67.jpg)
Rat In A Maze
• Move right.• Backtrack.
![Page 68: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/68.jpg)
Rat In A Maze
• Move downward.
![Page 69: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/69.jpg)
Rat In A Maze
• Move right.
![Page 70: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/70.jpg)
Rat In A Maze
• Move one down and then right.
![Page 71: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/71.jpg)
Rat In A Maze
• Move one up and then right.
![Page 72: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/72.jpg)
Rat In A Maze
• Move down to exit and eat cheese.• Path from maze entry to current position operates as
a stack.
![Page 73: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/73.jpg)
Homework 2.3 (bonus)
• Implement the Rat in a maze
![Page 74: Stacks](https://reader036.vdocuments.us/reader036/viewer/2022062520/56816391550346895dd488df/html5/thumbnails/74.jpg)
Homework 2• Due date: Feb 24