# 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.

## Presentation on theme: "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."— Presentation transcript:

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;  Queue Basic Operations  add(q, x) Adds item x at the rear of the queue q  x = remove(q) Removes the front element from the queue q. q q.rear q.front q.items[0] q.items[7] 3

Queue  Demonstration q.front =0; q.rear = -1; add(q,4); add(q, 7); add(q, 3); x=remove(q); y=remove(q); add(q, 9); q.rear q.front 0 1 2 3 4 5 6 7 0 4 01 2 7 3 12 3 9 Initial conditions 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 3 7 4 2 q.front 0 1 2 3 4 5 6 7 9 To add an element, the element is inserted in first location of the queue. It behaves like a circular list. Add(q, 5) q.rear 5 7777 7

Circular Queue q.rear q.front 0 1 2 3 4 5 6 7 The previous initial condition: q.fron = 0 q.rear = -1 is not suitable here. It is difficult to differentiate between an empty queue and full queue. 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 # 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:"< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151040/slides/slide_12.jpg", "name": "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: <

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151040/slides/slide_13.jpg", "name": "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<

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

Download ppt "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."

Similar presentations