210 likes | 258 Views
CSC 248 – fundamentals of Data structure. CHAPTER 4 – QUEUE. PREPARED BY : ZANARIAH IDRUS. CHAPTER OBJECTIVES. Understanding Queue Operations on Queue Applications of Queue. APPLICATIONS OF QUEUES. Scheduler For controlling access to shared resources Maintain queues for printers’ jobs
E N D
CSC 248 – fundamentals of Data structure CHAPTER 4 – QUEUE PREPARED BY : ZANARIAH IDRUS
CHAPTER OBJECTIVES • Understanding Queue • Operations on Queue • Applications of Queue
APPLICATIONS OF QUEUES • Scheduler • For controlling access to shared resources • Maintain queues for printers’ jobs • Maintains queues of disk input/output requests • Simulation of real world situations • Use queues to simulate waiting line queues in real scenarios • Public places such as bank, service counter and ticket counter (bus, train, flight-checki-n) • Telephone operators • Queuing system for handling calls to toll-free numbers
QUEUE • Represents a waiting line. • First-in First-out (FIFO) data structure. • Nodes are removed only from the head of the queue, and are inserted only at the tail of the queue. • Insert operation is known as ENQUEUE (enQ) • Remove operation is known as DEQUEUE (deQ)
ENQUEUE • Insert operation is known as ENQUEUE.
DEQUEUE • Remove operation is known as DEQUEUE. • It remove an element from the front of the queue, provided the queue is not Empty
OPERATIONS ON QUEUE • initializeQueue: Initializes the queue to an empty state. • isEmptyQueue: Determines whether the queue is empty. If the queue is empty, it returns the value true; otherwise, it returns the value false. • isFullQueue: Determines whether the queue is full. If the queue is full, it returns the value true; otherwise, it returns the value false. • getFront: Returns the front (first) element of the queue; the queue must exist. • back: Returns the rear (last) element of the queue; the queue must exist. • addQueue: Adds a new element to the rear of the queue; the queue must exist and must not be full. • deleteQueue: Removes the front element of the queue; the queue must exist and must not be empty.
OPERATIONS ON QUEUE • Searching – search a particular element in queue (check and get the elements using dequeue method • Queue must be dequeue (remove data ) in order to traverse the queue • Removed data from queue must be keep and send into temporary queue if the data need to be used again • In queue, the order of the data will not change after and inserting process using temporary queue
QUEUE CLASS DEFINITION • Class: Queue extends LinkedList class • Methods: • Constructors (default) • Enqueue (object) // insert element at the end of queue • Dequeue ( ) // remove element from front of queue • getFront ( ) // get the front element • getEnd ( ) // get the end element • isEmpty ( ) // check if queue is Empty (inherit from class LinkedList)
INITIALIZE QUEUE public void initializeQueue() { for(inti = queueFront; i < queueRear; i = (i + 1) % maxQueueSize) list[i] = null; queueFront= 0; queueRear= maxQueueSize -1; count = 0; }
EMPTY QUEUE & FULLQUEUE public booleanisEmptyQueue() { return (count == 0); } public booleanisFullQueue() { return (count == maxQueueSize); }
FRONT public Object front() throws QueueUnderflowException { if(isEmptyQueue()) throw new QueueUnderflowException(); Object temp = list[queueFront]; return temp; }
BACK public Object back() throws QueueUnderflowException { if(isEmptyQueue()) throw new QueueUnderflowException(); Object temp = list[queueRear]; return temp; }
ADD QUEUE public void addQueue(Object queueElement) throws QueueOverflowException { if(isFullQueue()) throw new QueueOverflowException(); queueRear= (queueRear + 1) % maxQueueSize; //use the mod //operator to advance queueRear //because the array is circular count++; list[queueRear] = queueElement; }
DELETE QUEUE public void deleteQueue() throws QueueUnderflowException { if(isEmptyQueue()) throw new QueueUnderflowException(); count--; list[queueFront] = null; queueFront = (queueFront + 1) % maxQueueSize; //use the mod //operator to advance queueFront //because the array is circular }
OTHERS APPLICATIONS OF QUEUE • Simulation: technique in which one system models the behavior of another system; used when it is too expensive or dangerous to experiment with real systems. • Simulationexamples: • Wind tunnels used to experiment with design of car bodies • Flight simulators used to train airline pilots • Computer simulations: objects being usually represented as data