CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Queues and Linked Lists
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.
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.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
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 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
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.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
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.
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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
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.
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.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Elementary Data Structures
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.
Queues Rem Collier Room A1.02
Stacks and Queues.
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks, Queues, and Deques
Stacks and Linked Lists
Presentation transcript:

CSC 212 Stacks & Queues

Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test them  Can make it much less work  Please, please, please use this as an excuse to practice your Java Next homework assignment is on web

Working Together Goal is for students to learn  Means mistakes are made, questions raised  Peers useful when learning material But the goal is for YOU to learn material Talk about homework, daily quizzes all you want  Leave conversation with only memories, nothing written, typed, dictated, displayed on a screen…  Wait at least 15 minutes before continuing with homework

Queues Elements may be inserted at any time  Only element which has been in queue the longest may be removed Items enqueued into rear of queue Items dequeued from front of queue cashier Front of line Dequeue here Back of line Enqueue here

Queue vs. Stack Stack follows LIFO policy  Last In-First Out  Objects removed in reverse order of addition Queue follows FIFO policy  First In-First Out  Objects removed in order they were added

Queue ADT Queue supports two key methods:  enqueue(obj): Insert obj at the rear of the queue  dequeue(): Returns and removes the object from front of queue; sends error when queue is empty

Queue ADT Queue also defines other methods:  size(): Number of objects in the queue  isEmpty(): Returns if the queue is empty.  front(): Return but do not remove object from front of queue; send error when queue is empty

Array-based Queue Queue uses array in circular manner Also specifies maximum size of queue The queue consists of:  N-element array, q  f, index of the front element  r, index of the rear element

Array-based Queue Pictorial

Array-Based Queue Pseudocode int size(): return (N - f + r) mod N boolean isEmpty(): return(f == r) Object front(): if isEmpty() then throw QueueEmptyException return q [f ]

Array-Based Queue Pseudocode Object dequeue(): if isEmpty() then throw QueueEmptyException temp  q[f] q[f]  null f  (f +1) mod N return temp

Array-Based Queue Pseudocode Object enqueue(obj): if size() = N - 1 then throw QueueFullException q[r]  obj r  (r + 1) mod N

Daily Quiz #1 Show the output (if any) and what q, f, & r would equal after each of the following calls (assume N = 5): enqueue(5) dequeue() enqueue(3) enqueue(4) enqueue(7) isEmpty() front() dequeue() enqueue(1) enqueue(2) size() enqueue(9)

Linked Lists Linked lists are dynamically allocated collection of nodes Pseudocode of Node class: public class Node { Object element; Node next; // constructors, get & set methods } next elem node

Adding to Linked Lists Maintain a “head” field/variable To insert new node into linked list  Create & initialize new node newNode  new Node(elem)  Set new node’s next field to current value of head newNode.setNext(head)  Assign head to the new node head  newNode

Deleting from Linked List Easy to delete head node  Save value of head returnMe  head  Move head to next node head  head.getNext()  Return previous head return returnMe

Using Linked Lists What data structure does a linked list resemble? What is the maximum size the linked list can hold?

Using Linked Lists What is complexity of insert and remove? How would you design isEmpty()? What is its complexity?

Using Linked List algorithm size Node trav  head int count = 0 while (trav != null) count++; trav  trav.getNext() endwhile return count What is this complexity?

Linked List and Queues What is needed to implement a queue with a linked list? How would we write this algorithm?

DLNode public class DLNode { /** prev links to the previous DLNode in the linked list */ protected DLNode prev; /** next links to the next DLNode in the linked list */ protected DLNode next; /** element is the data stored at this node */ protected Object element; // Not shown here: constructors, get & set // routines }

Deque Pronounced “deck”  Avoids mistaking it for dequeue method  Stands for “double ended queue” Enables inserting and removing items at both front and rear Uses doubly-linked list  So uses DLNode rather than Node

Deque ADT Defines the following methods:  insertFirst(Object e), insertLast(Object e)  Object removeFirst(), Object removeLast()  Object first(), Object last()  int size()  boolean isEmpty() What exceptions should be thrown?

Daily Quiz #2 Use the Queue interface, & Node, EmptyQueueException classes found on the quiz web page Implement a linked-list based queue  You do not need to do any javadoc documentation  But you can (it is worth 30% of programming assignment grades)!