problem of the day
DESCRIPTION
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 .” - PowerPoint PPT PresentationTRANSCRIPT
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 gets 10 (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)
LECTURE 25: DEQUES
CSC 212 – Data Structures
Stack Memory Aid
Roses are red and violets are blue
Implement push, peek, & pop
And you’re a Stack too!
Stack Interface
public interface Stack<E> extends Collection {public E peek() throws EmptyCollectionException;public E pop() throws EmptyCollectionException;public void push(E element);
}
Queue Memory Aid
Queue Memory AidIt’s hard writing
rhymes with enqueue,
dequeue, and first
Queue Memory Aid
Queue ADT
public interface Queue<E> extends Collection {public E first() throws EmptyCollectionException;public E dequeue() throws EmptyCollectionException;public void enqueue(E element);
}
Stacks vs. Queues
Access data with Stack in LIFO order Last In-First Out ordering is unfair
(unless late) Data accessed in Queue using FIFO
orderFirst In-First Out is first-come, first-
served
Stacks vs. Queues
Access data with Stack in LIFO order Last In-First Out ordering is unfair
(unless late) Data accessed in Queue using FIFO
orderFirst In-First Out is first-come, first-
served
Ord
er re
ad if
Que
ue
Stacks vs. Queues
Access data with Stack in LIFO order Last In-First Out ordering is unfair
(unless late) Data accessed in Queue using FIFO
orderFirst In-First Out is first-come, first-
served
Ord
er re
ad if
Que
ueO
rder 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) 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<E> extends Collection { /* first() */ public E peekFirst() throws EmptyCollectionException;
/* peek() */ public E peekLast() throws EmptyCollectionException;
/* dequeue() */ public E removeFirst() throws EmptyCollectionException;
/* pop() */ public E removeLast() throws EmptyCollectionException;
/* push() or enqueue() */ public addLast(E elem);
/* brand new method! */ public addFirst(E elem);
}
Deque Interface
VIPs versus Losers
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
retVal
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 Move end node & set next/previous to
remove element
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
retVal
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
retVal
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
retVal
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
newElem
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
newElem
newNode
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
newElem
newNode
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
newElem
newNode
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 Move end node & set next/previous to
remove element
Linked-list based Deque
head rear
Did you see why doubly-linked list is important?
Why Doubly-Linked?
head rear
Did you see why doubly-linked list is important? New or next node aliased by Queue’s fields Stack set up so we only ever move to next
node
Why Doubly-Linked?
head rear
Did you see why doubly-linked list is important? New or next node aliased by Queue’s fields Stack set up so we only ever move to next
node Deque moves forward & backward; needs
dual links
Why Doubly-Linked?
head rear
Your Turn
Get into your groups and complete activity
For Next Lecture
Project #1 is due by 11:59PM tomorrow Can use 1 or both virtual extensions on this
project Check with me if uncertain how many
you’ve used
Midterm #2 will be in class on Monday