310 likes | 434 Views
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 .”
E N D
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)
CSC 212 – Data Structures Lecture 25: DeQues
Stack Memory Aid Roses are red andviolets are blue Implementpush,peek,&pop And you’re a Stack too!
Stack Interface public interface Stack<E> extends Collection {public Epeek()throws EmptyCollectionException;public Epop() throws EmptyCollectionException;public void push(E element); }
Queue Memory Aid It’s hard writing rhymes with enqueue, dequeue, and first
Queue ADT public interface Queue<E> extends Collection {public Efirst()throws EmptyCollectionException;public Edequeue() throws EmptyCollectionException;public void enqueue(E element); }
Stacks vs. Queues • Access data with Stack in LIFO order • LastIn-First Out ordering is unfair (unless late) • Data accessed in Queue using FIFO order • FirstIn-First Out is first-come, first-served
Stacks vs. Queues • Access data with Stack in LIFO order • LastIn-First Out ordering is unfair (unless late) • Data accessed in Queue using FIFO order • FirstIn-First Out is first-come, first-served Order read if Queue
Stacks vs. Queues • Access data with Stack in LIFO order • LastIn-First Out ordering is unfair (unless late) • Data accessed in Queue using FIFO order • FirstIn-First Out is first-come, first-served Order read if Queue Order read if Stack
Still Have Limits • 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 • Queuelimits how each side used • Front provides access& removal of elements • Must use the Queue’s end to add elements
Deque ADT • Pronounced “deck” (like on a house) • Mnemonic for Double Ended QUEue • dequeue ≠ dequeand do not sound alike • Structure that provides access to both ends • Combines Stack & Queueconcepts • Is also able to add elements to start
Deque Interface public interface Deque<E> extends Collection { /* first()*/ public EpeekFirst()throws EmptyCollectionException; /* peek()*/ public EpeekLast()throws EmptyCollectionException; /* dequeue()*/ public EremoveFirst()throws EmptyCollectionException; /* pop()*/ public EremoveLast()throws EmptyCollectionException; /* push() or enqueue()*/ public addLast(Eelem); /* brand new method!*/ public addFirst(Eelem); }
Linked-list based Deque • 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 rear head
Linked-list based Deque • 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 rear head retVal
Linked-list based Deque • 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 rear head retVal
Linked-list based Deque • 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 rear head retVal
Linked-list based Deque • 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 rear head retVal
Linked-list based Deque • 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 rear head newElem
Linked-list based Deque • 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 rear head newNode newElem
Linked-list based Deque • 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 rear head newNode newElem
Linked-list based Deque • 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 rear head newNode newElem
Linked-list based Deque • 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 rear head
Why Doubly-Linked? • Did you see why doubly-linked list is important? rear head
Why Doubly-Linked? • 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 rear head
Why Doubly-Linked? • 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 • Dequemovesforward&backward; needs dual links rear head
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 #2will be in class on Monday