Professional Documents
Culture Documents
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
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
end
3
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
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
10
// objQ will be empty at end of operation num = Integer.parseInt (numobj().toString()); sum +=num; tempQ.push (numobj); // same as original order
13
14
17
18
19
20