Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Chapter 3 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Stacks.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
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.
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)
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Abstract Data Type (ADT) & Stacks
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
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.
© 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 AND QUEUES 1. Outline 2  Stacks  Queues.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
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,
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Stacks (and Queues).
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks Abstract Data Types (ADTs) Stacks
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

stack

ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

ADS2 Lecture 1011 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

ADS2 Lecture 1012 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

ADS2 Lecture 1013 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

The Stack Example What is the stack formed by carrying out the following sequence of instructions: push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A;

The Stack Example What is the stack formed by carrying out the following sequence of instructions: push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; E

The Stack Example What is the stack formed by carrying out the following sequence of instructions: push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; E N

N The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A;

C The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A;

C The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; H

The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; H C

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C L

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C L L

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C L L

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C A L

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C A L

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C D L

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C D L

push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C A L

ADS2 Lecture 1032 Stack Interface in Java Java interface corresponding to our Stack ADT Requires the definition of class EmptyStackException Different from the built-in Java class java.util.Stack public interface Stack { /** *Return the number of elements in the stack */ public int size(); /** * Return whether the stack is empty */ public boolean isEmpty();

ADS2 Lecture 1033 /** * Inspect the element at the top of the stack */ public E top() throws EmptyStackException; /** * Insert an element at the top of the stack */ public void push (E element); /** * Remove the top element from the stack */ public E pop() throws EmptyStackException; } Stack interface contd.

ADS2 Lecture 1034 Exceptions Attempting the execution of an operation of ADT may sometimes cause an exception Exception is thrown by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException public class EmptyStackException extends RuntimeException { public EmptyStackException(String err){ super(err); } bit of revision coming up..

ADS2 Lecture 1035 Applications of Stacks Direct applications –Page-visited history in a Web browser –Undo sequence in a text editor –Chain of method calls in the Java Virtual Machine Indirect applications –Auxiliary data structure for algorithms –Component of other data structures

ADS2 Lecture 1038 Array-based Stack A simple way of implementing the Stack ADT uses an array We add elements from left to right (top is on the right) A variable tos keeps track of the index of the top of stack element S 012 t …

ArrayStack

Demo ArrayStack stack grows from left to right tos is on right hand side test out underflow test out overflow

ADS2 Lecture 1055 Performance and Limitations Performance –Let m be the size of the array –The space used is O(m) (independent of no. of elements actually in stack) –Each operation runs in time O(1) Limitations –The maximum size of the stack must be defined a priori and cannot be changed –Trying to push a new element into a full stack causes an implementation-specific exception

Example … a stack of characters used for checking parentheses Stack is implemented as an array

ADS2 Lecture 1057

ADS2 Lecture 1058

ADS2 Lecture 1059

ADS2 Lecture 1060

ADS2 Lecture 1061

ADS2 Lecture 1062

Analysis ADS2 Lecture 1063 Running time for methods in realization of stack by an array: MethodTime sizeO(1) isEmptyO(1) topO(1) pushO(1) popO(1) All methods run in constant time. Each consists of a constant number of operations. Pop also calls isEmpty, which is itself constant time.