Stacks Stack Examples:

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

Associate Professor Dr. Raed Ibraheem Hamed

08 STACKS DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Lecture Data Structure Stack

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

Linked List. April 2, 2007 Programming and Data Structure 1

ADTs Stack and Queue. Outline

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

Lecture 4 Stack and Queue

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

LIFO : Last In First Out

Lecture 3: Stacks & Queues

STACKS AND QUEUES. Problem Solving with Computers-II

Data Structures and Algorithms

File Input and Output

ITI Introduction to Computing II

1 P age DS & OOPS / UNIT II

ITEC2620 Introduction to Data Structures

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

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

Lecture No.04. Data Structures

ITI Introduction to Computing II

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

Week 6. Data structures

CSC 222: Computer Programming II. Spring 2004

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, Queues (cont d)

1. Stack Implementation Using 1D Array

Duomenų struktūros ir algoritmai

Computer Systems Lecture 9

Introduction to Data Structures

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

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

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

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

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

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

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

17CS33:Data Structures Using C QUESTION BANK

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

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

List, Stack, and Queues

CS W3134: Data Structures in Java

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

Arrays and Linked Lists

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

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

An Introduction to Trees

Stack and Its Implementation

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CSE 214 Computer Science II Stack

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lecture 14. Arrays II. Selection Sort & Parallel Arrays. CptS 121 Summer 2016 Armen Abnousi

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Stack Abstract Data Type

12 Abstract Data Types

DS Assignment II. Full Sized Image

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

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

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Stacks Fall 2018 Margaret Reid-Miller

Postfix (and prefix) notation

Data Structures G5029

Lecture Notes on Memory Management

Stacks and Queues. CSE Data Structures April 12, 2002

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

ADTs: Stacks and Queues

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

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

Functions in MIPS. Functions in MIPS 1

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

Run Time Environment

Lecture Notes on Memory Management

CSC 222: Computer Programming II. Spring 2005

Systems I. Machine-Level Programming V: Procedures

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

Top of the Stack. Stack ADT

2012 Academic Challenge

Stacks and queues (chapters 6.6, 15.1, 15.5)

CS 206 Introduction to Computer Science II

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

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

Data Structures And Algorithms

Stacks. Revised based on textbook author s notes.

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

The time and space are the two measure for efficiency of an algorithm.

CMPT 125: Practice Midterm Answer Key

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

March 13/2003 Jayakanth Srinivasan,

Stacks, Queues and Hierarchical Collections

Technical Questions. Q 1) What are the key features in C programming language?

Data Structures using OOP C++ Lecture 9

Transcription:

Stacks 1 Stacks A Stack is a sequential organization of items in which the last element inserted is the first element removed. They are often referred to as LIFO, which stands for last in first out. Examples: letter basket, stack of trays, stack of plates. The only element of a stack that may be accessed is the one that was most recently inserted. There are only two basic operations on stacks, the push (insert), and the pop (read and delete). push pop Stack

Stacks 2 Stacks: Push and Pop The operation push(x) places the item x onto the top of the stack. The operation pop() removes the top item from the stack, and returns that item. We need some way of detecting an empty stack (This is an underfull stack). In some cases, we can have pop() return some value that couldn t possibly be on the stack. Example: If the items on the stack are positive integers, we can return -1 in case of underflow. In other cases, we may be better off simply keeping track of the size of the stack. In some cases, we will also have to worry about filling the stack (called overflow). One way to do this is to have push(x) return 1 if it is successful, and 0 if it fails.

Stacks 3 Example StackOperations Assume we have a stack of size 3 which holds integers between -100 and 100. Here is a series of operations, and the results. Operation Stack Contents Return create () push(55) (55) 1 push(-7) (-7,55) 1 push(16) (16,-7,55) 1 pop (-7,55) 16 push(-8) (-8,-7,55) 1 push(23) (-8,-7,55) 0 pop (-7,55) -8 pop (55) -7 pop () 55 pop () 101

Stacks 4 Implementing Stacks: Array Stacks can be implemented with an array and an integer top that stores the array index of the top of the stack. Empty stack has top = 1, and a full stack has top = n 1, where n is the size of the array. To push, increment the top counter, and write in the array position. To pop, decrement the top counter.

Stacks 5 Example of Array Implementation of Stack We use an array E[0..4] to store the elements and a variable p to keep track of the top. The last column, labeled R is the result of the function call. Operation p E0 E1 E2 E3 E4 R create -1????? push(55) 0 55???? 1 push(-7) 1 55-7??? 1 push(16) 2 55-7 16?? 1 pop 1 55-7 16?? 16 push(-8) 2 55-7 -8?? 1 pop 1 55-7 -8?? -8 pop 0 55-7 -8?? -7 Notice that some values are still in the array, but are no longer considered to be in the stack. In general, elements E[i] are garbage if i > p. Why don t we erase the element (i.e. set it to somedefault value)?

Stacks 6 Example Application of Stacks Stacks can be used to check a program for balanced symbols (such as {,(),[ ]). Example: {() is legal, as is {()({), whereas {(( and {() are not (so simply counting symbols does not work). If the symbols are balanced correctly, then when a closing symbol is seen, it should match the most recently seen unclosed opening symbol. Therefore, a stack will be appropriate. The following algorithm will do the trick: While there is still input: s = next symbol if (s is an opening symbol) push(s) else //s is a closing symbol if (Stack.Empty) report an error else r = pop() if(! Match(s,r)) report an error If (! Stack.Empty) report an error

Stacks 7 1. Input: { ( ) Read {, so push { Examples Read (, so push (. Stack has { ( Read ), so pop. popped item is ( which matches ). Stack has now {. Read, so pop; popped item is { which matches. End of file; stack is empty, so the string is valid. 2. Input: { ( ) ( { ) (This will fail.) 2. Input: { ( { ){ ( ) (This will succeed.) 3. Input: { ( ) ) (This will fail.)

Stacks 8 Stack: C++ Array Implementation The following is a C++ stack that holds items of type ItemType class Stack { private: ItemType *stack; int p; public: Stack(int max=100) {stack = new ItemType[max]; p = 0; Stack() {delete [ ] stack; void push(itemtype v) { stack[p++] = v; ItemType pop() {return stack[--p]; int empty() {return!p; ;

Stacks 9 Stack: JAVA Array Implementation The following is a JAVA stack that holds items of type ItemType public class Stack { private ItemType[] stack; private int p; public Stack(int max) { stack = new ItemType[max]; p = 0; public void push(itemtype v) { stack[p++] = v; public ItemType pop() { return stack[--p]; public int empty() { return!p;

Stacks 10 Operator Precedence Parsing We can use the stack class we just defined to parse and evaluate mathematical expressions like: 5 (((9 + 8) (4 6)) + 7) First, we transform it to postfix notation (How would you do this?): 5 9 8 + 4 6 7 + Then, the following C++ routine uses a stack to perform this evaluation: char c; Stack acc(50); int x; while (cin.get(c)) { x = 0; while (c == ) cin.get(c); if (c == + ) x = acc.pop() + acc.pop(); if (c == * ) x = acc.pop() * acc.pop(); while (c>= 0 && c<= 9 ) {x = 10*x + (c- 0 ); cin.get(c); acc.push(x); cout << acc.pop() << \n ;

Stacks 11 Stack Implementation: C++ Linked Lists We can use linked lists to implement stacks. The head of the list represents the top of the stack. Example After push(d), push(c), push(b), push(a), we have: Head A B C D ItemType pop () { ItemType x = head->key; head=head->next; return x; void push(itemtype x) { node *x; x=new node; x->key = X; insert(x); Note: Slight error in pop. What is it?

Stacks 12 Stack Implementation: JAVA Linked Lists We assume head points to the first element of the list, and is the top of the stack. public ItemType pop () { ItemType x = head.key; head=head.next; return x; public void push(itemtype x) { node A=new node(); A.key = x; A.next=head; head=a;

Stacks 13 Linked Lists Stack Implementation: Array or Linked List? Use 1 pointer extra memory per item. If an item is an integer, that means twice as much space is used. If an item is a structure/class consisting of many objects, it is only a small price to pay. Are unlimited in size. Arrays Allocate a constant amount of space, some of which may never be used. The amount of wasted memory is the number of unused elements times the size of the item, which could be large in some cases. The maximum size is determined when the stack is created. Which is better? Why?

Stacks 14 Stack Applications Recursion removal can be done with stacks. Reversing things is easily done with stacks. Procedure call and procedure return is similar to matching symbols: When a procedure returns, it returns to the most recently active procedure. When a procedure call is made, save current state on the stack. On return, restore the state by popping the stack.