From 63a4cd9c98f0166e3d643447039f27bc8bcb62eb Mon Sep 17 00:00:00 2001 From: Shubham Prakash <89865676+Shubhamprakash007@users.noreply.github.com> Date: Sun, 24 Oct 2021 13:11:58 +0530 Subject: [PATCH 1/2] Update and rename Circular Queue to Circular Queue.cpp --- Circular Queue => Circular Queue.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) rename Circular Queue => Circular Queue.cpp (90%) diff --git a/Circular Queue b/Circular Queue.cpp similarity index 90% rename from Circular Queue rename to Circular Queue.cpp index 15e03c5..387a55c 100644 --- a/Circular Queue +++ b/Circular Queue.cpp @@ -2,15 +2,15 @@ #Algorithm -Insertion of an element +//Insertion of an element -STEP 1 START -STEP 2 Store the element to insert in linear data structure -STEP 3 Check if (front == 0 && rear == MAX-1) || (front == rear+1) then queue Overflow else goto step 4 -STEP 4 Check if (front == -1) then front = 0; rear= 0; else goto step 5 -STEP 5 Check if (rear == MAX -1) then rear=0; else rear= rear+1; and goto step 6 -STEP 6 Insert element cqueue_arr[rear] = item; -STEP 7 STOP +//STEP 1 START +//STEP 2 Store the element to insert in linear data structure +//STEP 3 Check if (front == 0 && rear == MAX-1) || (front == rear+1) then queue Overflow else goto step 4 +//STEP 4 Check if (front == -1) then front = 0; rear= 0; else goto step 5 +//STEP 5 Check if (rear == MAX -1) then rear=0; else rear= rear+1; and goto step 6 +//STEP 6 Insert element cqueue_arr[rear] = item; +//STEP 7 STOP ##Program @@ -110,7 +110,7 @@ int main() ##Output - + /* If you want to enter the element in queue press 1 else 0 1 Inset the element in queue : 5 @@ -124,6 +124,6 @@ If you want to enter the element in queue press 1 else 0 0 Queue is : 5 10 15 - + */ From 1bbdeed2b701f0bb72feb0ad4f68d3fbf3e58911 Mon Sep 17 00:00:00 2001 From: Shubham Prakash <89865676+Shubhamprakash007@users.noreply.github.com> Date: Sun, 24 Oct 2021 13:26:56 +0530 Subject: [PATCH 2/2] Update Circular Queue.cpp --- Circular Queue.cpp | 259 +++++++++++++++++++++++---------------------- 1 file changed, 134 insertions(+), 125 deletions(-) diff --git a/Circular Queue.cpp b/Circular Queue.cpp index 387a55c..e7d08c8 100644 --- a/Circular Queue.cpp +++ b/Circular Queue.cpp @@ -1,129 +1,138 @@ ##Circular Queue -#Algorithm - -//Insertion of an element - -//STEP 1 START -//STEP 2 Store the element to insert in linear data structure -//STEP 3 Check if (front == 0 && rear == MAX-1) || (front == rear+1) then queue Overflow else goto step 4 -//STEP 4 Check if (front == -1) then front = 0; rear= 0; else goto step 5 -//STEP 5 Check if (rear == MAX -1) then rear=0; else rear= rear+1; and goto step 6 -//STEP 6 Insert element cqueue_arr[rear] = item; -//STEP 7 STOP - -##Program - -#include -#define MAX 5 -using namespace std; -/* - * Class Circular Queue - */ -class Circular_Queue -{ - private: - int *cqueue_arr; - int front, rear; - public: - Circular_Queue() - { - cqueue_arr = new int [MAX]; - rear = front = -1; - } - - void insert(int item) - { - int i=0; - while(1) - { - cout<<"If you want to enter the element in queue press 1 else 0\n"; - cin>>i; - if(i==1) - - { if ((front == 0 && rear == MAX-1) || (front == rear+1)) - { - cout<<"Queue Overflow \n"; - return; - } - if (front == -1) - { - front = 0; - rear = 0; - } - else - { - if (rear == MAX - 1) - rear = 0; - else - rear = rear + 1; - } - cqueue_arr[rear] = item ; - } - else - display();} - } - void display() - { - int front_pos = front, rear_pos = rear; - if (front == -1) - { - cout<<"Queue is empty\n"; - return; - } - cout<<"Queue elements :\n"; - if (front_pos <= rear_pos) - { - while (front_pos <= rear_pos) - { - cout<>item; - cq.insert(item); - cq.display(); - return 0; -} - - -##Output - /* -If you want to enter the element in queue press 1 else 0 -1 -Inset the element in queue : 5 -If you want to enter the element in queue press 1 else 0 -1 -Inset the element in queue : 10 -If you want to enter the element in queue press 1 else 0 -1 -Inset the element in queue : 15 -If you want to enter the element in queue press 1 else 0 -0 -Queue is : -5 10 15 - */ +// C or C++ program for insertion and +// deletion in Circular Queue +#include +using namespace std; + +class Queue +{ + // Initialize front and rear + int rear, front; + + // Circular Queue + int size; + int *arr; + + Queue(int s) + { + front = rear = -1; + size = s; + arr = new int[s]; + } + + void enQueue(int value); + int deQueue(); + void displayQueue(); +}; + + +/* Function to create Circular queue */ +void Queue::enQueue(int value) +{ + if ((front == 0 && rear == size-1) || + (rear == (front-1)%(size-1))) + { + printf("\nQueue is Full"); + return; + } + + else if (front == -1) /* Insert First Element */ + { + front = rear = 0; + arr[rear] = value; + } + + else if (rear == size-1 && front != 0) + { + rear = 0; + arr[rear] = value; + } + + else + { + rear++; + arr[rear] = value; + } +} + +// Function to delete element from Circular Queue +int Queue::deQueue() +{ + if (front == -1) + { + printf("\nQueue is Empty"); + return INT_MIN; + } + + int data = arr[front]; + arr[front] = -1; + if (front == rear) + { + front = -1; + rear = -1; + } + else if (front == size-1) + front = 0; + else + front++; + + return data; +} + +// Function displaying the elements +// of Circular Queue +void Queue::displayQueue() +{ + if (front == -1) + { + printf("\nQueue is Empty"); + return; + } + printf("\nElements in Circular Queue are: "); + if (rear >= front) + { + for (int i = front; i <= rear; i++) + printf("%d ",arr[i]); + } + else + { + for (int i = front; i < size; i++) + printf("%d ", arr[i]); + + for (int i = 0; i <= rear; i++) + printf("%d ", arr[i]); + } +} + +/* Driver of the program */ +int main() +{ + Queue q(5); + + // Inserting elements in Circular Queue + q.enQueue(14); + q.enQueue(22); + q.enQueue(13); + q.enQueue(-6); + + // Display elements present in Circular Queue + q.displayQueue(); + + // Deleting elements from Circular Queue + printf("\nDeleted value = %d", q.deQueue()); + printf("\nDeleted value = %d", q.deQueue()); + + q.displayQueue(); + + q.enQueue(9); + q.enQueue(20); + q.enQueue(5); + + q.displayQueue(); + + q.enQueue(20); + return 0; +}