Stacks.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
© 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?
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
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,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
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)
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
© 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.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
© 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 (and Queues).
Unit 4 Stacks And Queues King Fahd University of Petroleum & Minerals
Stacks Chapter 5.
Stacks Stacks.
Revised based on textbook author’s notes.
CSCI 3333 Data Structures Stacks.
Recap: Solution of Last Lecture Activity
MEMORY REPRESENTATION OF STACKS
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.
CSE 373: Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Algorithms and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stack Implementations
Stacks.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
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.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Introduction to Stacks
Stack.
Introduction to Stacks
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

Stacks

Stack: Last In First Out (LIFO) Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions etc.•

Specifications of a Stack Organizes entries according to order added All additions added to one end of stack Added to “top” Called a “push” Access to stack restricted Access only top entry Remove called a “pop” Adapted from Pearson Education, Inc.

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

Adapted from Pearson Education, Inc. Stack operations Consider peek Consider push Another push Consider pop Farah Manal Samia Samia Asma Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Stack operations The TOP Samia Consider peek Consider push Another push Consider pop Which end of the stack is involved? Farah Manal Samia Asma Adapted from Pearson Education, Inc.

Linked Implementation The TOP Samia Which is better? Why? Farah Manal Samia Asma FirstNode Manal Samia Asma FirstNode Manal Samia Asma Adapted from Pearson Education, Inc.

The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme 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

Adapted from Pearson Education, Inc. ADT Stack Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. ADT Stack Adapted from Pearson Education, Inc.

Stack Interface in Java public interface Stack { public int size(); public boolean isEmpty(); public Object top() throws EmptyStackException; public void push(Object o); public Object pop() throws EmptyStackException; } Java interface corresponding to our Stack ADT Requires the definition of class EmptyStackException Different from the built-in Java class java.util.Stack

import java.io.*; import java.util.*; public class stack { public static void main(String[] args) { Stack<Integer> myStack = new Stack<Integer>(); myStack.push(1); myStack.push(2); myStack.push(3); myStack.push(4); myStack.push(5); System.out.println( "This is the stack : " +myStack); myStack.pop(); System.out.println("This is the stack after pop : " +myStack); System.out.println("This is first number in the stack : " +myStack.peek()); System.out.println("This is the stack now : " +myStack); }

Output: This is the stack : [1, 2, 3, 3, 4, 5] This is the stack after pop : [1, 2, 3, 3, 4] This is first number in the stack : 4 This is the stack now : [1, 2, 3, 3, 4]

Adapted from Pearson Education, Inc. Stack Usage Example Adapted from Pearson Education, Inc.

Algebraic Expressions Algebraic expressions are composed of Operands (variables, constants) Operators (+, -, /, *, ^) Operators can be unary or binary Precedence must be maintained According to order of operators According to parentheses When parentheses are used, they must be balanced Different notations Infix a + b Prefix + a b Postfix a b + Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (1) Figure 5-7 Converting a + b * c to postfix form Next Char Postfix Operator Stack (bottom to top) a (empty) + b a b * + * c a b c (end of infix) a b c * a b c * + Adapted from Pearson Education, Inc.

Evaluating Infix Expressions evaluation of a + b * c when a is 2, b is 3, and c is 4 after reaching the end of the expression while performing the multiplication; while performing the addition Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) The only thing that can pop an open parenthesis is a matching closing parenthesis Next Char Postfix Operator Stack (bottom to top) 7 (empty) + 2 72 ^ +^ ( +^( 1 721 +^(+ 3 7213 Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) Next Char Postfix Operator Stack (bottom to top) 3 7213 +^(+ ) 7213+ +^ / 7213+^ +/ ( +/( 6 7213+^6 + +/(+ 5 7213+^65 * +/(+* Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) Are we done? We are only done when we reach the end of the infix expression AND the stack is empty. Next Char Postfix Operator Stack (bottom to top) * 7213+^65 +/(+* 3 7213+^653 - 7213+^653* +/(+ 7213+^653*+ +/(- 5 7213+^653*+5 ) 7213+^653*+5- +/ (end of infix) 7213+^653*+5-/ + 7213+^653*+5-/+ (empty) Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) 7 + 2 ^ 4 / (6 + 5 * 3 – 5) = 7 + 16 / (6 + 15 – 5) = 7 + 16 / (21 – 5) = 7 + 16 / 16 = 7 + 1 = 8 Next Char Postfix Operator Stack (bottom to top) * 7213+^65 +/(+* 3 7213+^653 - 7213+^653* +/(+ 7213+^653*+ +/(- 5 7213+^653*+5 ) 7213+^653*+5- +/ (end of infix) 7213+^653*+5-/ + 7213+^653*+5-/+ (empty) Adapted from Pearson Education, Inc.

Postfix Evaluation - Algorithm Any operator operates on its two previous operands Example - manual 7 2 1 3 + ^ 6 5 3 * + 5 - / + 7 2 4 ^ 6 5 3 * + 5 - / + 7 16 6 15 + 5 - / + 7 16 21 5 - / + 7 16 16 / + 7 1 + 8 Algorithm basics Scan postfix expression left to right When operand found, push it onto operand stack When operator found: Pop stack for 2nd operand Pop stack for 1st operand Evaluate Push result onto operand stack Adapted from Pearson Education, Inc.

Evaluating Postfix Expression – Example (1) 7 2 1 3 + ^ 6 5 3 * + 5 - / + 1 + 3 2 ^ 4 7 2 7 1 2 7 3 1 2 7 427 167 6 167 56167 5 * 3 6 + 15 21 - 5 16 / 16 7 + 1 356167 156167 21167 5 21 167 16167 17 8 Adapted from Pearson Education, Inc.

Linked Implementation null; return { top = topNode.data; topNode = topNode.next; } null; Adapted from Pearson Education, Inc.

Linked Implementation null; { T top = topNode.data; topNode = topNode.next; return top; } return null; } { top = topNode.data; topNode = topNode.next; } Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Adding to the Top Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Peek and Pop Adapted from Pearson Education, Inc.

Performance and Limitations Let n be the number of elements in the stack The space used is O(n) 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

Stack Array-based Stack http://www.cs.usfca.edu/~galles/visualization/StackArray.html Link stack http://www.cs.usfca.edu/~galles/visualization/StackLL.html