STACKS AND QUEUES. Problem Solving with Computers-II

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

The Stack and Queue Types

Lecture Data Structure Stack

Stacks and their Applications

PA3 Design Specification

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

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

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

March 13/2003 Jayakanth Srinivasan,

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

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

Stack Abstract Data Type

Stack and Its Implementation

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

12 Abstract Data Types

[CS302-Data Structures] Homework 2: Stacks

Programming Abstractions

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

Programming Abstractions

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

Stacks. Revised based on textbook author s notes.

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

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

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

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

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

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

Lecture 12 ADTs and Stacks

Data Structure. Recitation VII

Programming Abstractions

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

CSC148 Week 2. Larry Zhang

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

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

CSC 222: Computer Programming II. Spring 2004

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

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

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

Postfix (and prefix) notation

Lecture 4 Stack and Queue

Infix to Postfix Conversion

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

1 P age DS & OOPS / UNIT II

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

-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

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.

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

Queues Fall 2018 Margaret Reid-Miller

Top of the Stack. Stack ADT

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

Chapter 18: Stacks And Queues

ADTs: Stacks and Queues

infix expressions (review)

List, Stack, and Queues

Abstract Data Types 1

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Stacks Fall 2018 Margaret Reid-Miller

CSC148H Week 3. Sadia Sharmin. May 24, /20

Monday, November 13, 2017

Data Structure - Stack and Queue-

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Stacks Calculator Application. Alexandra Stefan

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Chapter 18: Stacks And Queues

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

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

Stacks and Queues. CSE Data Structures April 12, 2002

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

DATA STRUCTURE UNIT I

Stacks, Queues (cont d)

Algorithms and Data Structures Exercise I

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

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

Stacks, Queues and Hierarchical Collections

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

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

CS W3134: Data Structures in Java

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

Advanced Java Concepts Unit 3: Stacks and Queues

DS Assignment II. Full Sized Image

Stacks and queues (chapters 6.6, 15.1, 15.5)

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

CSE 230 Intermediate Programming in C and C++

csci 210: Data Structures Stacks and Queues

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

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

DEEPIKA KAMBOJ UNIT 2. What is Stack?

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linear Data Structure

Transcription:

STACKS AND QUEUES Problem Solving with Computers-II

2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop() top() empty() Demo reversing a string

3 Notations for evaluating expression Infix number operator number ( 7 + ( 3 * 5) ) ( 4 / 2 ) Prefix operators precede the operands Postfix operators come after the operands

Lab05 part 1: Evaluate a fully parenthesized infix expression 4

( ( 2 * 2 ) + ( 8 + 4 ) ) 5

6 ( ( 2 * 2 ) + ( 8 + 4 ) ) What should be the next step after the first right parenthesis is encountered? A. Push the right parenthesis onto the stack B. If the stack is not empty pop the next item on the top of the stack C.Ignore the right parenthesis and continue checking the next character D.None of the above

( ( 2 * 2 ) + ( 8 + 4 ) ) 7

Evaluating a fully parenthesized infix expression 8

Evaluating a fully parenthesized infix expression 9

Evaluating a fully parenthesized infix expression 10

Evaluating a fully parenthesized infix expression 11

12 Lab 05, part2 : Evaluating post fix expressions using a single stack Postfix: 7 3 5 * + 4 2 / - Infix: ( 7 + ( 3 * 5) ) ( 4 / 2 )

13 Small group exercise Write a ADT called in minstack that provides the following methods push() // inserts an element to the top of the minstack pop() // removes the last element that was pushed on the stack top () // returns the last element that was pushed on the stack min() // returns the minimum value of the elements stored so far

The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ Rear Front

The Queue Operations New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Rear Front

The Queue Operations When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ Rear Front

The Queue Class The C++ standard template library has a queue template class. The template parameter is the type of the items that can be put in the queue. template <class Item> class queue<item> { public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const;

Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 4 8 6 An array of integers to implement a queue of integers We don't care what's in this part of the array.

Array Implementation The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). 3 0 2 size first last [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 4 8 6

A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. 2 1 size first 2 last [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 4 8 6

An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. 3 1 size first 3 last [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 8 6 2

At the End of the Array There is special behavior at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: 3 3 5 size first last [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 2 6 1

At the End of the Array The new element goes at the front of the array (if that spot isn t already used): 4 3 size first 0 last [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 4 2 6 1

Array Implementation Easy to implement But it has a limited capacity with a fixed array Or you must use a dynamic array for an unbounded capacity Special behavior is needed when the rear reaches the end of the array. 3 0 2 size first last [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 4 8 6

Summary Like stacks, queues have many applications. Items enter a queue at the rear and leave a queue at the front. Queues can be implemented using an array or using a linked list.