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

Similar documents
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

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

DATA STRUCTURE UNIT I

Data Structures Week #3. Stacks

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Stacks Calculator Application. Alexandra Stefan

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

Algorithms and Data Structures

Stacks. Revised based on textbook author s notes.

March 13/2003 Jayakanth Srinivasan,

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

Bharati Vidyapeeth s College Of Engineering for Women Pune-43 Department E & TC. SE- Unit Test I Subject-DS

Stacks and Queues. CSE Data Structures April 12, 2002

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

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

Infix to Postfix Conversion

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues

Lecture No.04. Data Structures

An Introduction to Trees

Linear Data Structure

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

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

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

CMPT 225. Stacks-part2

STACKS AND QUEUES. Problem Solving with Computers-II

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

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

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

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

Stacks and their Applications

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

Lecture Data Structure Stack

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

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

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

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

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.

Stacks. Chapter 3. Exercises. 1. See Figure 3-1. FIGURE 3-1 Solution to Exercise 1

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

Top of the Stack. Stack ADT

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT)

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

UNIT 3

Stack Abstract Data Type

Associate Professor Dr. Raed Ibraheem Hamed

12 Abstract Data Types

Top of the Stack. Stack ADT

PA3 Design Specification

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

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

1. Stack Implementation Using 1D Array

Stack and Its Implementation

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

Problem with Scanning an Infix Expression

The Stack and Queue Types

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

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

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

infix expressions (review)

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

CHAPTER 3 STACKS AND QUEUES

Data Structures using OOP C++ Lecture 9

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

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

Content: Learning Objectives

Parsing Scheme (+ (* 2 3) 1) * 1

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

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

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

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

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Stacks, Queues (cont d)

INSTITUTE OF AERONAUTICAL ENGINEERING

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

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

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Data Structure - Stack and Queue-

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

CISC-235. At this point we fnally turned our atention to a data structure: the stack

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

STACKS AND QUEUES CHAPTER 3

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

Lecture 13: Expression Evaluation

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

17CS33:Data Structures Using C QUESTION BANK

Lecture Chapter 6 Recursion as a Problem Solving Technique

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

CS W3134: Data Structures in Java

Transcription:

Stack Chapter 4

Outline Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications Recursive Programming Evaluation of Expressions ADT for stacks

Introduction A stack is an ordered list with the restriction that elements are added or deleted from only one end of the list termed top of stack. The other end of the list which lies inactive is termed bottom of stack. The stack data structure obeys the principle of Last In First Out (LIFO). In other words, elements inserted or added into the stack join last and those that joined last are the first to be removed.

Stack operation The two operations which stack data structure supports are Insertion or addition of elements known as Push. Deletion or removal of elements known as Pop.

Stack Implementation A common and a basic method of implementing stacks is to make use of another fundamental data structure, i.e arrays. Arrays are sequential data structures, but we can also employ linked data structures and have been successfully attempted and applied. An array based implementation of stacks is fairly convenient considering the fact that stacks are unidimensional ordered lists and so are arrays which despite their multi-dimensional structure are inherently associated with a one-dimensional consecutive set of memory locations.

Implementing PUSH Let STACK[1:n] be an array implementation of a stack. Top is variable recording the current top of stack position and initialized to 0. Item is the element to be pushed into the stack and n is the maximum capacity of the stack. Algorithm 4.1 Implementation of push operation on a stack procedure PUSH(STACK, n, top, item) if (top = n) then STACK_FULL; else { top = top + 1; STACK[top] = item; /* store item as top element of STACK */ } end PUSH

Implementing POP Empty_STACK : Top=0 Full_STACK : Top = n Algorithm 4.2 : Implementation of pop operation on a stack procedure POP(STACK, top, item) if (top = 0) then STACK_EMPTY; else { item = STACK[top]; top = top - 1; } end POP

Stack ADT Data objects: A finite set of elements of the same type a linear array Operations: Create an empty stack and initialize top of stack CREATE(STACK) Check if stack is empty CHK_STACK_EMPTY(STACK) (Boolean function) Check if stack is full CHK_STACK_FULL(STACK) (Boolean function) Push ITEM into stack STACK PUSH(STACK, ITEM) Pop element from stack STACK and output the element popped in ITEM POP(STACK, ITEM)

Application of Stack Recursive programming During the execution of a recursive program, to keep track of the calls made to itself and to record the status of the parameters at the time of the call, a stack data structure is used Example: factorial function factorial (5)

Stack implementation on recursive prog.

Tail recursion or Tail end recursion is a special case of recursion where a recursive call to the function turns out to be the last action in the calling function. Note that the recursive call needs to be the last executed statement in the function and not necessarily the last statement in the function. In a stack implementation of a recursive call, all the local variables of the function that are to be remembered, are pushed into the stack when the call is made.

Conti... Upon termination of the recursive call, the local variables are popped out and restored to their previous values. Now for tail recursion, since the recursive call turns out to be the last executed statement, there is no need that the local variables must be pushed into a stack for them to be remembered and restored on termination of the recursive call. factorial1(n, accumulator) { if (n == 0) return accumulator; return factorial1(n - 1, n * accumulator); } factorial(n) { return factorial1(n, 1); }

Evaluation of expression Infix expression <Operator> <Operand> <Operator> Postfix expression <Operator> <Operator> <Operand> a+b*c-d abc*+d- Prefix expression <Operand> <Operator> <Operator> -+a*bcd These are important feature of compiler to evaluate and understand the an arithmetic expression. In reality compiler rework the infix expression to produce the follows the Postfix scheme or Prefix scheme above.

Example Postfix Infix : a+b*c-d

Example prefix Infix : (a*b-f*h)^d

Evaluation of Postfix

Q&A