Data Structures and Algorithms For Information Processing: Lecture 5: Stacks & Queues

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Data Structures and

Algorithms for Information


Processing
Lecture 5: Stacks & Queues

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Stacks
What is a Stack?
A stack is a data structure of
ordered items such that items can
be inserted and removed only at
one end.
Examples of Stacks:
Pez Dispenser
Cafeteria Trays
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Stacks
What can we do with a stack?
push - place an item on the stack
peek - Look at the item on top of
the stack, but do not remove it
pop - Look at the item on top of
the stack and remove it

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Stacks
A stack is a LIFO (Last-In/First-Out)
data structure
A stack is sometimes also called a
pushdown store.
What are some applications of stacks?
Program execution
Parsing
Evaluating postfix expressions
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Stacks
Problem:
What happens if we try to pop an
item off the stack when the stack is
empty?
This is called a stack underflow. The pop
method needs some way of telling us that
this has happened. In java we use the
java.util.EmptyStackException

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Implementing a Stack
There are two ways we can implement a
stack:
Using an array
Using a linked list

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Implementing a Stack
Implementing a stack using an array is
fairly easy.
The bottom of the stack is at data[0]
The top of the stack is at
data[numItems-1]
push onto the stack at
data[numItems]
pop off of the stack at
data[numItems-1]
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Implementing a Stack
Implementing a stack using a linked list
isnt that bad either
Store the items in the stack in a linked
list
The top of the stack is the head node,
the bottom of the stack is the end of
the list
push by adding to the front of the list
pop by removing from the front of the
list
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Reversing a Word
We can use a stack to reverse the
letters in a word.
How?

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

Reversing a Word
Read each letter in the word and push it
onto the stack
When you reach the end of the word,
pop the letters off the stack and print
them out.

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

10

The N-Queens Problem


Main slides

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

11

Queues
What is a queue?
A data structure of ordered items
such that items can be inserted only
at one end and removed at the other
end.
Example
A line at the supermarket

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

12

Queues
What can we do with a queue?
Enqueue - Add an item to the queue
Dequeue - Remove an item from the
queue
The book calls these insert and getFront
in order to simplify things.

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

13

Queues
A queue is called a FIFO (First in-First
out) data structure.
What are some applications of queues?
Round-robin scheduling in processors
Input/Output processing
Queueing of packets for delivery in
networks

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

14

Implementing a Queue
Just like a stack, we can implementing a
queue in two ways:
Using an array
Using a linked list

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

15

Implementing a Queue
Using an array to implement a queue is
significantly harder than using an array
to implement a stack. Why?
Unlike a stack, where we add and
remove at the same end, in a queue
we add to one end and remove from
the other.

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

16

Implementing a Queue
There are two options for implementing
a queue using an array:
Option 1:
Enqueue at data[0] and shift all of
the rest of the items in the array
down to make room.
Dequeue from data[numItems-1]

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

17

Implementing a Queue
Option 2
Enqueue at data[rear+1]
Dequeue at data[front]
The rear variable always contains the
index of the last item in the queue.
The front variable always contains the
index of the first item in the queue.
When we reach the end of the array,
wrap around to the front again.
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

18

Implementing a Queue
// option 2 sketch of insert
insert(Object item) {
if(manyItems == 0) front = rear = 0;
else rear = (rear + 1) mod size;
data[rear] = item;
manyItems++;
}
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

19

Implementing a Queue
// option 2 sketch of getFront
Object getFront() {
answer = data[front];
front = (front + 1) mod size;
manyItems--;
return answer
}
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

20

Implementing a Queue
Which approach is better?
Assume that the queue has a large
number of entries.
In best case, for both queues, what is
Big Theta?
In the worst case, for both queues,
what is Big Theta?
Does it make sense to talk about
cases?
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

21

Implementing a Queue
Implementing a queue using a linked
list is still easy:
Front of the queue is stored as the
head node of the linked list, rear of
the queue is stored as the tail node.
Enqueue by adding to the end of the
list
Dequeue by removing from the front
of the list.
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

22

Palindromes
We can determine whether or not a
word is a palindrome using a stack and
a queue.
How?

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

23

Palindromes
Read each letter in the phrase. Enqueue
the letter into the queue, and push the
letter onto the stack.
After we have read all of the letters in the
phrase:
Until the stack is empty, dequeue a
letter from the queue and pop a letter
from the stack.
If the letters are not the same, the
phrase is not a palindrome
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

24

Priority Queues
Sometimes we may want certain things
to get to go to the front of the line.
Examples:
Frequent fliers on airlines
Real-time video and audio packets in
networks

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

25

Priority Queues
In a priority queue, each item stored in
the queue has a priority associated with
it.
When we call enqueue, we pass the
item to be enqueued and the priority
associated with that item.

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

26

Implementing a PQ
There are several ways in which we
might implement a priority queue:
Use an array of ordinary queues, one
for each priority.
Queues[0] is the queue for priority 0,
queues[1] is the queue for priority 1

Use a sorted linked list


The list should be sorted according the the
priorities of the items contained

Which approach is better?


90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

27

Thinking Exercise
Where might a computers operating
system use queues?

90-723: Data Structures


and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

28

Review
A stack is a LIFO data structure
A queue is a FIFO data structure
Both queues and stacks can be
implemented using either linked lists or
arrays
A priority queue is a queue in which the
ordering of the items is determined by
the priorities assigned to them.
90-723: Data Structures
and Algorithms for
Information Processing

Lecture 5: Stacks and Queues


Copyright 1999, Carnegie Mellon. All Rights Reserved.

29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy