Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Similar documents
Standard ADTs. Lecture 19 CS2110 Summer 2009

Stack and Its Implementation

STANDARD ADTS Lecture 17 CS2110 Spring 2013

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

CMPS 390 Data Structures

1. Stack Implementation Using 1D Array

Chapter 2. Stack & Queues. M hiwa ahmad aziz

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Lecture No.04. Data Structures

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks Fall 2018 Margaret Reid-Miller

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Lecture Data Structure Stack

CS350: Data Structures Stacks

Lecture 12 ADTs and Stacks

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.

Stack Abstract Data Type

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

An Introduction to Trees

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Stacks, Queues (cont d)

CSC 273 Data Structures

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Linear Data Structure

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Lists are great, but. Stacks 2

DEEPIKA KAMBOJ UNIT 2. What is Stack?

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks

Stacks. Revised based on textbook author s notes.

infix expressions (review)

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions

What can we do with Coin dispenser?

Top of the Stack. Stack ADT

Formal Languages and Automata Theory, SS Project (due Week 14)

STACKS AND QUEUES. Problem Solving with Computers-II

Data Structures And Algorithms

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Abstract Data Types. Abstract Data Types

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Lab 7 1 Due Thu., 6 Apr. 2017

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

CS 211. Project 5 Infix Expression Evaluation

CMSC 132: Object-Oriented Programming II. Stack and Queue

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

CS W3134: Data Structures in Java

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

CSCD 326 Data Structures I Stacks

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

ITEC2620 Introduction to Data Structures

Data Structures Week #3. Stacks

Top of the Stack. Stack ADT

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Basic Data Structures

CIS Fall Data Structures Midterm exam 10/16/2012

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

CS 211 Programming Practicum Spring 2017

Stacks and their Applications

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Basic Data Structures 1 / 24

Queues Fall 2018 Margaret Reid-Miller

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Interfaces & Generics

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

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.

Data Structures G5029

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Stacks. Common data structures - useful for organizing data for specific tasks Lists Stacks - an Abstract Data Type

Fun facts about recursion

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Data Structure (CS301)

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Linked Structures. See Section 3.2 of the text.

CS 211 Programming Practicum Spring 2018

Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Harald Gall, Prof. Dr.

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

CMPT 225. Stacks-part2

Transcription:

STACK

Stack ADT 2 A stack is an abstract data type based on the list data model All operations are performed at one end of the list called the top of the stack (TOS) LIFO (for last-in first-out) list is a synonym for stack Common operations:! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Java Stack Interface 3 public interface Stack { public void push(object x); public Object pop(); public Object peek(); public boolean isempty(); public void clear(); }

From interface to implementation 4 Given that we want to support some interface, the designer still faces a choice! What will be the best way to implement this interface for my expected type of use?! Choice of implementation can reflect many considerations Major factors we think about! Speed for typical use case! Storage space required

Array implementation of Stack 5 class ArrayStack implements Stack { max-1 } private Object[] array; //Array that holds the Stack private int index = 0; //First empty slot in Stack public ArrayStack(int maxsize) { array = new Object[maxSize]; } public void push(object x) { array[index++] = x; } public Object pop() { return array[--index]; } public Object peek() { return array[index-1]; } public boolean isempty() { return index == 0; } public void clear() { index = 0; } Question: What can go wrong? index 4 3 2 1 0 O(1) worstcase time for each operation. What if maxsize is too small?

Linked List Implementation of Stack 6 class ListStack implements Stack { private Node head = null; //Head of list that //holds the Stack public void push(object x) { head = new Node(x, head); } public Object pop() { Node temp = head; head = head.next; return temp.data; } public Object peek() { return head.data; } public boolean isempty() { return head == null; } public void clear() { head = null; } } O(1) worst-case time for each operation (but constant is larger) Note that array implementation can overflow, but the linked list version cannot head

Use of Stack 7 Infix to postfix conversion Postfix evaluation Function calls etc

Infix to postfix 8 When an operand is read, output it When ( is read, push it When an operator is read:! If TOS item has higher or equal precedence, repeatedly pop until TOS has an element of lower precedence or stack empty, then push the operator! If the stack is empty or TOS item has lower precedence, just push it When ) is found, pop until we find the matching ( ( has the lowest precedence when in the stack but has the highest precedence when in the input When we reach the end of input, pop until the stack is empty

9 Stack: Output:

10 Stack: ( Output:

11 Stack: ( Output: 8

12 Stack: ( + Output: 8

13 Stack: ( + Output: 8 9

14 Stack: ( + * Output: 8 9

15 Stack: ( + * ( Output: 8 9

16 Stack: ( + * ( Output: 8 9 4

17 Stack: ( + * ( + Output: 8 9 4

18 Stack: ( + * ( + Output: 8 9 4 5

19 Stack: ( + * ( + * Output: 8 9 4 5

20 Stack: ( + * ( + * Output: 8 9 4 5 7

21 Stack: ( + * ( + Output: 8 9 4 5 7 *

22 Stack: ( + * ( Output: 8 9 4 5 7 * +

23 Stack: ( + * ( + Output: 8 9 4 5 7 * +

24 Stack: ( + * ( + Output: 8 9 4 5 7 * + 6

25 Stack: ( + * ( Output: 8 9 4 5 7 * + 6 +

26 Stack: ( + * Output: 8 9 4 5 7 * + 6 +

27 Stack: ( + Output: 8 9 4 5 7 * + 6 + *

28 Stack: ( Output: 8 9 4 5 7 * + 6 + * +

29 Stack: Output: 8 9 4 5 7 * + 6 + * +

30 Stack: + Output: 8 9 4 5 7 * + 6 + * +

31 Stack: + Output: 8 9 4 5 7 * + 6 + * + 3

32 Stack: + * Output: 8 9 4 5 7 * + 6 + * + 3

33 Stack: + * Output: 8 9 4 5 7 * + 6 + * + 3 5

34 Stack: + Output: 8 9 4 5 7 * + 6 + * + 3 5 *

35 Stack: Output: 8 9 4 5 7 * + 6 + * + 3 5 * +

36 Stack: + Output: 8 9 4 5 7 * + 6 + * + 3 5 * +

37 Stack: + Output: 8 9 4 5 7 * + 6 + * + 3 5 * + 4

38 Stack: Output: 8 9 4 5 7 * + 6 + * + 3 5 * + 4 +

More Examples 39 Refer to the slides by Otávio Braga linked from the class website

Postfix evaluation 40 Starting with an empty stack, we scan the postfix expression from left to right Each time we encounter an argument, we push it onto the stack When we encounter an operator, we pop the stack twice, remembering the operands popped We then apply the operator to the two popped values (with the second as the left operand) and push the result onto the stack

41 Evaluating 3 4 + 2 *

Postfix to Expression Tree 42 A 2 * 2 A * B * - B 2 * + A B - / 1. Push A 2. Push 2 3. Pop 2 4. Pop A 5. Create tree_1 6. Push tree_1 7. Push 2 8. Push A 9. Pop A 10. Pop 2 11. Create tree_2 12. Push tree_2 13. Push B 14. Pop B 15. Pop tree_2 16. Create tree_3 17. Push tree_3 18. Pop tree_3 19. Pop tree_1 20. Create tree_4 21. Push tree_4 22. Push B 23. Push 2 24. Pop 2 25. Pop B 26. Create tree_5 27. Push tree_5 28. Pop tree_5 29. Pop tree_4 30. Create tree_6 31. Push tree_6 32. Push A 33. Push B 34. Pop B 35. Pop A 36. Create tree_7 37. Push tree_7 38. Pop tree_7 39. Pop tree_6 40. Create tree_8 41. Push tree_8 tree_1 tree_4 tree_2 tree_6 tree_3 tree_8 tree_5 tree_7

Evaluating expression tree 43 If at leaf node (operand node) return the operand s value Otherwise: Evaluate the left sub-tree Evaluate the right sub-tree Combine the results from both sub-trees with the operator at root public static int eval(treecell root) { int left_result, right_result; if (root.datum is operand) return operand s value; left_result = eval(root.left); right_result = eval(root.right); switch (root.datum) { case + : return left_result + right_result; case * : return left_result * right_result; : : } }

Function calls 44 An important application of stacks is normally hidden from view! A stack is used to allocate space in the computer s memory to the variables belonging to the various functions of a program Example: a recursive factorial function! Has a parameter n and a return value! As fact calls itself recursively, different calls with different parameter are active at the same time! A frame for each call is created and place on TOS! When a given call return, its frame gets popped off

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; }

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; } public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return fact; }

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; } public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return fact; } public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }

public static int factorial(int 3) { int fact; if (n > 1) fact = 2 * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }

public static int factorial(int 3) { int fact; if (n > 1) fact = 2 * 3; else fact = 1; return 6; }