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.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Chapter 24 Lists, Stacks, and Queues
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CS Data Structures II Review COSC 2006 April 14, 2017
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
List Interface and Linked List Mrs. Furman March 25, 2010.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Welcome to CSCE 221 – Data Structures and Algorithms
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Elementary Data Structures
Week 4 - Monday CS221.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Linked List Stacks, Linked List Queues, Dequeues
Csc 2720 Data structure Instructor: Zhuojun Duan
Circular queue.
Queue.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Stacks and Linked Lists
Data Structures & Programming
Queues, Deques, and Priority Queues
Presentation transcript:

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