Stack and Its Implementation

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

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

CSC 273 Data Structures

Stack Abstract Data Type

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

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

STACKS AND QUEUES. Problem Solving with Computers-II

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

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

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

Stacks and their Applications

Stacks. Revised based on textbook author s notes.

Top of the Stack. Stack ADT

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

Lecture 12 ADTs and Stacks

-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

1. Stack Implementation Using 1D Array

Top of the Stack. Stack ADT

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.

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

March 13/2003 Jayakanth Srinivasan,

Stacks Fall 2018 Margaret Reid-Miller

Linear Data Structure

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

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

Lecture Data Structure Stack

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

Advanced Java Concepts Unit 3: Stacks and Queues

Lists are great, but. Stacks 2

Stacks, Queues (cont d)

The Stack and Queue Types

[CS302-Data Structures] Homework 2: Stacks

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

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

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

Lecture No.04. Data Structures

Data Abstraction and Abstract Data Types

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

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

ADT Implementation Array

CMPS 390 Data Structures

CSC 222: Computer Programming II. Spring 2005

infix expressions (review)

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

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

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

Data Structures Week #3. Stacks

Infix to Postfix Conversion

CS 206 Introduction to Computer Science II

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

12 Abstract Data Types

Data Structures and Algorithms

Stacks Calculator Application. Alexandra Stefan

PA3 Design Specification

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

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

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

CS 211 Programming Practicum Spring 2018

BBM 201 DATA STRUCTURES

CS350: Data Structures Stacks

CS 211 Programming Practicum Spring 2017

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

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

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

CSIS 10B Lab 2 Bags and Stacks

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

What can we do with Coin dispenser?

Project 1: Implementation of the Stack ADT and Its Application

Queues Fall 2018 Margaret Reid-Miller

Stacks II. Adventures in Notation. stacks2 1

An Introduction to Trees

CS 211 Programming Practicum Fall 2018

CS 206 Introduction to Computer Science II

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

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

Operators. Java operators are classified into three categories:

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Monday, November 13, 2017

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

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

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

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

LECTURE 17. Expressions and Assignment

BBM 201 DATA STRUCTURES

Lecture Chapter 6 Recursion as a Problem Solving Technique

Postfix (and prefix) notation

CSC148 Week 2. Larry Zhang

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


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

CMPT 225. Stacks-part2

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Assignment 5. Introduction

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

Review: Expressions, Variables, Loops, and more.

Transcription:

Stack and Its Implementation Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1

Definition of Stack Usage of Stack Outline Checking for Balanced Algebraic Expression Infix to Postfix Conversion The Program Stack Array Based Implementation Linked List Based Implementation Vector Based Implementation 2

Stack Stack is an ADT that follow last-in, first-out( LIFO) behavior All additions added to one end of stack Added to top Called a push operation All removes to stack restricted to one end of stack Remove only top entry Called a pop operation 3

Stack 4

Data Stack ADT A collection of objects having the same data type Operations +push(newentry: T): void +pop(): T +peek(): T +isempty(): boolean +clear(): void 5

StackInterface public interface StackInterface<T> { public void push(t newentry); public T pop(); public T peek(); public boolean isempty(); public void clear(); } // end 6

Stackinterface Example: 7

StackInterface 8

Exercise 9

Using a Stack to Process Algebraic Expressions Algebraic expressions composed of Operands (variables, constants) Operators (+, -, /, *, ^) Operators can be unary or binary Different precedence notations Infix a + b Prefix + a b Postfix a b + 10

Using a Stack to Process Algebraic Expressions Precedence must be maintained Order of operators Use of parentheses (must be balanced) Use stacks to evaluate parentheses usage Scan expression Push symbols Pop symbols 11

Using a Stack to A balanced expression contains delimiters that are paired correctly Example a {b [c (d + e)/2 - f ] + 1} - balanced a {b [c (d + e)/2 - f ] + 1} } not balanced Algorithm 1. Read the expression from left to right, ignoring other characters 2. When an opening delimeter is found store it 3. When a closing delimeter found, read the last saved opening delimeter 4. If they are the same goto 1 12

Using a Stack to Example a {b [c (d + e)/2 - f ] + 1} Lets use stack to store the delimiters 13

Example Using a Stack to {a* [b + c *(c/d ]-6 )+e } 14

Using a Stack to Example [ ( ) ] } 15

16

Infix to Postfix Conversion Manual algorithm for converting infix to postfix (a + b) * c Write with parentheses to force correct operator precedence ((a + b) * c) Move operator to right inside parentheses ((a b + ) c * ) Remove parentheses a b + c * 17

Infix to Postfix Algorithm basics Scan expression left to right When operand found, place at end of new expression When operator found, save to determine new position 18

Infix to Postfix Example a + b*c 19

Infix to Postfix Example a- b + c 20

Infix to Postfix Conversion 1. Operand Append to end of output expression 2. Operator ^ Push ^ onto stack 3. Operators +, -, *, / Pop from stack, append to output expression Until stack empty or top operator has lower precedence than new operator Then push new operator onto stack 21

Infix to Postfix Conversion 4. Open parenthesis Push ( onto stack 5. Close parenthesis Pop operators from stack and append to output Until open parenthesis is popped. Discard both parentheses 22

Example a / b * (c + (d e ) ) 23

Infix to Postfix General rule : 24

Infix to Postfix Exercise (a b * c) / (d * e ^ f * g + h) 25

Evaluating Postfix Expression Requires no rules of precedence The order of its operators and operands dictates the order of operations Contains no parenthesis Algorithm Scan the postfix expression Save operands till operator is found Apply the operator on the operands The last saved operand is the second operand Save the result Continue till the expression is empty 26

Example a b / where a = 2, b= 4 27

Evaluating Postfix Expressions 28

Example a b + c / where a= 2, b = 4 and c = 3 29

Exercise e b c a ^ * + d - where a = 2, b = 3, c = 4, d = 5, e = 6 30

The Program Stack When a program executes, a special location called the program counter(pc) references the current instruction. When a method is called, the program s run-time environment creates an object called an activation record, or frame, for the method Contains method s arguments, local variables, and a reference to the current instruction (PC) At the time the method is called, the activation record is pushed onto a stack called the program stack The program stack often contains more than one activation record. The record at the top of the stack belongs to the method that is currently executing 31

The Program Stack FIGURE 5-13 The program stack at three points in time: (a) when main begins execution; (PC is the program counter) 32

The Program Stack FIGURE 5-13 The program stack at three points in time: (b) when methoda begins execution; (PC is the program counter) Copyright 2012 by Pearson Education, Inc. All rights reserved 33

The Program Stack FIGURE 5-13 The program stack at three points in time: (c) when methodb begins execution; (PC is the program counter) Copyright 2012 by Pearson Education, Inc. All rights reserved 34

Java Class Library : Stack Under java.util package Methods public T push(t item); public T pop(); public T peek(); public boolean empty(); 35

Linked Implementation Consider push, pop, peek Each involves top of stack Best to put top of stack at head node Fastest, easiest to access 36

37

push Method 38

push Method 39

peek Method 40

pop Method 41

pop Method 42

Other Methods 43

Array Based Implementation Again the question: Where to place the top entry? 44

Array Based Implementation 45

Array Based Implementation More efficient operations with bottom of stack at beginning of array Top of stack at last occupied entry Must consider memory wastage of unused array elements 46

47

push Method 48

peek Method Copyright 2012 by Pearson Education, Inc. All rights reserved 49

pop Method 50

pop Method 51

pop Method 52

Other Methods public clear() { } for(int i=0;i<topindex;i++) stack[i]=null; topindex=-1; 53

Vector-Based Implementation Vector is a structure which behaves like a high level array Has methods to access entries Grows in size as needed (hidden from client) 54

Constructors and Methods of Vector public Vector() public Vector(int initialcapacity) public boolean add(t newentry) public T remove(int index) public void clear() public T lastelement() public boolean isempty() public int size() 55

56

Stack Methods for Vector Implementation Copyright 2012 by Pearson Education, Inc. All rights reserved 57

pop Method 58

peek Method 59

Other Methods 60

Exercise Write a program that accepts a string from the user and displays the reverse of the string. Use Stack class form Java s collection framework 61