Top Banner
LECTURE 24: QUEUES CSC 212 – Data Structures
15

CSC 212 – Data Structures. Using Stack Stack Limitations Great for Pez dispensers, JVMs,& methods All of these use most recent item added only

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

LECTURE 24:QUEUES

CSC 212 – Data Structures

Page 2: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Last-In, First-Out principle used to access data Also called LIFO ordering

Top of stack is where data added & removed Pulling out tablecloth trick does not work

Using Stack

Page 3: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Stack Limitations

Great for Pez dispensers, JVMs,& methods All of these use most recent item added

only Do not complain when later additions

served first Many situations use items in order

added Checker at Wegmans & other stores do not

cut line Use first-come, first-served getting food at

dining hall

Page 4: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Collection’s operations are part of Queue As in Stack, declares size() & isEmpty()

Add & remove elements using 2 methods Element gets added to end with enqueue(elem) dequeue() removes front element in structure

Also includes method to peek in at first element front() returns element at front without removing

Queue ADT

Page 5: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Queue Interface

public interface Queue<E> extends Collection {public E front() throws EmptyQueueException;public E dequeue() throws EmptyQueueException;public void enqueue(E element);

}

Very similar to Stack interface Defines specific methods to add, remove, &

view data Holds many elements, but can access only

one Stack & Queue always add to the end

Remove element at start of this QUEUE… …while STACK removes element at the end

Page 6: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Stacks vs. Queues

Access data with Stack in LIFO order

Last In-First Out Completely unfair (unless you are always

late) Data accessed in Queue using FIFO

order

First In-First Out Lines at bank, airports represented fairly

with these

Page 7: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

“Obvious” implementation uses an array Must consume a constant amount of space enqueue() throws exception when it lacks

space Instead write linked list-based

implementation Singly-, doubly-, or circular-linked list could

work Size of the Queue grows & shrinks as

needed No additional exceptions needed, but is it

slower?

Queue Implementation

Page 8: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Class defines fields aliased to first & last nodes head & rear often used as fields’ names

(creative!) enqueue element by adding new Node after rear

Set head to next Node in list to dequeue element

Linked-list based Queue

head rear

Page 9: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Class defines fields aliased to first & last nodes head & rear often used as fields’ names

(creative!) enqueue element by adding new Node after rear

Set head to next Node in list to dequeue element

Linked-list based Queue

head rear

retVal

Page 10: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0

QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue

Ends of a array-based QUEUE like clock time

Circular Access

q

Page 11: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Array-based Queue

Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element

Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,

loop to start

Array must be empty at index in r

q

Page 12: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Array-based Queue Operations

Based on clock math Uses mod

(remainder) Java expressed mod

as %

How mod works:0 % 3 = 01 % 3 = 12 % 3 = 23 % 3 = 0

Algorithm size()N q.length return (N - f + r)

mod N

Page 13: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Array-based Queue Operations

Algorithm enqueue(e)if size() = q.length 1

thenthrow

FullQueueExceptionelse

q[r] er (r + 1) mod

q.lengthq

rf

Algorithm dequeue()if isEmpty() then

throw EmptyQueueException

elseretVal q[f]f (f + 1) mod

q.lengthreturn retVal

Page 14: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

Your Turn

Get into your groups and complete activity

Page 15: CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only

For Next Lecture

Read GT section 5.3 before Friday's class Discusses design of the Deque ADT Array-based implementation of Deque

presented Deque implementation of linked-list also

shown

Week #9 weekly assignment due on Tuesday

Midterm #2 will be in class next Wednesday