Lecture 4 Stack and Queue

Similar documents
Lecture Data Structure Stack


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

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

STACKS AND QUEUES. Problem Solving with Computers-II

CS 206 Introduction to Computer Science II

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

! Set of operations (add, remove, test if empty) on generic data. ! Intent is clear when we insert. ! Which item do we remove? Stack.

4.3 Stacks and Queues

Lecture 3: Stacks & Queues

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

Postfix (and prefix) notation

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

4.3 Stacks and Queues. Stacks. Stacks and Queues. Stack API

PA3 Design Specification

The Stack and Queue Types

An Introduction to Trees

Linear Data Structure

Information Science 2

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

DEEPIKA KAMBOJ UNIT 2. What is Stack?

March 13/2003 Jayakanth Srinivasan,

12 Abstract Data Types

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

Stacks and Queues. CSE Data Structures April 12, 2002

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

Stacks and Queues. CSE 373 Data Structures Lecture 6

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

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Stacks, Queues and Hierarchical Collections

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

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

infix expressions (review)

push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m).

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

CS 206 Introduction to Computer Science II

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

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

17CS33:Data Structures Using C QUESTION BANK

Stack Abstract Data Type

BBM 201 DATA STRUCTURES

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

Queue: Queue Representation: Basic Operations:

DATA STRUCTURES AND ALGORITHMS

CSE 214 Computer Science II Stack

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

CS W3134: Data Structures in Java

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Name CPTR246 Spring '17 (100 total points) Exam 3

What is an algorithm?

Stack and Queue. Stack:

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

Stacks and their Applications

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

LIFO : Last In First Out


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

Data Structures and Algorithms

Stacks. Revised based on textbook author s notes.

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

15. Stacks and Queues

Foundations of Data Structures

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

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

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

CSE 230 Intermediate Programming in C and C++

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

TSP assignment next lecture

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

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

Sample Question Paper

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

UNIT-2 Stack & Queue

Recursive Data Structures and Grammars

1. Stack Implementation Using 1D Array

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

CS 106B Lecture 5: Stacks and Queues

Lab 7 1 Due Thu., 6 Apr. 2017

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

List, Stack, and Queues

LECTURE 17 GRAPH TRAVERSALS

Stacks and queues (chapters 6.6, 15.1, 15.5)

BBM 201 DATA STRUCTURES

Stacks and Queues. David Greenstein Monta Vista

Motivation for Queues

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

2.6 Stacks and Queues

Lecture 12 ADTs and Stacks

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

INSTITUTE OF AERONAUTICAL ENGINEERING

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

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

Abstract Data Type: Stack

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

Chapter 2. Stack & Queues. M hiwa ahmad aziz

1 P age DS & OOPS / UNIT II

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

Transcription:

Lecture 4 Stack and Queue Bo Tang @ SUSTech, Spring 2018

Our Roadmap Stack Queue Stack vs. Queue 2

Stack A stack is a sequence in which: Items can be added and removed only at one end (the top) You can only access the item that is currently at the top Stack Analogy 3

Stack First In Last Out (FILO) Constrained item access Major Operations push: add an item to the top of the stack pop: remove the item at the top of the stack Illustration top top a top empty push(a) push(b), push(c) pop() c b a top b a 4

Stack Operation push: add an item to the top of the stack pop: remove the item at the top of the stack top/peek: get the item at the top of the stack, but do not remove it isempty: test if the stack is empty isfull: test if the stack is full clear: clear the stack, set it as empty stack size: return the current size of the stack top c b a top d c b a top c b a push(d) pop() top clear() 5

Implementation of Stack Array-based Stack Linked Stack top 9 9 6 5 4 1 top 6 5 4 1 ^ 6

Implementation of Stack Array based Stack MAX_SIZE = n // the max size of stack top = -1 // the current top position Array S with n elements Example MAX_SIZE = 5 top = -1 Array S S top S[4] S[3] S[2] S[1] S[0] 7

Push Operator push(item): 1. top++; 2. S[top] = item push(1) S[4] S[3] S[2] S[1] top=0 S[0] 1 push(1) 8

Push Operator push(item): 1. top++; 2. S[top] = item push(1) push(4) top=1 top=0 S[4] S[3] S[2] S[1] S[0] 4 1 push(4) push(1) 9

Push Operator push(item): 1. top++; 2. S[top] = item push(1) push(4) push(5) top=2 top=1 S[4] S[3] S[2] S[1] 5 4 push(5) push(4) top=0 S[0] 1 push(1) 10

Push Operator push(item): 1. top++; 2. S[top] = item push(1) push(4) push(5) push(6) top=3 top=2 top=1 S[4] S[3] S[2] S[1] 6 5 4 push(6) push(5) push(4) top=0 S[0] 1 push(1) 11

Push Operator push(9) top=4 S[4] 9 push(9) S[3] 6 S[2] 5 S[1] 4 S[0] 1 push(10) OVERFLOW How to avoid that? top=5 S[4] 9 S[3] 6 S[2] 5 S[1] 4 S[0] 1 12

Push / Pop Operator push(item): 1. if(top == MAXSIZE-1) 2. Stack is FULL! No push! 3. else 4. top++; 5. S[top] = item pop(): // should avoid underflow 1. if(top == -1) 2. Stack is EMPTY! No pop! 3. else 4. top--; 13

Application of Stacks Making sure the delimiters (parens, brackets, etc.) are balanced: Push open (i.e., left) delimiters onto a stack When you encounter a close (i.e., right) delimiter, pop an item off the stack and see if it matches Evaluating arithmetic expressions Parsing arithmetic expressions written using infix notation The runtime stack in memory Converting a recursive algorithm to an iterative one by using a stack to emulate the runtime stack 14

Brackets Balance Problem a+{2-[b+c ]*(8* [8+g ]/[m-e ]-7)-p} {[ ]([ ][ ])} Skip operators and notations Is the bracket expression balanced or not? () Yes [} No {[()}] No {[ ]( [ ][ ])} Yes {{{{{{[[[[[[(((((())))))]]]]]]}}}}}} Yes 15

Brackets Balance Problem Given a bracket expression, determine whether it is balanced or not? {[ ]([ ][ ])} How to solve it by using stack? Bracket pairs: ( ), [ ], { } Any ideas? Methodology Employ stack store checked left bracket Pop out left bracket if it is matched 16

Arithmetic Expression Evaluation Arithmetic expression operands (a, b, c), operator (+, *) a + b * c Prefix expression + a * b c Infix expression a + b * c Postfix expression a b c * + 17

Postfix Expression Infix expression 5 * ((9 + 3) * (4*2) + 7) Postfix expression 5 9 3 + 4 2 * * 7 + * Parse postfix expression is somewhat easier problem than directly parsing infix (why) Postfix has a nice property that parentheses are unnecessary Postfix Expression Evaluation Convert from infix to postfix (one of lab problems) Evaluate a postfix expression 18

Postfix Expression Postfix expression 5 9 3 + 4 2 * * 7 + * Methodology Read the tokens in one at a time If it is an operand, push it on the stack If it is a binary operator: pop top two elements from the stack, apply the operator, and push the result back on the stack 19

Postfix Expression Evaluation 5 9 3 + 4 2 * * 7 + * Postfix Expression Evaluation Stack operations Stack elements push(5) 5 push(9) 5 9 push(3) 5 9 3 push(pop() + pop()) 5 12 push(4) 5 12 4 push(2) 5 12 4 2 push(pop() * pop()) 5 12 8 push(pop() * pop()) 5 96 push(7) 5 96 7 push(pop() + pop()) 5 103 push(pop() * pop()) 515 20

Our Roadmap Stack Queue Stack vs. Queue 21

Queue A queue is a sequence in which: items are added at the rear and removed from the front You can only access the item that is currently at the front Queue Analogy 22

Queue First In First Out (FIFO) Items access constrained Major elements front: the first element in the queue (remove) rear: the last element in the queue (add) Illustration dequeue a 1, a 2,, a n enqueue front rear 23

Queue Operations enqueue: add an item at the rear of the queue dequeue: remove the item at the front of the queue front: get the item at the front of the queue, but do not remove it isempty: test if the queue is empty isfull: test the queue is full clear: clear the queue, set it as empty queue size: return the current size of the queue rear front c b a rear front d c b a enqueue(d) rear front d c b dequeue() 24

Implementation of Queue Array based Queue MAX_SIZE = n // the max size of stack front = 0 // the current front rear = 0 // the current rear Array S with n elements Example MAX_SIZE = 5 front = 0 rear = 0 Array S rear front S[4] S[3] S[2] S[1] S[0] S 25

enqueue Operator enqueue(item): 1. if(rear < MAXSIZE) 2. S[rear] = item 3. rear++ 3. else 4. Queue is FULL, no enqueue enqueue (1), enqueue(4), enqueue(5), enqueue(6) front=0 S[4] S[3] 6 S[2] 5 S[1] 4 S[0] 1 rear = 4 26

dequeue Operator dequeue(): 1. if(front < rear) 2. front++ 3. else 4. Queue is empty, no dequeue dequeue (), dequeue(), dequeue(), dequeue() front=4 S[4] S[3] S[2] S[1] S[0] rear = 4 27

enqueue and dequeue enqueue(9) front=4 enqueue(10) rear >= MAXSIZE Queue is FULL!!! Wrong OVERFLOW S[0] to S[3] is empty? How to address it? front=4 rear = 5 S[4] 9 S[3] S[2] S[1] S[0] S[4] 9 S[3] S[2] S[1] S[0] rear = 5 28

Ring Queue front rear 0 1 5 4 3 2 front rear d 0 5 4 1 3 e 2 g b rear 5 0 4 1 2 3 g b front (a) empty (b) d, e, b, g enqueue (c) d, e dequeue j k 5 0 i 4 1 2 3 g rear front b j rear 1 2 3 front (d) i, j, k enqueue (e) b, g dequeue k 5 0 i 4 front j k 5 0 i 4 1 3 r 2 p rear (f) r, p, s, t dequeue 29

Application of Queues First-in first-out (FIFO) inventory control OS scheduling: processes, print jobs, packets, etc. Breadth-first traversal of a graph or level-order traversal of a binary tree (more on these later) Real applications itunes playlist. Data buffers (ipod, TiVo). Asynchronous data transfer (file IO, pipes, sockets). Dispensing requests on a shared resource (printer, processor) 30

Our Roadmap Stack Queue Stack vs. Queue 31

Stack VS. Queue Stack Queue In-Out FILO FIFO Application function runtime OS scheduling Operations push pop enqueue, dequeue Ops Time Complexity O(1) O(1) Implementa tion Array-based, Linked-based Array-based, Linked-based 32

Data Structure 33

Our Roadmap Stack Queue Stack vs. Queue 34

Thank You! 35