Queues CSC212.

Slides:



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

Data Structures ADT List
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
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 and Priority Queues
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Stacks, Queues & Deques CSC212.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queue, Deque, and Priority Queue Implementations.
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Queues and Priority Queues
Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
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.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Linked Data Structures
Queues Chapter 8 (continued)
Queues Chapter 4.
CC 215 Data Structures Queue ADT
Queues Queues Queues.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Java collections library
Priority Queue.
Chapter 13 Queues and Priority Queues
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Queues.
Data Structures ADT List
Data Structures ADT List
Recitation 5 CS0445 Data Structures
Adapted from Pearson Education, Inc.
ADT list.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
A List Implementation That Links Data
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures ADT List
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
ADT Queue (Array Implementation)
More Data Structures (Part 1)
Queues: Implemented using Linked Lists
A List Implementation That Links Data
Queues.
Data Structures & Programming
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Queues CSC212

Specifications for the ADT Queue Queue organizes entries according to order of entry Exhibits first-in, first-out behavior Contrast to stack which is last-in, first-out All additions are at the back of the queue Front of queue has items added first Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue Fig. 23-1 Some everyday queues. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue View interface for a queue of objects Note methods enqueue dequeue getFront isEmpty clear Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue Fig. 23-2 Queue of strings after (a) enqueue adds Jim; (b) Jess; (c) Jill; (d) Jane; Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifications for the ADT Queue Fig. 23-2 Queue of strings after (e) enqueue adds Joe; (f) dequeue retrieves, removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves, removes Jess. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using a Queue to Simulate a Waiting Line Fig. 23-3 A line, or queue of people. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Java Class Library: The Interface Queue Similar to our QueueInterface Specifies more methods Methods provided Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

import java.util.LinkedList; import java.util.Queue; public class queu { public static void main(String[] args) { // Create and initialize a Queue using a LinkedList Queue<String> waitingQueue = new LinkedList<>(); // Adding new elements to the Queue (The Enqueue operation) waitingQueue.add("Rajeev"); waitingQueue.add("Chris"); waitingQueue.add("John"); waitingQueue.add("Mark"); waitingQueue.add("Steven"); System.out.println("WaitingQueue : " + waitingQueue); }

Output: WaitingQueue : [Rajeev, Chris, John, Mark, Steven] What is the output after executing the following line ? waitingQueue.remove(); Output: WaitingQueue : [Chris, John, Mark, Steven]

Queues Front Tail

Adding an item to a non-empty queue public void enqueue (T newEntry) { Node newNode = new Node (newEntry, null); if ( isEmpty()) firstNode = newNode; else lastNode.setNextNode(newNode); lastNode = newNode; } // end enqueue ONLY lastNode needs adjustment Adapted from Pearson Education, Inc.

Removing an item from a queue with more than one item ONLY firstNode needs adjustment Adapted from Pearson Education, Inc.

Removing last item from a queue public T dequeue () { T front = null; if (!isEmpty ()) front = firstNode.data; firstNode = firstNode.next; if (firstNode == null) lastNode = null; } // end if return front; } // end dequeue BOTH firstNode AND lastNode need adjustment Adapted from Pearson Education, Inc.

ADT Queue: Specification Elements: The elements are of a variable type <Type>. In a linked implementation elements are placed in nodes. public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } }

ADT Queue: Specification Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element is called the tail and least recently arrived element the front or head. Domain: the number of elements in the queue is bounded therefore the domain is finite. Type of elements: Queue

ADT Queue: Specification Operations: Method Enqueue (Type e) requires: Queue Q is not full. input: Type e. results: Element e is added to the queue at its tail. output: none. Method Serve (Type e) requires: Queue Q is not empty. input: results: the element at the head of Q is removed and its value assigned to e. output: Type e. Method Length (int length) input: results: The number of element in the Queue Q is returned. output: length.

ADT Queue: Specification Operations: Method Full (boolean flag). requires: input: results: If Q is full then flag is set to true, otherwise flag is set to false. output: flag.

ADT Queue (Linked Implementation) public class LinkQueue <Type> { private Node<Type> head, tail; private int size; /** Creates a new instance of LinkQueue */ public LinkQueue() { head = tail = null; size = 0; }

ADT Queue (Linked Implementation) public boolean full() { return false; } public int length (){ return size;

ADT Queue (Linked Implementation) public void enqueue (Type e) { if (tail == null){ head = tail = new Node(e); } else { tail.next = new Node(e); tail = tail.next; size++;

ADT Queue (Linked Implementation) public Type serve() { Node<Type> tmp; Type x; tmp = head; x = head.data; head = head.next; size--; if (size == 0) tail = null; return x; }

Priority Queue Each data element has a priority associated with it.Highest priority item is served first. Real World Priority Queues: hospital emergency rooms…most sick patients treated first, events in a computer system, etc. Priority Queue can be viewed as: View 1: Priority queue as an ordered list. View 2: Priority queue as a set.

ADT Priority Queue Specification: Elements: The elements are of type PQNode. Each node has in it a data element of variable type <Type> and priority of type Priority ( which could be int type). public class PQNode<T> { private T data; private Priority priority; public PQNode<T> next; public PQNode() { next = null; } public PQNode(T e, Priority p) { data = e; priority = p; }

ADT Priority Queue Structure: the elements are linearly arranged, and may be ordered according to a priority value, highest priority element is called the tail and least priority element the front or head. Domain: the number of nodes in the queue is bounded therefore the domain is finite. Type of elements: PriorityQueue

ADT Priority Queue Operations: Method Enqueue (Type e, Priority p) requires: PQ is not full. input: e, p. results: Element e is added to the queue according to its priority. output: none. Method Serve (Type e, Priority p) requires: PQ is not empty. input: results: the element at the head of PQ is removed and returned. output: e, p. Method Length (int length) input: results: The number of element in the PQ is returned. output: length.

ADT Priority Queue Operations: Method Full (boolean flag). requires: input: results: If PQ is full then flag is set to true, otherwise flag is set to false. output: flag.

ADT Priority Queue Array Implementation el 10 8 7 5 Head Tail el el el Linked Implementation Insert Where? el 10 8 7 5 Head Tail

ADT Priority Queue (Linked) public class LinkPQ<T> { private int size; private PQNode<T> head, tail; /* tail is of no use here. */ public LinkPQ() { head = tail = null; size = 0; } public int length (){ return size; }

ADT Priority Queue (Linked) public int length (){ return size; } public boolean full () { return false;

ADT Priority Queue (Linked) public void enqueue(T e, int pty) { PQNode<T> p, q, tmp; if ((size == 0) || (pty > head.Priority())) { tmp = new PQNode<T>(e, pty); tmp.next = head; head = tmp; } else { p = head; q = null; while ((p != null) && (p.Priority() > pty)) { q = p; p = p.next; } tmp.next = p; q.next = tmp; } }

ADT Priority Queue (Linked) public T serve (Priority pty){ T e = head.get_data(); pty.set_value(head.get_priority().get_value()); head = head.next; size--; return(e); }

ADT Priority Queue Implementations Array Implementation: Enqueue is O(n), Serve is O(1). Linked List: Enqueue is O(n), Serve is O(1). Heap: Enqueue is O(log n), Serve is O(log n)  Heaps to be discussed later.