lecture 26: queues csc 212 – data structures. using stack
TRANSCRIPT
![Page 1: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/1.jpg)
LECTURE 26:QUEUESCSC 212 – Data Structures
![Page 2: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/2.jpg)
Using Stack
![Page 3: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/3.jpg)
Great for Pez dispensers, calculators,& methods All of these use only the last item added Do not complain when later additions
served first Many situations use 1 item, but in
different order Take reservations in order they are
received Cards (usually) dealt from top of the deck
Stack Limitations
![Page 4: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/4.jpg)
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 element at front of structure
Also includes method to peek in at first element front() returns element at front without
removing
Queue ADT
![Page 5: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/5.jpg)
public interface Queue<E> extends Collection {public E front() throws EmptyQueueException;public E dequeue() throws EmptyQueueException;public void dequeue(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 the start of a QUEUE… …while STACK removes element at the end
Queue Interface
![Page 6: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/6.jpg)
Stacks vs. Queues
Access data with Stack in LIFO order LLast IIn-FFirst OOut Completely unfair (unless you are always
late) Data accessed in Queue using FIFO
order FFirst IIn-FFirst OOut Better resembles lines at bank, Sabres
game, airports
![Page 7: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/7.jpg)
Among the most fundamental data types Selecting application to run in modern OS Handling and processing network traffic Message checking in IM chat servers Accepting ticket orders for Sabres game
Other data structures also rely upon a Queue
Key feature in many algorithms
Queue Applications
![Page 8: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/8.jpg)
“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 More straightforward, but is it slower?
Queue Implementation
![Page 9: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/9.jpg)
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 10: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/10.jpg)
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 removes element from the front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
q
rf r r r r rr r
![Page 11: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/11.jpg)
fr
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 at opposite ends of array Uses circular access to the array Like a clock –when end (12) reached we
loop to start
Array must be empty at index in rq
r rf rrr
![Page 12: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/12.jpg)
Array-based Queue Operations Based on clock
math Uses mod
(remainder) Java expressed as %
How mod works:4 % 3 = 13 % 4 = 36 % 6 = 012 % 6 = 0
Algorithm size()N q.length return (N f + r) mod N
![Page 13: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/13.jpg)
Array-based Queue Operations
Algorithm enqueue(e)if size() = q.length 1
thenthrow
FullQueueExceptionelse
q[r] er (r + 1) mod
q.length
q
rf
Algorithm dequeue()if isEmpty() then
throw EmptyQueueException
elseretVal q[f]f (f + 1) mod
q.lengthreturn retVal
![Page 14: LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack](https://reader030.vdocuments.site/reader030/viewer/2022032415/56649f065503460f94c1bf39/html5/thumbnails/14.jpg)
Finish week #9 assignment Due by 5PM tomorrow
Continue programming assignment #3 Messages are not always sent to everyone!
Read section 5.3 in book before class Discusses merger of Stack & Queue ADT Implement this Deque ADT with array Could instead implement it with a linked-list Make also sorts of bad jokes about porches
Before Next Lecture…