Stacks 황승원 Fall 2010 CSE, POSTECH.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
List Representation - Chain Fall 2010 CSE, POSTECH.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
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.
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.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks, Queues, and Deques
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.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 3 Introduction to Collections – Stacks Modified
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.
© 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.
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.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
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.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
پشته Stack ساختمان داده ها و الگوريتمها. 2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Stacks
Elementary Data Structures
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Stacks Linear list. One end is called top. Other end is called bottom.
The Class ArrayLinearList
Recap: Solution of Last Lecture Activity
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
List Representation - Array
CSCE 3110 Data Structures & Algorithm Analysis
Stacks and Queues.
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
Stacks, Queues, and Deques
Stacks template<class T> class stack { public:
Stacks.
Stacks, Queues, and Deques
Stacks.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
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:
ساختمان داده ها پشته ها Give qualifications of instructors: DAP
Stacks Abstract Data Types (ADTs) Stacks
Stacks public interface Stack { public boolean empty();
Stacks.
Introduction to Stacks
Stacks template<class T> class stack { public:
Introduction to Stacks
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks 황승원 Fall 2010 CSE, POSTECH

Stack Of Cups bottom top bottom top Remove a cup from new stack. D E F bottom top C A B D E Picture is really a stack of cups and saucers. LIFO = last in first out. The first cup that is removed from a stack of cups is the Last one that was added to the stack. Other examples of LIFO lists in real life: stack of trays in a cafeteria; paper stack in a printer or copy machine; newspaper stack at a news stand. Add a cup to the stack. Remove a cup from new stack. A stack is a LIFO list.

The Interface Stack public interface Stack { public boolean empty(); public void push(Object theObject); public Object pop(); public Object peek(); } Choice of method names is the same as used in Java’s class java.util.Stack. Notice the difference in names of the method to check if an instance is empty—isEmpty for linear lists and empty for a stack.

Derive From A Linear List Class ArrayLinearList Chain

Derive From ArrayLinearList 1 2 3 4 5 6 a b c d e stack top is either left end or right end of linear list empty() => isEmpty() O(1) time peek() => get(0) The decision whether to use the left or right end of the linear list as the stack top is made on the basis of efficiency of resulting stack methods. The complexity of empty() and peek() are independent of the choice.

Derive From ArrayLinearList 1 2 3 4 5 6 a b c d e top=left end push(theObject) => add(0, theObject)  O(size) time pop() => remove(0)

Derive From ArrayLinearList 1 2 3 4 5 6 a b c d e top=right end push(theObject) => add(size(), theObject)  O(1) time pop() => remove(size()-1)

Derive From Chain a b c d e null firstNode stack top is either left end or right end of linear list empty() => isEmpty() O(1) time The complexity of empty() is independent of which end of the chain is used as the top of the stack. The complexity of the remaining methods depends on which end is the stack top.

Derive From Chain (top=left) b c d e null firstNode peek() => get(0)  O(1) time push(theObject) => add(0, theObject) pop() => remove(0)

Derive From Chain (top=right) b c d e null firstNode peek() => get(size()-1)  O(size) time push(theObject) => add(size(), theObject) pop() => remove(size()-1)

Derive From ArrayLinearList package dataStructures; import java.util.*; // has stack exception public class DerivedArrayStack extends ArrayLinearList implements Stack { // constructors come here // Stack interface methods come here }

Constructors /** create a stack with the given initial * capacity */ public DerivedArrayStack(int initialCapacity) {super(initialCapacity);} /** create a stack with initial capacity 10 */ public DerivedArrayStack() {this(10);}

empty() And peek() 1 2 3 4 5 6 a b c d e public boolean empty() 1 2 3 4 5 6 a b c d e public boolean empty() {return isEmpty();} public Object peek() { if (empty()) throw new EmptyStackException(); return get(size() - 1) }

push(theObject) And pop() 1 2 3 4 5 6 a b c d e public void push(Object theElement) {add(size(), theElement);} public Object pop() { if (empty()) throw new EmptyStackException(); return remove(size() - 1); }

Evaluation Merits of deriving from ArrayLinearList 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 extends ArrayLinearList with extends 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.

Demerits All public methods of ArrayLinearList may be performed on a stack. get(0) … get bottom element remove(5) add(3, x) So we do not have a true stack implementation. Must override undesired methods. public Object get(int theIndex) {throw new UnsupportedOperationException();} Change earlier use of get(i) to super.get(i).

Evaluation Code developed from scratch will run faster but will take more time (cost) to develop. Tradeoff between software development cost and performance. Tradeoff between time to market and performance. Could develop easy code first and later refine it to improve performance.

Code From Scratch Use a 1D array stack whose data type is Object. same as using array element in ArrayLinearList Use an int variable top. Stack elements are in stack[0:top]. Top element is in stack[top]. Bottom element is in stack[0]. Stack is empty iff top = -1. Number of elements in stack is top+1.

Code From Scratch package dataStructures; import java.util.EmptyStackException; import utilities.*; // ChangeArrayLength public class ArrayStack implements Stack { // data members int top; // current top of stack Object [] stack; // element array // constructors come here // Stack interface methods come here }

Constructors public ArrayStack(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); stack = new Object [initialCapacity]; top = -1; } public ArrayStack() {this(10);}

push(…) top 1 2 3 4 a b c d e public void push(Object theElement) { 1 2 3 4 a b c d e top public void push(Object theElement) { // increase array size if necessary if (top == stack.length - 1) stack = ChangeArrayLength.changeLength1D (stack, 2 * stack.length); // put theElement at the top of the stack stack[++top] = theElement; }

pop() top 1 2 3 4 a b c d e public Object pop() { if (empty()) 1 2 3 4 a b c d e top public Object pop() { if (empty()) throw new EmptyStackException(); Object topElement = stack[top]; stack[top--] = null; // enable garbage collection return topElement; }

Linked Stack From Scratch See text.

java.util.Stack Derives from java.util.Vector. java.util.Vector is an array implementation of a linear list.

Application

Towers Of Hanoi/Brahma C 4 3 2 Could use animation of towers of hanoi from animations page on Web site. Also known as Towers of Brahma. According to legend, on the day of creation Buddhist monks began the task of moving disks from tower A to tower C. When they get done, the world will come to an end. 1 64 gold disks to be moved from tower A to tower C each tower operates as a stack one at a time cannot place big disk on top of a smaller one!

Towers Of Hanoi/Brahma C 3 2 1 A 3-disk Towers Of Hanoi/Brahma? Rather easy (7 moves) What if there are 64 disks? 1.8 * 10^9 

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

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

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

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

Simple Recursive Implementation Public class Hanoi { public static void tHanoi(int n){ tower=new ArrayStack[4] for (int i=1;i<=3; i++) tower[i]=new ArrayStack(); for (int d=n; d>0; d--) tower[1].push(new Integer(d)); move(n,1,2,3); //12 using 3 as intermediate } public static void move (int n, int x, int y, int z){ move(n-1,x,z,y); // (n-1) xz d=tower[x].pop(); tower[y].push(d); // top xy System.out.println(“move ”+d+” from ”+x+” to ”+y); move(n-1,z,y,x); // (n-1) zy

Towers Of Hanoi/Brahma moves(64) = 1.8 * 1019 (approximately) Performing 109 moves/second, a computer would take about 570 years to complete. At 1 disk move/min, the monks will take about 3.4 * 1013 years. At the rate of a billion moves/second it would take 570 years to complete the task of moving 64 disks. Of course, the Buddhist monks engaged in this activity are moving far fewer disks/second. At the rate of (say) 1 disk a minute (the disks are, after all, rather heavy), the monks will take about 3.4 * 1013 years. So there is quite a while to go before the world comes to an end.

Coming Up Next READING: Ch 9 NEXT: Queue (FIFO) Ch 10