problem of the day

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 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?

Upload: brina

Post on 23-Feb-2016

27 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Problem of the Day

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

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

LECTURE 25: DEQUES

CSC 212 – Data Structures

Page 4: Problem of the Day

Stack Memory Aid

Roses are red and violets are blue

Implement push, peek, & pop

And you’re a Stack too!

Page 5: Problem of the Day

Stack Interface

public interface Stack<E> extends Collection {public E peek() throws EmptyCollectionException;public E pop() throws EmptyCollectionException;public void push(E element);

}

Page 6: Problem of the Day

Queue Memory Aid

Page 7: Problem of the Day

Queue Memory AidIt’s hard writing

rhymes with enqueue,

dequeue, and first

Page 8: Problem of the Day

Queue Memory Aid

Page 9: Problem of the Day

Queue ADT

public interface Queue<E> extends Collection {public E first() throws EmptyCollectionException;public E dequeue() throws EmptyCollectionException;public void enqueue(E element);

}

Page 10: Problem of the Day

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

Page 11: Problem of the Day

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

Page 12: Problem of the Day

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

Page 13: Problem of the Day

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

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

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

Page 16: Problem of the Day

VIPs versus Losers

Page 17: Problem of the Day

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

Page 18: Problem of the Day

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

Page 19: Problem of the Day

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

Page 20: Problem of the Day

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

Page 21: Problem of the Day

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

Page 22: Problem of the Day

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

Page 23: Problem of the Day

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

Page 24: Problem of the Day

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

Page 25: Problem of the Day

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

Page 26: Problem of the Day

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

Page 27: Problem of the Day

Did you see why doubly-linked list is important?

Why Doubly-Linked?

head rear

Page 28: Problem of the Day

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

Page 29: Problem of the Day

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

Page 30: Problem of the Day

Your Turn

Get into your groups and complete activity

Page 31: Problem of the Day

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