27
7/11/2011 1 Stacks and Queues 15-121 Introduction to Data Structures Ananda Gunawardena

Lecture 09 Stacks and Queues

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 09 Stacks and Queues

7/11/2011 1

Stacks and Queues

15-121

Introduction to Data Structures

Ananda Gunawardena

Page 2: Lecture 09 Stacks and Queues

Stack and Heap

• A stack is a first-in-last-out structure

7/11/2011 2

• When a method is called computer stores all

related data in a place called stack

• When the method returns, stack is used

generate the return parameters

Page 3: Lecture 09 Stacks and Queues

Basic Architecture

7/11/2011 3

•Stacks retain

Information

About where to

return

Stacks can be used

(implicitly) to

implement a

technique called

recursion

Page 4: Lecture 09 Stacks and Queues

7/11/2011 4

A Stack interface

public interface Stack {

public void push(Object x);

public void pop();

public Object top();

public boolean isEmpty();

public void clear();

}

Page 5: Lecture 09 Stacks and Queues

7/11/2011 5

Stacks are LIFO

a

b

c

d

e

Push operations:

Page 6: Lecture 09 Stacks and Queues

7/11/2011 6

Stacks are LIFO

a

b

c

d

e

Pop operation:

Last element that was pushed is the first to be popped.

Page 7: Lecture 09 Stacks and Queues

Implementing Stacks

7/11/2011 7

Page 8: Lecture 09 Stacks and Queues

7/11/2011 8

Implementing stacks using

arrays• Use an array-based representation.

• What are some advantages and disadvantages of an

array-based representation?

a b c

top

Page 9: Lecture 09 Stacks and Queues

7/11/2011 9

Implementing stacks using

linked lists

a

b

c

Linked representation.All operations constant time, or O(1).

Page 10: Lecture 09 Stacks and Queues

7/11/2011 10

Queues

• Behavior

– add item only from the back (enqueue)

– remove items only from the front (dequeue)

• Many Applications

– printer queue

– network queues

– common resource sharing

• Queue Operations

– enqueue

– dequeue

– clear

– empty

– full

Page 11: Lecture 09 Stacks and Queues

7/11/2011 11

A Queue interface

public interface Queue {

public void enqueue(Object x);

public Object dequeue();

public boolean isEmpty();

public void clear();

}

Page 12: Lecture 09 Stacks and Queues

7/11/2011 12

Queues are FIFO

frontback

k r q c m

Page 13: Lecture 09 Stacks and Queues

7/11/2011 13

Queues are FIFO

frontback

k r q c my

Enqueue operation:

Page 14: Lecture 09 Stacks and Queues

7/11/2011 14

Queues are FIFO

frontback

k r q c my

Enqueue operation:

Page 15: Lecture 09 Stacks and Queues

7/11/2011 15

Queues are FIFO

frontback

k r q c my

Dequeue operation:

Page 16: Lecture 09 Stacks and Queues

Implementing Queues

7/11/2011 16

Page 17: Lecture 09 Stacks and Queues

Implementing a Queue with an

array• What are the disadvantages?

7/11/2011 17

Page 18: Lecture 09 Stacks and Queues

Implementing a Queue with an

circular array• What are the advantages?

7/11/2011 18

Page 19: Lecture 09 Stacks and Queues

7/11/2011 19

A queue from two stacks

f

g

h

i

j

Enqueue:

e

d

c

b

a

Dequeue:

What happens when the stack on the right becomes empty?

Page 20: Lecture 09 Stacks and Queues

Applications

7/11/2011 20

Page 21: Lecture 09 Stacks and Queues

7/11/2011 21

Applications of Stacks

• Balancing Symbols

• Problem: Write a program that will check the validity of a statement

• Eg: (2 + 3 ( 4 – 5 )) is valid

• (2 + 3 (4 – 5 ) is not valid

• Discuss an algorithm using stacks

Page 22: Lecture 09 Stacks and Queues

Infix to postfix algorithm

• Read in the tokens one at a time

• If a token is an integer, write it into the output

• If a token is an operator, push it to the stack, if the stack is empty. If the stack is not empty, you pop entries with higher or equal priority and only then you push that token to the stack.

• If a token is a left parentheses '(', push it to the stack

• If a token is a right parentheses ')', you pop entries until you meet '('.

• When you finish reading the string, you pop up all tokens which are left there.

• Arithmetic precedence is in increasing order: '+', '-', '*', '/';

7/11/2011 22

Page 23: Lecture 09 Stacks and Queues

7/11/2011 23

Applications of Stacks ctd..

• Evaluating Postfix expressions

• Consider the expression

( 2+ 3*(5 – 2) + 3*2)

• How do we write a program to find the answer to the

above expression?

• Write the expression in postfix notation

2 3 5 2 - * + 3 2 * +

• Use a stack to evaluate it.

Page 24: Lecture 09 Stacks and Queues

7/11/2011 24

Stack Application - Maze

• Think about a grid of rooms separated by walls.

• Each room can be given a name.

a b c d

hgfe

i j k l

ponm

Find a path thru the maze from a to p.

Page 25: Lecture 09 Stacks and Queues

7/11/2011 25

Applications of Queue

• Breadth First Traversal

Page 26: Lecture 09 Stacks and Queues

7/11/2011 26

Applications of Queue

• Snake Game

Page 27: Lecture 09 Stacks and Queues

To Do

• Read notes on recursion

• Think of more applications where a stack or queue can be used

• Read the assignment on Mazes

7/11/2011 27