Docsity

Queue in C++ insertion deletion, Exercises of Programming Languages

Toggle sidebar
bg1
LAB TASK 6– Circular Queue and Implementation of Deque using
circular array
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Queue in C++ insertion deletion and more Exercises Programming Languages in PDF only on Docsity!

LAB TASK 6 – Circular Queue and Implementation of Deque using

circular array

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:

  1. Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).
  2. If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert element. deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. Steps:
  3. Check whether queue is Empty means check (front==-1).
  4. If it is empty then display Queue is empty. If queue is not empty then step 3.
  5. Check if (front==rear) if it is true then set front=rear= - 1 else check if (front==size-1), if it is true then set front=0 and return the element.

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

  1. Create an empty array ‘arr’ of size ‘n’ initialize front = - 1 , rear = 0 Inserting First element in deque, at either front or rear will lead to the same result.

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 using namespace std; #define MAX_size 10 // Maximum size of array or Dequeue int array[MAX_size]; int front = - 1; int rear=0; int size=5; // Operations on Deque: void insertfront(int key); void insertrear(int key); void deletefront(); void deleterear(); int getFront(); int getRear(); // Check if Deque is full bool isFull(){ return ((front == 0 && rear == size-1)||front == rear+1); } // Check if Deque is empty bool isEmpty(){ return (front == - 1); } // Insert an element at front of the deque void insertfront(int key) { if (isFull()) { cout <<"Overflow!!\n" << endl; return; } // If queue is initially empty,set front=rear=0; start of deque if (front == - 1) { front = 0; rear = 0; } else if (front == 0) // front is first position of queue front = size - 1 ; else // decrement front 1 position front = front-1; array[front] = key ; // insert current element into Deque } // insert element at the rear end of deque

// 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; }