Stacks and Queues

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Containers: Stack and Queue

CS 250 Data Structures and Algorithms


Prof. Dr. Faisal Shafait

1 / 18
Stack Application

Containers

Permits storage independent of data.


they are distinguished based on the retrieval order.
Fundamental data types.
Value: collection of objects.
Operations: insert, remove, iterate, test if empty.
Intent is clear when we insert.
Which item do we remove?
Stack. Examine the item most recently added.

Queue. Examine the item least recently added.

2 / 18
Stack Application

Containers

Permits storage independent of data.


they are distinguished based on the retrieval order.
Fundamental data types.
Value: collection of objects.
Operations: insert, remove, iterate, test if empty.
Intent is clear when we insert.
Which item do we remove?
Stack. Examine the item most recently added. => LIFO = last in
first out
Queue. Examine the item least recently added. => FIFO = first in
first out

3 / 18
Stack Application Abstract Stack

Abstract Stack: overview

An ordered collection of items where the addition of new items and the
removal of existing items always takes place at the same end.
This end is commonly referred to as the "top."
The end opposite the top is known as the "base."
Sometimes called a "push-down stack".

4 / 18
Stack Application Abstract Stack

Abstract Stack: ordering

The most recently added item is the one that is in position to be


removed first.
this ordering principle is sometimes called LIFO, last-in first-out.
provides an ordering based on length of time in the collection.
newer items are near the top, while older items are near the base.

5 / 18
Stack Application Abstract Stack

Abstract Stack: examples

Many examples of stacks occur in everyday situations.


A stack of trays or plates,
you take the one at the top
uncovering a new tray or plate for the next customer in line.

A stack of books on a desk,


the only book whose cover is visible is the one on
top.
to access others in the stack, remove the ones
sitting on top of them.

6 / 18
Stack Application Abstract Stack

Abstract Stack: examples

Examples
batch job processing - executed when resources permit
passengers entering a subway
food placed in refrigerator
recursive functions

7 / 18
Stack Application Abstract Stack

Abstract Stack: operations

Operations.
Push. adds a new item to the top of the stack.
Pop. removes the top item from the stack.
Misc. operations.
Peek. returns the top item from the stack but does not remove it.
IsEmpty. tests to see whether the stack is empty.
IsFull. tests to see whether the stack is full.
Size. returns the number of items on the stack.
Two exceptions.
Stack overflow exception
Stack underflow exception

8 / 18
Stack Application Abstract Stack

Abstract Stack: question

Given the following sequence of stack operations, what is the top item
on the stack when the sequence is complete?
m := Stack()
m.push(’x’)
m.push(’y’)
m.pop()
m.push(’z’)
m.peek()

(a) ’x’
(b) ’y’
(c) ’z’
(d) The stack is empty

9 / 18
Stack Application Abstract Stack

Abstract Stack: question


Given the following sequence of stack operations, what is the top item
on the stack when the sequence is complete?
m := Stack()
m.push(’x’)
m.push(’y’)
m.push(’z’)
while not m.isEmpty():
m.pop()
m.pop()

(a) ’x’
(b) the stack is empty
(c) an error will occur
(d) ’z’

10 / 18
Stack Application Abstract Stack

Abstract Stack: question

Write a function reverse_string(str) that uses a stack to reverse the


characters in a string.

11 / 18
Stack Application Queues

Queues: ordering

FIFO (first-in-first-out) order.


fairest way to provide services
minimizes max time spent waiting
average time spent is same whether FIFO or LIFO
used when order is important

12 / 18
Stack Application Queues

Queues: ordering

FIFO (first-in-first-out) order.


fairest way to provide services
minimizes max time spent waiting
average time spent is same whether FIFO or LIFO
used when order is important

13 / 18
Stack Application Queues

Queues: ordering

FIFO (first-in-first-out) order.


fairest way to provide services
minimizes max time spent waiting
average time spent is same whether FIFO or LIFO
used when order is important

14 / 18
Stack Application Queues

Queues: operations

Operations.
Enqueue
Dequeue

15 / 18
Stack Application Queues

Queues: exceptions

Exceptions.
Queue overflow exception. adding an element to a full queue
Queue underflow exception. removing an element from an empty
queue

16 / 18
Stack Application Queues

Queues: applications

Applications.
playlist
shared resources like printer
grocery stores, banks, and airport security
customer waiting times in call centers
traffic analysis

17 / 18
Stack Application Queues

Queues: applications

The most common application is in client-server models


Multiple clients may be requesting services from one or more
servers
Some clients may have to wait while the servers are busy
Those clients are placed in a queue and serviced in the order of
arrival

18 / 18

You might also like