







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Name / Group ID: ________________________ Reg. No: ___________________ Class / Section: __________________________ Date: _____________________ Time Required : 3 hrs Programming Language : C++ Software Required : Dev - C++ Hardware Required : Computer System Circular Queue ADT Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we can not insert the next element even if there is a space in front of queue.
Operations on Circular Queue: Front: Get the front item from queue. Rear: Get the last item from queue. enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position. Steps:
while (f <= r){ cout<<cqueue[f]<<" "; f++; } } else { while (f <= n - 1) { cout<<cqueue[f]<<" "; f++; } f = 0; while (f <= r) { cout<<cqueue[f]<<" "; f++; } } cout<<endl; } int main() { insertCQ(15); insertCQ(20); insertCQ(25); displayCQ(); deleteCQ(); displayCQ(); insertCQ(35); displayCQ(); deleteCQ(); displayCQ(); insertCQ(50); displayCQ(); return 0; } Output
Deque Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.In previous post we had discussed introduction of deque. Now in this post we see how we implement deque Using circular array. Operations on Deque: Mainly the following four basic operations are performed on queue: insetFront(): Adds an item at the front of Deque. insertRear(): Adds an item at the rear of Deque. deleteFront(): Deletes an item from front of Deque. deleteRear(): Deletes an item from rear of Deque. In addition to above operations, following operations are also supported getFront(): Gets the front item from queue. getRear(): Gets the last item from queue. isEmpty(): Checks whether Deque is empty or not. isFull(): Checks whether Deque is full or not. Circular array implementation deque For implementing deque, we need to keep track of two indices, front and rear. We enqueue(push) an item at the rear or the front end of qedue and dequeue(pop) an item from both rear and front end. Working
Delete Element From Rear end a). first Check deque is Empty or Not b). If deque has only one element front = - 1 ; rear =-1 ; Else IF Rear points to the first index of array it's means we have to move rear to points last index [ now first inserted element at front end become rear end ] rear = size-1 ; Else || decrease rear by '1' rear = rear-1; Delete Element From Front end a). first Check deque is Empty or Not b). If deque has only one element front = - 1 ; rear =-1 ; Else IF front points to the last index of the array it's means we have no more elements in array so we move front to points first index of array front = 0 ; Else || increment Front by '1' front = front+1;
Deque Implementation: //C++ program to implement a Deque using circular #include
// Deque has only one element if (front == rear) { front = - 1; rear = - 1; } else if (rear == 0) rear = size-1; else rear = rear-1; } // retrieve front element of Deque int getFront() { if (isEmpty()) { cout << " Underflow!!\n" <<endl; return - 1 ; } return array[front]; } // retrieve rear element of Deque int getRear() { if(isEmpty() || rear < 0) { cout << " Underflow!!\n" << endl; return - 1 ; } return array[rear]; } //main program int main() { //Deque dq(5); cout <<"Insert element 1 at rear end \n"; insertrear(1); cout << "insert element 3 at rear end \n"; insertrear(3); cout << "rear element of deque " << " " << getRear() << endl; deleterear(); cout <<"After deleterear, rear = " << getRear() << endl; cout << "inserting element 5 at front end \n"; insertfront(5); cout << "front element of deque " << " "<<getFront() << endl;
deletefront(); cout<< "After deletefront, front = " <<getFront() << endl; return 0; } Output
// Return starting point return start; } // Driver code int main() { struct petrolPump arr[] = {{6, 4}, {3, 6}, {7, 3}}; int n = sizeof(arr)/sizeof(arr[0]); int start = printTour(arr, n); if(start == - 1) cout<<"No solution"; else cout<<"Start = "<<start; return 0; }