The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.

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

Data Structures ADT List
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.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
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.
The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
CHAPTER 7 Queues.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structure Dr. Mohamed Khafagy.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
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?
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Concepts of Algorithms CSC-244 Unit 11 & 12 Sorting (Radix Sort) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Linked Data Structures
Advanced Sorting 7 2  9 4   2   4   7
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.
September 29 – Stacks and queues
Chapter 5 Ordered List.
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
CSCE 3110 Data Structures & Algorithm Analysis
Introduction to Data Structure
Topic 16 Queues "FISH queue: n.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 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.
Chapter 5 Ordered List.
Queues.
Queue.
Describing algorithms in pseudo code
Advanced Sorting Methods: Shellsort
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures ADT List
CSC 143 Queues [Chapter 7].
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
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 (張智星)
ADT Queue (Array Implementation)
Sorting and Searching -- Introduction
Getting queues right … finally (?)
A type is a collection of values
Data Structures & Programming
Presentation transcript:

The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front. This strategy is known as first-in-first-out (FIFO) strategy. Operations (methods) on queues: enqueue (item) Inserts item at the rear of the queue dequeue () Removes the item from the front of the queue size () Returns the number of items in the queue empty () Returns true if the queue is empty full () Returns true if the queue is full front () Returns the item at the front of the queue without removing it from the queue.

The Queue Interface in two versions public interface QueueEx { public void enqueue (int item) throws QueueFullException; public int dequeue() throws QueueEmptyException; public int size(); public boolean empty(); public boolean full(); public int front() throws QueueEmptyException; } version 1: public interface Queue { public void enqueue (int item); public int dequeue(); public int size(); public boolean empty(); public boolean full(); public int front(); }

The Queue ADT -- an array implementation (version 1) class QueueADT implements Queue { final int MAXSIZE = 100; private int size; private int[] queueADT; private int front = 0; private int rear = -1; public QueueADT () { size = MAXSIZE; queueADT = new int[size]; } public QueueADT (int inputsize) { size = inputsize; public boolean empty () { return (rear < front); public boolean full () { return (rear == size - 1); public void enqueue (int number) { rear++; queueADT[rear] = number; } public int dequeue () { int i = queueADT[front]; front++; return i; public int front () { return queueADT[front]; public int size () { return (rear + 1 - front);

The Queue ADT -- an array implementation (version 2) class QueueEmptyException extends Exception { public QueueEmptyException (String message) { System.out.println (message); } } class QueueFullException extends Exception { public QueueFullException (String message) { class QueueADTEx implements QueueEx { final int MAXSIZE = 100; private int size; private int[] queueADT; private int front = 0; private int rear = -1; public QueueADTEx () { size = MAXSIZE; queueADT = new int[size]; } public QueueADTEx (int inputsize) { size = inputsize; public boolean empty () { return (rear < front); } public boolean full () { return (rear == size - 1); } public void enqueue (int number) throws QueueFullException { if (full()) throw new QueueFullException ("The queue is full."); rear++; queueADT[rear] = number; } public int dequeue () throws QueueEmptyException { if (empty()) throw new QueueEmptyException ("The queue is empty."); int i = queueADT[front]; front++; return i; public int front () throws QueueEmptyException { return queueADT[front]; public int size () { return (rear + 1 - front); } }

Example application of the Queue ADT using version 1 import java.util.Scanner; class QueueAppl { public static void main (String[] args) throws IOException { Scanner scan = new Scanner (System.in); System.out.print ("Enter queue size: "); int size = scan.nextInt(); QueueADT queue = new QueueADT(size); int i = 2; while (!queue.full()) { queue.enqueue(i); System.out.println (queue.front() + " is the front element."); i = i + 2; } System.out.println ("The current queue contains " + queue.size() + " elements."); while (!queue.empty()) System.out.println (queue.dequeue() + " is dequeued from the queue."); if (queue.empty()) System.out.println ("The queue is empty."); else System.out.println ("There are more elements on the queue."); } }

Example application of the Queue ADT using version 2 import java.util.Scanner; class QueueApplEx { public static void main (String[] args) throws IOException { Scanner scan = new Scanner (System.in); System.out.print ("Enter queue size: "); int size = scan.nextInt (); QueueADTEx queue = new QueueADTEx (size); int i = 2; try { for (int j = 1; j <= 7; j++) { queue.enqueue(i); System.out.println (queue.front() + " is the front item."); i = i + 2; } } catch (QueueFullException e) { System.out.println ("The queue is full."); } catch (QueueEmptyException e) { System.out.println ("The queue is empty."); } System.out.println ("The current queue contains " + queue.size() + " elements."); System.out.println (queue.dequeue() + " dequeued"); System.out.println ("The queue is empty."); } } }

Radix sort: another application of the Queue ADT Sorting methods which utilize digital properties of the numbers (keys) in the sorting process are called radix sorts. Example. Consider the list 459 254 472 534 649 239 432 654 477 Step 1: ordering wrt ones Step 2: ordering wrt tens Step 3: ordering wrt hundreds 0 0 0 1 1 1 2 472 432 2 2 239 254 3 3 432 534 239 3 4 254 534 654 4 649 4 432 459 472 477 5 5 254 654 459 5 534 6 6 6 649 654 7 477 7 472 477 7 8 8 8 9 459 649 239 9 9 After step 1: 472 432 254 534 654 477 459 649 239 After step 2: 432 534 239 649 254 654 459 472 477 After step 3: 239 254 432 459 472 477 534 649 654

Radix Sort: the algorithm Consider the following data structures: a queue for storing the original list and lists resulting from collecting piles at the end of each step (call these master lists); ten queues for storing piles 0 to 9; Pseudo code description of radix sort at the “idea” level: start with the one’s digit; while there is still a digit on which to classify data do { for each number in the master list do { add that number to the appropriate sublist } for each sublist do { for each number from the sublist do { remove the number from the sublist and append it to a newly arranged master list advance the current digit one place to the left

Radix Sort: the algorithm (cont.) Here is a more detailed pseudo code description of the radix sort: Input: A queue Q of N items Output: Q sorted in ascending order Algorithm RadixSort (Q, N): digit := 1 while StillNotZero (digit) do { for (i := 1 to 10) do { create (sublist[i]) } while (! empty Q) do { dequeue (Q, item) pile := getPile (item, digit) + 1 O(N) enqueue (sublist[pile], item) swaps this outer loop will execute } “digit” times reinitialize (Q) for (j :=1 to 10) do { while (! empty sublist(j)) do { dequeue (sublist[j], item) O(N) enqueue (Q, item) } swaps } digit := digit * 10

Efficiency of the Radix Sort Operations that affects the efficiency of radix sort the most are “dequeue- enqueue” swaps from and to the master list. Because the outer while-loop executes C times, and each of the inner loops is O(N), the total efficiency of radix sort is O(C*N). Notes: 1. If no duplicates are allowed in the list, we have log10 N <= C for non-negative integers. 2. If there is a limit on the number of digits in the integers being sorted, we have C <= H * log10 N. Therefore, radix sort is O(N * log N) algorithm if unique values are sorted; otherwise it is O(N) algorithm with a constant of proportionality, C, which can be large enough to make C * N > N * logN even for large N. A disadvantage of radix sort is that it required a large amount of memory to keep all of the sub-lists and the master list at the same time.

About the getPile method The getPile method must return an appropriate isolated digit from the number currently considered. That digit + 1 yields the sublist, where the number is to be enqueued. A possible implementation of the getPile method is the following: int getPile (int number, int digit) { return (number % (10 * digit) / digit); } Examples: number = 1234 digit = 100 (1234 % 1000) / 100 = 2 number = 12345 digit = 1 (12345 % 10) / 1 = 5