Stack Abstract Data Type

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

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

Lecture 12 ADTs and Stacks

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

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

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Stack and Its Implementation

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

Stacks. Revised based on textbook author s notes.

STACKS AND QUEUES. Problem Solving with Computers-II

Stack Abstract Data Type. Section 3.1

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

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

C27a: Stack and Queue

CSE 214 Computer Science II Stack

Stacks Fall 2018 Margaret Reid-Miller

CS 206 Introduction to Computer Science II

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

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Stacks and their Applications

Postfix (and prefix) notation

PA3 Design Specification

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

March 13/2003 Jayakanth Srinivasan,

CS 211 Programming Practicum Spring 2018

CS 211 Programming Practicum Spring 2017

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

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

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

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

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.

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

1. Stack Implementation Using 1D Array

CS 206 Introduction to Computer Science II

[CS302-Data Structures] Homework 2: Stacks

Lab 7 1 Due Thu., 6 Apr. 2017

The Stack and Queue Types

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

CS 211 Programming Practicum Fall 2018

Top of the Stack. Stack ADT

infix expressions (review)

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

BBM 201 DATA STRUCTURES

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lists are great, but. Stacks 2

CSC 222: Computer Programming II. Spring 2005

Project 1: Implementation of the Stack ADT and Its Application

Top of the Stack. Stack ADT

Algorithms and Data Structures

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

Stacks Calculator Application. Alexandra Stefan

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

Lecture 4 Stack and Queue

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

CS 211. Project 5 Infix Expression Evaluation

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Chapter 04: Instruction Sets and the Processor organizations. Lesson 18: Stack-based processor Organisation

CSE 230 Intermediate Programming in C and C++

Stacks, Queues (cont d)

Lecture Data Structure Stack

BBM 201 DATA STRUCTURES

Data Structures Week #3. Stacks

CSC 273 Data Structures

Application of Stack (Backtracking)

This is an individual assignment and carries 100% of the final CPS 1000 grade.

Content: Learning Objectives

Stacks II. Adventures in Notation. stacks2 1

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

CS W3134: Data Structures in Java

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CS350: Data Structures Stacks

12 Abstract Data Types

Prefix/Infix/Postfix Notation

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

CS2113 Lab: Collections 10/29/2018

A Simple Syntax-Directed Translator

Advanced Java Concepts Unit 3: Stacks and Queues

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Data Structures and Algorithms

DATA STRUCTURES AND ALGORITHMS

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

Queues Fall 2018 Margaret Reid-Miller

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

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Types of Data Structures

CHAPTER 8: Central Processing Unit (CPU)

Stacks, Queues and Hierarchical Collections

Linear Data Structure

Infix to Postfix Conversion

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

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

Transcription:

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 how Java implements a stack To learn how to implement a stack using an underlying array or a linked list To see how to use a stack to perform various applications, including finding palindromes, testing for balanced (properly nested) parentheses, and evaluating arithmetic expressions

Stack Abstract Data Type A stack can be compared to a Pez dispenser Only the top item can be accessed Can only extract one item at a time A stack is a data structure with the property that only the top element of the stack is accessible The stack s storage policy is Last-In, First-Out

Specification of the Stack Abstract Data Type Only the top element of a stack is visible, therefore the number of operations performed by a stack are few Need the ability to Inspect the top element Retrieve the top element Push a new element on the stack Test for an empty stack

Specification of the Stack Abstract Data Type (continued)

Stack Applications Two client programs using stacks Palindrome finder Parentheses matcher Palindrome: string that reads the same in either direction Example: Able was I ere I saw Elba

Stack Applications (continued)

Stack Applications (continued) When analyzing arithmetic expressions, it is important to determine whether an expression is balanced with respect to parentheses (a+b*(c/(d-e)))+(d/e) Problem is further complicated if braces or brackets are used in conjunction with parenthesis Solution is to use stacks!

Stack Applications (continued)

Stack Applications (continued)

Implementing a Stack as an Extension of Vector The Java API includes a Stack class as part of the package java.util The vector class implements a growable array of objects Elements of a vector can be accessed using an integer index and the size can grow or shrink as needed to accommodate the adding and removing of elements

Implementing a Stack as an Extension to Vector (continued)

Implementing a Stack with a List Component Can use either the ArrayList, Vector, or the LinkedList classes as all implement the List interface Name of class illustrated in text is ListStack<E> ListStack is an adapter class as it adapts the methods available in another class to the interface its clients expect by giving different names to essentially the same operations

Implementing a Stack Using an Array Need to allocate storage for an array with an initial default capacity when creating a new stack object Need to keep track of the top of the stack No size method

Implementing a Stack Using an Array (continued)

Implementing a Stack as a Linked Data Structure We can implement a stack using a linked list of nodes

Comparison of Stack Implementations Extending a Vector (as is done by Java) is a poor choice for stack implementation as all Vector methods are accessible Easiest implementation would be to use an ArrayList component for storing data All insertions and deletions are constant time regardless of the type of implementation discussed All insertions and deletions occur at one end

Additional Stack Applications Consider two case studies that relate to evaluating arithmetic expressions Postfix and infix notation Expressions normally written in infix form Binary operators inserted between their operands A computer normally scans an expression string in the order that it is input; easier to evaluate an expression in postfix form

Additional Stack Applications (continued)

Additional Stack Applications (continued) Advantage of postfix form is that there is no need to group subexpressions in parentheses No need to consider operator precedence

Evaluating Postfix Expressions

Evaluating Postfix Expressions (continued)

Evaluating Postfix Expressions (continued)

Evaluating Postfix Expressions (continued)

Converting from Infix to Postfix

Additional Stack Applications (continued)

Evaluating Postfix Expressions (continued)

Evaluating Postfix Expressions (continued)

Chapter Review A stack is a last-in, first-out (LIFO) data structure A stack is a simple but powerful data structure; its four operations include empty, peek, pop, and push Stacks are useful to process information in the reverse of the order that it is encountered Java.util.Stack is implemented as an extension of the Vector class

Chapter Review (continued) Three ways to implement a stack: Using an object of a class that implements the List interface as a container Using an array as a container Using a linked list as a container Stacks can be applied in programs for evaluating arithmetic expressions