problem of the day bezout acquired 19 camels through his trading skill, “of the collected...
TRANSCRIPT
![Page 1: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/1.jpg)
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?
![Page 2: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/2.jpg)
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)
![Page 3: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/3.jpg)
LECTURE 26: DEQUES
CSC 212 – Data Structures
![Page 4: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/4.jpg)
Stack Memory Aid
Roses are red and violets are blue
Implement push, pop , & top
And you’re a Stack too!
![Page 5: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/5.jpg)
Stack Interface
public interface Stack<E> extends Collection {public E top() throws EmptyStackException;
public E pop() throws EmptyStackException;
public void push(E element);
}
![Page 6: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/6.jpg)
Queue Memory Aid
![Page 7: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/7.jpg)
Queue Memory Aid
It’s hard writing rhymes with
enqueue, dequeue, and
front
![Page 8: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/8.jpg)
Queue Memory Aid
![Page 9: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/9.jpg)
public interface Queue<E> extends Collection {
public E front() throws EmptyQueueException;
public E dequeue() throws EmptyQueueException;
public void enqueue(E element);
}
Queue ADT
![Page 10: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/10.jpg)
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 order
First In-First Out is first-come, first-served
![Page 11: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/11.jpg)
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 order
First In-First Out is first-come, first-served
Ord
er r
ead
if
Qu
eue
![Page 12: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/12.jpg)
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 order
First In-First Out is first-come, first-served
Ord
er r
ead
if
Qu
eue O
rder read
if Stack
![Page 13: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/14.jpg)
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
![Page 15: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/15.jpg)
public interface Deque<E> 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
![Page 16: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/16.jpg)
VIPs versus Losers
![Page 17: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/17.jpg)
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
![Page 18: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/19.jpg)
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
![Page 20: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/20.jpg)
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
![Page 21: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/21.jpg)
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
![Page 22: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/22.jpg)
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
![Page 23: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/23.jpg)
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
![Page 24: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/24.jpg)
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
![Page 25: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/25.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qfr
![Page 26: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/26.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qf r
![Page 27: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/27.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qf r
![Page 28: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/28.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qf r
![Page 29: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/29.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qf r
![Page 30: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/30.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qf r
![Page 31: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/31.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qfr
![Page 32: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/32.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qfr
![Page 33: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/33.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qfr
![Page 34: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/34.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qfr
![Page 35: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/35.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qf r
![Page 36: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/36.jpg)
DEQUES, like QUEUES, have both ends move addFirst & removeFirst moves its front
index rear index 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
qf r
![Page 37: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/37.jpg)
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
![Page 38: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/38.jpg)
Array-based DEQUE 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
![Page 39: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/39.jpg)
Your Turn
Get into your groups and complete activity
![Page 40: 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](https://reader033.vdocument.in/reader033/viewer/2022051621/56649f045503460f94c183ae/html5/thumbnails/40.jpg)
For Next Lecture
Midterm #2 will be in class on Wednesday