You are on page 1of 22

CHAPTER 4 : QUEUE

4.1 41 Introduction to Queue I t d ti t Q 4.1.1 What is Queue? 4.1.2 The uses of Queue 4.1.3 Basic Operations of Queue

4.2

Queue Implementation and Application : 4.2.1 Implementation 4.2.2 Application 4 2 2 A li ti

Intersession May 2009

Introduction to Queue
What is Queue?
Queue i a collection of items with first in first out Q is ll i fi i h fi i fi retrieval Example : p queue in bank services the person at the beginning of the line first and last person to come in must queue at end of line New items can be added at the end of queue and removed f d from the f h front of queue f Q Queue is referred as a First-in, First-out data structure (FIFO).
Intersession May 2009 2

Introduction to queue (cont)


Queue can be implemented using linked list or sequential list In this course, self-defined Class Queue is using a course self defined constrained version of a linked list, new node can be added at end of list and removed from the front of the list. Q Queue using linked list structure dynamic data structure g y where elements are stored anywhere in memory
Front node
Intersession May 2009

end
3

The uses of Queue (advantages)


Computer with single processor only one applications can be serviced at a time, each application requires processor ti time to be placed i a queue. t b l d in Support spooling in printing single printer shared by all users in a network, each printing job are placed in a queue until printer becomes available. Information packets in computer networks routing nodes send one packet at a time and must be in queue File server in a computer network - file access / request from clients are placed in a queue.
Intersession May 2009 4

Basic operations of Queue


enqueue (enQ) adds new element to the end of queue. dequeue (deQ) removes element from front of queue and returns the data from the removed node. isEmpty checks whether the queue is empty or not.

Intersession May 2009

Queue Implementation
Queue using LinkedList Structure
Class : Attributes: LinkedList first node // represent top last node ast ode // represent bottom ep esent current node // use to traverse the list

Methods : Constructor (default & normal) isEmpty() // check whether list is empty insertAtBack (object) // insert at the end of list removeFromFront() // delete element from front of list getFirst() // get the first node

Intersession May 2009

Queue implementation (cont)


Class Queue Design
Class : Queue extends LinkedList class Methods : Constructor (default) enqueue (object) // insert element at the end of q queue dequeue () // remove element from front of queue getFront () // get the front element getFront () // get the end element isEmpty() // check if queue is Empty (inherit from class LinkedList
Intersession May 2009 7

Queue Implementation(cont.)
// Class Queue definition public class Queue extends LinkedList{ public Queue() { } // constructor public void enqueue( Object elem) { insertAtBack (elem); } public Object dequeue ( ) { return removeFromFront(); } public Object getFront() { return getFirst(); } public Object getEnd() { Object O = removeFromBack(); insertAtBack(O); return O; } } // end Queue
Intersession May 2009 8

Queue Application
Application using Queue with primitive type
// Queue with primitive data type Q p yp import javax.swing.*; public class primitiveQueue { public static void main (String [] arg) { // create a queue instances Queue objQ = new Queue (); // create objects to be stored into the queue j q String a = 100; String b= 200; String c = 300; 300 ;
Intersession May 2009 9

Queue Application (cont.) (cont.)


// check the content of queue if (objQ isEmpty()) (objQ.isEmpty()) System.out.println(Queue is Empty); else System.out.println(Queue is Not Empty); // insert elements at end of queue q objQ.enqueue (a); objQ.enqueue (b); objQ.enqueue objQ enqueue (c);

Intersession May 2009

10

Queue Application (cont.) (cont.)


// to display the front data System.out.println( Top : System.out.println(Top : + objQ.getFront().toString()); // to store data into queue as many as the user wants int more = 1; while (more == 1) { String number = JOptionPane.showInputDialog (Enter number:); objQ.enqueue (number); int more = Integer.parseInt (JOptionPane. showInputDialog (More number?(1-yes, 0,no)?:); }
Intersession May 2009 11

Queue Application (cont.) (cont.)


// to print content of queue using independent function printQueue (objQ); // to calculate the total numbers of data in queue and insert data into temporary queue for reuse Object numobj; Queue tempQ = new Queue (); // temporary queue int sum=0, num; while(!objQ.isEmpty()) ( j p y()) { numobj = objQ.dequeue ();

// objQ will be empty at end of operation num = Integer.parseInt (numobj().toString()); sum +=num; tempQ.push (numobj); // same as original order

} System.out.println(Total is: S stem o t println(Total is + s m) sum);


Intersession May 2009 12

Queue Application (cont.) (cont.)


// to calculate total numbers in queue int sum=0 num; sum=0, while(!tempQ.isEmpty()) { num = Integer.parseInt(tempQ.dequeue().toString()); sum +=num; } System.out.println(Total is: + sum); }//end main

Intersession May 2009

13

Queue Application (cont.) (cont.)


// independent function to print contents of queue and retain numbers in original queue in original order public static void printQueue (Queue q) { Object numobj; Queue tQ = new Queue(); // temporary queue while(!q.isEmpty()) { numobj = q.dequeue(); System.out.println ( Num: System out println (Num: + numobj toString()); numobj.toString()); // insert number into temp queue will be in same order as original tQ.enqueue (numobj); } } } // end of class primitiveQueue

Intersession May 2009

14

Queue Application (cont.) (cont.)


Application using Queue with ADT
p public class Student // student object j { private String metric; private double gpa; public Student() { metric = "0000"; 0000 ; gpa = 0.0; } public Student(String m,double gp) { metric = m; gpa = gp; } public String Display( ) { String out=""; out += "\nMetric No:" + metric + " Gpa:" + gpa; \nMetric No: Gpa: return out;} public String theMetric( ){ return metric;} public double theGpa( ){ return gpa;} } // end of class student
Intersession May 2009 15

Queue Application (cont.) (cont.)


// use self-defined class queue import javax.swing.*; public class St d tQ bli l StudentQueue { public static void main (String [ ] arg) { Queue objQ = new Queue(); Queue objQ1 = new Queue(); for (int x=0; x <4; x++) { String metric = JOptionPane.showInputDialog( Enter student metric:); String sgpa = JOptionPane.showInputDialog( Enter student gpa:); double gpa = Double parseDouble(sgpa); Double.parseDouble(sgpa); Student stu = new Student(metric,gpa); objQ.enqueue (stu); objQ1.enqueue (stu); }
Intersession May 2009 16

Queue Application (cont.) (cont.)


// Calculate the average of gpa double average1 = calcAverage1(objQ); // Calculate the average of gpa and return queue through parameter Queue nQueue = new Queue(); (); double average2 = calcAverage2(objQ1, nQueue); String out = Average1 is : + average1 + Average2 is : average2; JOptionPane.showMessageDialog(null, out,Result, JOptionPane.PLAIN_MESSAGE); JO ti P PLAIN MESSAGE)

Intersession May 2009

17

Queue Application (cont.) (cont.)


// Determine the best student Student bestStudent = findBestStudent(nQueue); String out1 = \n Best student is : + bestStudent.Display(); S p y(); out += out1; JOptionPane.showMessageDialog(null, out,Result, JOptionPane.PLAIN_MESSAGE); }//end }// d main i

Intersession May 2009

18

Queue Application (cont.) (cont.)


// independent function to calculate the average public static double calcAverage1(Queue que) (Q ) { double sumgpa=0; int totalStu=0; Student data; while(!que.isEmpty()) { data = (Student) que.dequeue(); sumgpa += data theGpa(); data.theGpa(); totalStu++; } return sumgpa/totalStu; gp ; } // end calcAverage

Intersession May 2009

19

Queue Application (cont.) (cont.)


// independent function to calculate the average and pass new queue through parameter public static d bl calcAverage2 (Q bli i double l A 2 (Queue que, Q Queue nque) ) { double sumgpa=0; int totalStu=0; Student data; while(!que.isEmpty()) { data = (Student) que.dequeue (); sumgpa += data theGpa(); data.theGpa(); totalStu++; nque.enqueue (data); } return sumgpa/totalStu; } // end calcAverage

Intersession May 2009

20

Queue Application (cont.) (cont.)


// independent function to determine the best student public static Student findBestStudent (Queue queue) { Student b t St d t best = new Student(); St d t() Student data = (Student) que.getFirst(); double bestgpa = data.theGpa(); while (!que.isEmpty()) (!que isEmpty()) { data = (Student) que.dequeue(); if (data.theGpa() > bestgpa) (data theGpa() { bestgpa = data.theGpa(); best = data; } } return best; } // end findBestStudent } // end class StudentStack
Intersession May 2009 21

Queue Application (cont)


Searching search a particular element in queue (check and get the elements (using dequeue method). g g q Manipulating do some operation on Queue (Example : calculate average, determine maximum & minimum and etc.) Important thing about application using queue is that queue must be dequeue ( b d (remove data) i order t traverse th queue d t ) in d to t the It is important to keep removed data from queue into temporary queue if the data need to be used again In queue, the order of the data will not change after and inserting p process using temporary q g p y queue
Intersession May 2009 22

You might also like