Download presentation
Presentation is loading. Please wait.
Published byAnastasia Beasley Modified over 9 years ago
1
LECTURE 27: DEQUES CSC 212 – Data Structures
2
Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory Aid
3
public interface Stack { public E top() throws EmptyStackException; public E pop() throws EmptyStackException; public void push(E elem); public int size(); public boolean isEmpty(); } Stack Interface
4
Queue Memory Aid It’s hard writing rhymes with enqueue, dequeue, and front
5
public interface Queue { public E front() throws EmptyQueueException; public E dequeue() throws EmptyQueueException; public void enqueue(E elem); public int size(); public boolean isEmpty(); } Queue ADT
6
Stack vs. Queue Access data with Stack in LIFO order LIFO L ast I n- F irst O ut is totally unfair (unless always late) Data accessed in Queue using FIFO order FIFO F irst I n- F irst O ut ensures early bird gets the worm Order read if Stack
7
Cannot access both sides of Collection Transplant waiting lists Help center phone banks Grandpa dealing cards for money Stack only works on one end All insertions & removals from the Stack ’s top Queue limits how each side used Elements can only be added to end Front allows accessing & removal of elements Still Have Limits
8
Pronounced “deck” (like on a house) DEQUE Mnemonic for Double Ended QUEue dequeue ≠ deque and do not sound alike Structure that provides access to both ends Combines Stack & Queue concepts Is also able to add elements to start Deque ADT
9
public interface Deque { public E getFirst() /* front() */ throws EmptyDequeException; public E removeFirst() /* dequeue() */ throws EmptyDequeException; public E getLast() /* top() */ throws EmptyDequeException; public E removeLast() /* pop() */ throws EmptyDequeException; public void addFirst(E elem); /* Brand new! */ public void addLast(E elem); /* push() & enqueue() */ public int size(); public boolean isEmpty(); } Deque Interface
10
VIP Lines & Lucky Losers
11
Class defines fields aliased to first & last nodes Doubly-linked list enables O(1) time for all methods Add elements by adding new Node at end Update sentinels next/previous to remove element Linked-list based Deque head rear Ø retVal Ø
12
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front Structure’s end moved by addLast & removeLast Ends of a array-based DEQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q r f r rrr r r r f
13
Array-based DEQUE Operations Uses property of clock math Remainder of result is all that matters But the values sign is also important -1 % 4 == -1, for example To get this to work, use a cheap trick Adding size of the array does not change result (-1 + 6) % 6 (3 + 6) % 6 = 5 % 6= 9 % 6 = 5= 3
14
Start week #10 assignment Due by 5PM next Tuesday Continue programming assignment #3 Messages are not always sent to everyone! Read section 6.1 in book before class What if we wanted to access all the items? Could we get something like a resizable array? How do ranks differ from indices? Before Next Lecture…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.