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.

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

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 be instantiated.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
The unorganized person’s data structure
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.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
Chapter 3 Stacks.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Stacks.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
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 Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
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.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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.
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.
SNU IDB Lab. 1 Ch9. Stacks © copyright 2006 SNU IDB Lab.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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
CHP-3 STACKS.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
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.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
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.
1 Chapter 5 Stacks. 2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses.
Programming Application in Civil Engineering
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Stacks
Stacks Stacks.
Stacks 황승원 Fall 2010 CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom.
Stacks.
Stacks Chapter 4.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
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:
Stacks template<class T> class stack { public:
Stacks Chapter 5.
Stacks template<class T> class stack { public:
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Abstract Data Types Stacks CSCI 240
Presentation transcript:

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.

3 3 Stack of Cups Add a cup on the stack. Remove a cup from the stack. A stack is a LIFO (Last-In, First-Out) list. Read Example 8.1

4 4 Observations on Stack & Linear List Stack is a restricted version of linear list – All the stack operations can be performed as linear list operations If we designate the left end of the list as the stack bottom and the right as the stack top – Stack add (push) operation is equivalent to inserting at the right end of a linear list – Stack delete (pop) operation is equivalent to deleting from the right end of a linear list

5 5 Stack ADT AbstractDataType stack { instances linear list of elements; one end is the bottom; the other is the top. operations empty():Return true if stack is empty, return false otherwise; size():Return the number of elements in the stack; top():Return top element of stack; pop():Remove the top element from the stack; push(x):Add element x at the top of the stack; } See the C++ abstract class stack definition in Program 8.1

6 6 Derived Classes and Inheritance Stack can be defined as a special type of linear list Class B may inherit members of class A in one of three basic modes: public, protected and private class B: public A – protected members of A become protected members of B – public members of A become public members of B – cannot access private members of A class B: public A, private C – public and protected members of C become private members of B

7 7 Array-based Representation of Stack Since stack is a restricted version of linear list, we can use an array-based representation of linear list (given in Section 5.3.3) to represent stack Top of stack  element[length-1] Bottom of stack  element[0] The class derivedArrayStack defined in Program 8.2 is a derived class of arrayList (Program 5.3)

8 8 Derived Array-based class Stack template // program 8.2 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);} };

9 9 Base Class arrayStack We learned that a stack class can be defined by extending an arrayList class as in Program 8.2 However, this is not a very efficient implementation A faster implementation is to develop a base class that uses an array stack to hold the stack elements Program 8.4 is a faster implementation of an array stack Read Section 8.3.2

10 Efficiency of Array-based Representation Array-based representation of a stack can waste space when multiple stacks are to coexist An exception is when only two stacks are to coexist How do we implement two stacks in an array? – Fix the bottom of one stack at position 0 – Fix the bottom of the other at position MaxSize-1 – The two stacks grow toward the middle of the array See Figure 8.4

11 Linked Representation of Stack Multiple stacks can be represented efficiently using a chain for each stack Which end of chain should be the stack top? – If we use the right end of the chain as the stack top, then stack operations push and pop are implemented using chain operations insert(n,x) and erase(n-1) where n is the number of nodes in the chain   (n) time – If we use the left end of the chain as the stack top, then insert(0,x) and erase(0)   (1) time What should the answer be? – Left end! See Program 8.5 for linked stack definition

12 Application: Parenthesis Matching Problem: match the left and right parentheses in a character string (a*(b+c)+d) – Left parentheses: position 0 and 3 – Right parentheses: position 7 and 10 – Left at position 0 matches with right at position 10 (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

13 Parenthesis Matching (((a+b)*c+d-e)/(f+g)-(h+j)*(k-1))/(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) How do we implement this using a stack? 1. Scan expression from left to right 2. When a left parenthesis is encountered, add its position to the stack 3. When a right parenthesis is encountered, remove matching position from the stack

14 Example of Parenthesis Matching (((a+b)*c+d-e)/(f+g)-(h+j)*(k-1))/(m-n) stack output 0 1 (2,6) 0 (1,13) (15,19) (21,25) … … – See Program 8.6 – Do the same for (a-b)*(c+d/(e-f))/(g+h)

15 Application: Towers of Hanoi Read the ancient Tower of Brahma ritual (p. 285) n disks to be moved from tower A to tower C with the following restrictions: – Move 1 disk at a time – Cannot place larger disk on top of a smaller one

16 Let’s solve the problem for 3 disks

17 Towers of Hanoi (1, 2)

18 Towers of Hanoi (3, 4)

19 Towers of Hanoi (5, 6)

20 Towers of Hanoi (7) So, how many moves are needed for solving 3- disk Towers of Hanoi problem?  7 7

21 Time complexity for Towers of Hanoi A very elegant solution is to use recursion. See Program 8.7 for Towers of Hanoi recursive function The minimum number of moves required is 2 n -1 Time complexity for Towers of Hanoi is  (2 n ), which is exponential! Since disks are removed from each tower in a LIFO manner, each tower can be represented as a stack See Program 8.8 for Towers of Hanoi using stacks

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

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

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

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

26 64-disk Towers of Hanoi Problem How many moves is required to move 64 disks?  moves(64) = = 1.8 * (approximately) How long would it take to move 64 gold disks by the Brahma priests?  At 1 disk move/min, the priests will take about 3.4 * years.  “ According to legend, the world will come to an end when the priests have competed their task ” How long would it take to move 64 disks by Pentium 5?  Performing 10 9 moves/second, a Pentium 5 would take about 570 years to complete. - try running hanoiRecursive.cpp on your computer using n=3, 4, 10, 20, 30 and find out how long it takes to run

27 Application: Rearranging Railroad Cars Read the problem on p. 289 Rearrange the cars at a shunting yard that has an input track, and output track, and k holding tracks between the input and output tracks See Figure 8.6 for a three-track example See Figure 8.7 for track states See Program 8.9, 8.10 and 8.11

28 READING Read Switch Box Routing Problem Read Offline Equivalence Problem Read Rat in a Maze Read all of Chapter 8