Data Structures Queue Namiq Sultan 1 Queue • A queue is an ordered collection of items into which items may be added at one end (rear) and from which items may be deleted form the end (front). • A queue is logically a first in first out (FIFO) linear data structure. front rear 2 Queue Representing queues const MAX = 8; struct queue{ int items[MAX]; int front, rear; } q; q.items[7] Queue Basic Operations add(q, x) Adds item x at the rear of the queue q q q.items[0] x = remove(q) Removes the front element from the queue q. q.front q.rear 3 Queue Demonstration q.front =0; q.rear = -1; Initial conditions 7 add(q,4); add(q, 7); add(q, 3); 6 x=remove(q); y=remove(q); 3 9 2 3 add(q, 9); 1 7 0 4 5 4 q.front q.rear 201 1 0 -1 32 4 Important !!!!! If q.rear==MAX-1 we cannot add elements even though there are free cells in the front of the queue. What will we do? 5 Queue This problem can be solved by one of the following: 1. Shifting the elements whenever q.rear equals MAX-1 and there are empty cells in the front of the queue. 2. Converting this linear queue to a circular queue. 6 Circular Queue • To add an element, the element is inserted in first location of the queue. q.rear • It behaves like a circular list. Add(q, 5) q.front 7 2 6 4 5 7 4 3 3 9 2 1 q.rear 0 5 7 Circular Queue • The previous initial condition: q.fron = 0 q.rear 7 q.rear = -1 6 is not suitable here. 5 • It is difficult to differentiate between an empty queue and full queue. 4 3 2 1 q.front 0 8 Circular Queue • Solution: Make front points to the element immediately preceding the first item. • Initial condition: front = rear = 0; • The following conditions are satisfied: Queue is empty if: front == rear Queue is full if front == rear + 1 OR front == 0 && rear == MAX-1 9 Program 5.1 //PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY #include <process.h> #include <iostream> #using namespace std; const MAX = 10; class c_queue{ int cq_arr[MAX]; int front,rear; public: //a constructor is created to initialize the variables c_queue() { front= 0; rear = 0; } //public function declarations void insert(); void del(); void display(); }; 10 Program 5.1 void c_queue::insert() { int added_item; //Checking for overflow condition if ((front == 0 && rear == MAX-1) || (front == rear +1)) { cout<<"\nQueue Overflow \n"; return; } else if (rear == MAX-1) /*rear is at last position of queue */ rear = 0; else rear = rear + 1; cout<<"\nInput the element for insertion in queue:"; cin>>added_item; cq_arr[rear] = added_item; }/*End of insert()*/ 11 Program 5.1 //This function will delete an element from the queue void c_queue::del() { //Checking for queue underflow if (front == rear) { cout<<"\nQueue Underflow\n"; return; } else if (front == MAX-1) front = 0; else front = front + 1; cout<<"\nElement deleted from queue is:"<<cq_arr[front]<<"\n"; }/*End of del()*/ 12 Program 5.1 // Display the elements in the queue void c_queue::display() { int front_pos = front, rear_pos = rear; if (front == rear) { // is empty or not cout<<"\nQueue is empty\n"; return; } cout<<"\nQueue elements:\n"; //Displaying the queue elements if (front_pos < rear_pos ) while(front_pos < rear_pos){ cout<<cq_arr[++front_pos]<<", "; } else{ while(front_pos < MAX-1){ cout<<cq_arr[++front_pos]<<", "; } front_pos = -1; while(front_pos < rear_pos) { cout<<cq_arr[++front_pos]<<", "; } } /*End of else*/ cout<<"\n"; } /*End of display() */ 13 Program 5.1 void main() { int choice; c_queue co; //Creating the object while(1){ //Menu options cout <<"\n1. Insert\n"; cout <<"2. Delete\n"; cout <<"3. Display\n"; cout <<"4. Quit\n"; cout <<"\n Enter your choice: "; cin>>choice; switch(choice){ case 1: co.insert(); break; case 2 : co.del(); break; case 3: co.display(); break; case 4: exit(1); default: cout<<"\nWrong choice\n"; } /*End of switch*/ } /*End of while*/ } /*End of main()*/ 14 APPLICATIONS OF QUEUE 1. Round robin techniques for processor scheduling is implemented using queue. 2. Printer server routines (in drivers) are designed using queues. 3. All types of customer service software (like Railway/Air ticket reservation) are designed using queue to give proper service to the customers. 15