Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go to my first son, Abdul, One-fourth to Wasim, one-fifth to Rasul, Call a wise man to distribute — don’t sell or kill.” How does the Wise Man do it?
Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go to my first son, Abdul, One-fourth to Wasim, one-fifth to Rasul, Call a wise man to distribute — don’t sell or kill.” How does the Wise Man do it? The Wise Man adds his own camel (making 20 to distribute) Abdul gets10 (20 * 0.5) Wasim gets 5 (20 * 0.25) Rasul gets 4 (20 * 0.2) & Wise Man gets his camel back (20 – 10 – 5 – 4 = 1)
CSC 212 – Data Structures
Stack Memory Aid Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too!
Stack Interface public interface Stack extends Collection { public E top() throws EmptyStackException; public E pop() throws EmptyStackException; public void push(E element); }
Queue Memory Aid
It’s hard writing rhymes with enqueue, dequeue, and front
Queue Memory Aid
public interface Queue extends Collection { public E front() throws EmptyQueueException; public E dequeue() throws EmptyQueueException; public void enqueue(E element); } Queue ADT
Stacks vs. Queues
Order read if Queue
Stacks vs. Queues Order read if Queue Order read if Stack
Cannot access both sides of either Collection Transplant waiting lists Help center phone banks My Grandpa dealing cards for money Stack only works with one end Add & remove from top of the Stack Queue limits how each side used Front provides access & removal of elements Must use the Queue ’s end to add elements Still Have Limits
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
public interface Deque extends Collection { /* front() */ public E getFirst() throws EmptyDequeException; /* top() */ public E getLast() throws EmptyDequeException; /* dequeue() */ public E removeFirst() throws EmptyDequeException; /* pop() */ public E removeLast() throws EmptyDequeException; /* push() or enqueue() */ public addLast(E elem); /* brand new method! */ public addFirst(E elem); } Deque Interface
VIPs versus 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 sentinel’s next or previous to remove element Linked-list based Deque head rear
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 sentinel’s next or previous to remove element Linked-list based Deque rear retVal head
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 sentinel’s next or previous to remove element Linked-list based Deque retVal head rear
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 sentinel’s next or previous to remove element Linked-list based Deque retVal head rear
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 sentinel’s next or previous to remove element Linked-list based Deque newElem head rear
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 sentinel’s next or previous to remove element Linked-list based Deque newElem newNode head rear
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 sentinel’s next or previous to remove element Linked-list based Deque newElem newNode head rear
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 sentinel’s next or previous to remove element Linked-list based Deque head rear
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
D EQUES, like Q UEUES, have both ends move f addFirst & removeFirst moves its f ront index r r ear index moved by addLast & removeLast Ends of a array-based D EQUE like clock time Identical to Queue and how it works, except… …occasionally need to subtract from index, also Circular Access q f r
Array-based D EQUE 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
Array-based D EQUE Operations 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
Your Turn Get into your groups and complete activity
For Next Lecture Midterm #2 Midterm #2 will be in class on Wednesday