Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stack? Menyimpan data dalam susunan tertentu Prinsip Stak: Last In First Out = LIFO atau: First In Last Out = FILO Data yang masuk terkahirlah yang dikeluarkan.
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.
Stacks, Queues, and Deques
Data Structures Lecture 5 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
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.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks and QueuesCSC311: Data Structures1 Chapter 5 Stacks and Queues Objectives –Stacks and implementations –Queues and implementations –Double-ended.
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)
Implementing and Using Stacks
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
CS2468: Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone: TA (Assignment.
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.
Stacks & Queues EECS: Stacks & Queues Stacks April 23, 2017
© 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.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Stacks by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
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.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
1 COMP9024: Data Structures and Algorithms Week Four: Stacks and Queues Hui Wu Session 1, 2016
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,
Stacks 1/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks (and Queues).
Stacks 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Stacks 9/12/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks © 2013 Goodrich, Tamassia, Goldwasser Stacks.
Stacks.
Stacks.
Chapter 5 Stacks and Queues 11/28/2018 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.
Copyright © Aiman Hanna All rights reserved
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues DSA 2013 Stacks n Queues.
Stacks.
CS2468: Data Structures and Data Management
Stacks.
Introduction to Stacks
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Stacks What is stack?  An abstract data type storing arbitrary objects.  An ordered list in which all insertions and deletions are made at one end, called top Properties  Last‐In‐First‐Out (LIFO) scheme  Typically implemented with array or linked list Stack class in Java  2

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Stack Operations Main operations:  push(Object) : inserts an element at the top  Object pop() : removes an element at the top and returns it Others:  Object top() : returns the element at the top without removing it  int size() : returns the number of elements stored  boolean isEmpty() : indicates whether no elements are stored 3

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Stack Interface in Java Restrictions  The execution of pop() and top() on an empty stack throws an EmptyStackException().  The execution of push() on a full stack throws an FullStackException(). Java: interface  A group of related methods with empty bodies  To implement this interface, the name of your class would change and keyword implements is used in the class declaration. 4 public interface Stack { public int size(); public boolean isEmpty(); public Object top() throws EmptyStackException(); public void push(Object element) throws FullStackException(); public Object pop() throws EmptyStackException(); }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Stack Implementation in Java 5 public class BoundedStack implements Stack { private Object[] array; private int size = 0; public BoundedStack(int capacity) { array = new Object[capacity]; } public boolean isEmpty() { return (size == 0); } public int size() { return size; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Stack Implementation in Java 6 public void push(Object item) { if (size == array.length) throw new FullStackException(); array[size++] = item; } public Object pop() { if (size == 0) throw new EmptyStackException(); Object result = array[size‐1]; array[‐‐size] = null; return result; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Common Applications of Stack Page‐visited history in a web browser Undo sequence in a text editor Parentheses matching Evaluating arithmetic expressions 7

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Method Stack in the JVM The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack. When a method is called, the JVM pushes on the stack a frame containing  Local variables and return value  Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack. 8

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Method Stack 9 public void main(String args[]) { int i = 5; foo(i); } void foo(int j) { int k; k = j+1; bar(k); } void bar(int m) { … } main PC = 2 i = 5 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 main PC = 2 i = 5 Invoke foo() Invoke bar() Return to foo() Return to main() Stack

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Array‐based Stack A simple way of implementing the Stack ADT uses an array. Top points to the last element in stack.  We add elements from left to right.  A variable keeps track of the index of the top element. 10 Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else t  t  1 return S[t + 1] S 012 t … top

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Array‐based Stack The array storing the stack elements may become full. A push operation will then throw a FullStackException().  Limitation of the array‐based implementation  Not intrinsic to the Stack ADT 11 Algorithm push(o) if t = S.length  1 then throw FullStackException else t  t + 1 S[t]  o S 012 t …

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Array‐based Stack 12

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Application of Stack Parentheses Matching  Each “(”, “{”, and “[” must be paired with “)”, “}”, and “]”, respectively. correct: ( )(( )){([( )])} correct: ((( )(( )){([( )])})) incorrect: )(( )){([( )])} incorrect: ({[ ])} incorrect: (  ParenMatch Input: (2+3)*(3+6) X: an array of tokens, e.g., {(,2,+,3,),*,(,3,+,6,)} n = 11 Output: true 13

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 ParenMatch Algorithm 14 Algorithm ParenMatch(X,n): Input: An array X of n tokens Output: true if and only if all the grouping symbols in X match Let S be an empty stack for i=0 to n‐1 do if X[i] is an opening grouping symbol then // e.g., ( S.push(X[i]) else if X[i] is a closing grouping symbol then // e.g., ) if S.isEmpty() then return false // nothing to match with if S.pop() does not match the type of X[i] then return false // wrong match if S.isEmpty() then return true // Every symbol was matched. else return false // Some symbols were never matched.

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Application of Stack Evaluating arithmetic expressions  Example: 14 – 3 * = (14 – (3 * 2) ) + 7  Operator precedence * and / have higher precedence over + and –.  Associativity Operators of the same precedence group Evaluated from left to right Example: x – y + z: (x – y) + z rather than x – (y + z) 15

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Application of Stack Evaluating arithmetic expressions  Idea: push each operator on the stack, but first pop and perform higher and equal precedence operations.  Two stacks: Operator stack (opStk) holds operators. Value stack (valStk) holds values.  To clean up the stack at the end, we use $ as special “end of input” token with lowest precedence. 16

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Application of Stack Evaluating arithmetic expressions 17 Algorithm EvalExp() Input: a stream of tokens representing an arithmetic expression (with numbers) Output: the value of the expression while there is another token z if isNumber(z) then valStk.push(z) // push number else repeatOps(z); // calculate the higher precedence operators opStk.push(z); // before push the operator z repeatOps($); // calculate all the rest operators return valStk.top()

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Application of Stack Evaluating Arithmetic Expressions  Auxiliary functions 18 Algorithm repeatOps( refOp ): while ( valStk.size() > 1 & prec(refOp) ≤ prec(opStk.top()) doOp() Algorithm doOp() x  valStk.pop(); y  valStk.pop(); op  opStk.pop(); valStk.push( y op x )

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example ≤ 4 – 3 * Operator ≤ has lower p recedence than +/–. – ≤14 4 *3 – ≤ 4 2 *3 – ≤ *3 – ≤ – ≤ 4+ ≤ -2 $ 7 + ≤14 -2 $ F $ ≤14 5

20