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.

Slides:



Advertisements
Similar presentations
Stacks Example: Stack of plates in cafeteria.
Advertisements

Data Structures and Algorithms (60-254)
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Queues CSE, POSTECH 2 2 Queues Like a stack, special kind of linear list One end is called front Other end is called rear Additions (insertions or enqueue)
Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top.
Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Data Structure Dr. Mohamed Khafagy. Stacks Stack: what is it? ADT Applications Implementation(s)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Chapter 7 Stacks Dr. Youssef Harrath
Data Structures and Algorithms
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
SNU IDB Lab. 1 Ch9. Stacks © copyright 2006 SNU IDB Lab.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Data Structures & Algorithms
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only. FIFO.
CHP-3 STACKS.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
STACK Data Structure
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
1 Chapter 5 Stacks. 2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses.
Reminder: Course Policy
Stacks Stacks.
Stacks 황승원 Fall 2010 CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom.
Stacks.
Stacks Chapter 7 introduces the stack data type.
Stacks Stack: restricted variant of list
The Class chain.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks template<class T> class stack { public:
Stacks template<class T> class stack { public:
Queues Linear list. One end is called front. Other end is called rear.
Stacks template<class T> class stack { public:
Stacks public interface Stack { public boolean empty();
The Class chain.
The Class chain.
Stacks template<class T> class stack { public:
Queues Linear list. One end is called front. Other end is called rear.
CHAPTER 3: Collections—Stacks
Presentation transcript:

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 C++ Programming: Program Design Including Data Structures, Fifth Edition3

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

Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only. top bottom

Various types of stacks

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

The Abstract Class stack template 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; };

Derive From A Linear List Class Class arrayList Class chain

–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) abcde 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 abcde

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 abcde

Derive From Chain –stack top is either left end or right end of linear list –empty() => chain::empty() –size() => chain::size() abcde NULL firstNode

Derive From Chain abcde NULL firstNode – when top is left end of linear list  top() => get(0)  push(theElement) => insert(0, theElement)  pop() => erase(0)

Derive From Chain abcde null 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

Derive From arrayList template class derivedArrayStack : private arrayList, public stack { public: // code for stack methods comes here };

Constructor derivedArrayStack(int initialCapacity = 10) : arrayList (initialCapacity) {}

empty() And size() bool empty() const {return arrayList ::empty();} int size() const {return arrayList ::size();} abcde

top() T& top() { if (arrayList ::empty()) throw stackEmpty(); return get(arrayList ::size() - 1); } abcde

push(theElement) void push(const T& theElement) {insert(arrayList ::size(), theElement);} abcde

pop() void pop() { if (arrayList ::empty()) throw stackEmpty(); erase(arrayList ::size() - 1); } abcde

template class derivedArrayStack : private arrayList,public stack { public: derivedArrayStack(int initialCapacity = 10) :arrayList (initialCapacity) {} bool empty() const {return arrayList ::empty();} int size() const {return arrayList ::size();} T& top() { if (arrayList ::empty()) throw stackEmpty(); return get(arrayList ::size() - 1); } void pop() { if (arrayList ::empty()) throw stackEmpty(); erase(arrayList ::size() - 1); } void push(const T& theElement) {insert(arrayList ::size(), theElement);} };

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 with private chain 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.

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.

Code From Scratch template class class arrayStack : public stack { public: // public methods come here private: int stackTop; // current top of stack int arrayLength; // stack capacity T *stack; // element array };

Constructor template arrayStack ::arrayStack(int initialCapacity) {// Constructor. if (initialCapacity < 1) {// code to throw an exception comes here } arrayLength = initialCapacity; stack = new T[arrayLength]; stackTop = -1; }

push(…) template void arrayStack ::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; } abcde top

pop() void pop() { if (stackTop == -1) throw stackEmpty(); stack[stackTop--].~T(); // destructor for T } abcde top

Linked Stack template class linkedStack : public stack { 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 * stackTop; // pointer to stack top int stackSize; // number of elements in stack };

Push() template void linkedStack ::push(const T& theElement) { stackTop = new chainNode (theElement, stackTop); stackSize++; }

Pop() template void linkedStack ::pop() {// Delete top element. if (stackSize == 0) throw stackEmpty(); chainNode * nextNode = stackTop->next; delete stackTop; stackTop = nextNode; stackSize--; }

Implement stack derivedArrayStack.h derivedLinkedStack.h arrayStack.h linkedStack.h

Stack in C++

Applications

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

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

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 2

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13) 15

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19) 21

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

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

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).

Towers Of Hanoi/Brahma ABC 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

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC disk moves

Recursive Solution ABC 1 n > 0 gold disks to be moved from A to C using B move top n-1 disks from A to B using C

Recursive Solution A BC 1 move top disk from A to C

Recursive Solution A BC 1 move top n-1 disks from B to C using A

Recursive Solution A BC 1 moves(n) = 0 when n = 0 moves(n) = 2*moves(n-1) + 1 = 2 n -1 when n > 0

Homework : 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.

Rat In A Maze

Move order is: right, down, left, up Block positions to avoid revisit.

Rat In A Maze Move order is: right, down, left, up Block positions to avoid revisit.

Rat In A Maze Move backward until we reach a square from which a forward move is possible.

Rat In A Maze Move down.

Rat In A Maze Move left.

Rat In A Maze Move down.

Rat In A Maze Move backward until we reach a square from which a forward move is possible.

Rat In A Maze Move backward until we reach a square from which a forward move is possible. Move downward.

Rat In A Maze Move right. Backtrack.

Rat In A Maze Move downward.

Rat In A Maze Move right.

Rat In A Maze Move one down and then right.

Rat In A Maze Move one up and then right.

Rat In A Maze Move down to exit and eat cheese. Path from maze entry to current position operates as a stack.

Homework 2.3 (bonus) Implement the Rat in a maze

Homework 2 Due date: Feb 24