14
LECTURE 27: DEQUES CSC 212 – Data Structures

LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

Embed Size (px)

Citation preview

Page 1: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

LECTURE 27:DEQUESCSC 212 – Data Structures

Page 2: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

Roses are red and violets are blue

Implement push, pop, & top

And you’re a Stack too!

Stack & ADT Memory Aid

Page 3: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 4: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

Queue Memory Aid

It’s hard writing rhymes with

enqueue, dequeue, and

front

Page 5: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 6: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 7: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 8: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 9: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 10: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

VIP Lines & Lucky Losers

Page 11: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Ø

Page 12: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 13: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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

Page 14: LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory

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…