Upload
anastasia-beasley
View
214
Download
0
Embed Size (px)
Citation preview
LECTURE 27:DEQUESCSC 212 – Data Structures
Roses are red and violets are blue
Implement push, pop, & top
And you’re a Stack too!
Stack & ADT Memory Aid
public interface Stack<E> {public E top() throws EmptyStackException;
public E pop() throws EmptyStackException;
public void push(E elem);
public int size();public boolean isEmpty();
}
Stack Interface
Queue Memory Aid
It’s hard writing rhymes with
enqueue, dequeue, and
front
public interface Queue<E> {public E front() throws EmptyQueueException;
public E dequeue() throws EmptyQueueException;
public void enqueue(E elem);
public int size();public boolean isEmpty();
}
Queue ADT
Stack vs. Queue
Access data with Stack in LIFO order LLast IIn-FFirst OOut is totally unfair (unless
always late) Data accessed in Queue using FIFO
order FFirst IIn-FFirst OOut ensures early bird gets
the worm
Order read if
Stack
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
Pronounced “deck” (like on a house) Mnemonic for DDouble EEnded QUEQUEue 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
public interface Deque<E> {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
VIP Lines & Lucky Losers
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
Ø
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
rf r r r r rr r f
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
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…