Presentation is loading. Please wait.

Presentation is loading. Please wait.

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


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:

1 Data Structures Queue Namiq Sultan 1

2 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

3 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

4 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 Initial conditions 4

5 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

6 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

7 Circular Queue q.front 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

8 Circular Queue q.rear q.front 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

9 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

10 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

11 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

12 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:"<

13 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<

14 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

15 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


Ads by Google