Last active
October 20, 2021 07:48
-
-
Save felixdusengimana/0d9d926506ce7078dd40f7b236642c85 to your computer and use it in GitHub Desktop.
Implementation of circular Queue using linked list and Array in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*implementation of circular queue using array */ | |
#include<iostream> | |
using namespace std; | |
int front=-1,rear=-1; | |
int size=0; | |
void createQueue(){ | |
cout<<"How many Element you want to create in Queue:: "; | |
cin>>size; | |
} | |
int *Queue = new int[size]; | |
void insert(int val){ | |
if ((front == 0 && rear == size-1) || (front == rear+1)) { | |
cout<<"Overflow error\n"; | |
return; | |
} | |
if(front==-1){ | |
front=0; | |
rear=0; | |
}else{ | |
if(rear==size-1) | |
rear= 0; | |
else | |
rear=rear+1; | |
} | |
Queue[rear]=val; | |
} | |
void deleteQ(){ | |
if(front==-1){ | |
cout<<"Underflow error\n"; | |
return; | |
} | |
cout<<"Element deleted from queue is : "<<Queue[front]<<endl; | |
if(front==rear){ | |
front=-1; | |
rear=-1; | |
}else{ | |
if(front==size-1) | |
front=0; | |
else | |
front+=1; | |
} | |
} | |
void display(){ | |
int f = front, r = rear; | |
if (front == -1) { | |
cout<<"Queue is empty"<<endl; | |
return; | |
} | |
cout<<"Queue elements are :\n"; | |
if (f <= r) { | |
while (f <= r){ | |
cout<<Queue[f]<<" "; | |
f++; | |
} | |
}else{ | |
while (f <= size - 1) { | |
cout<<Queue[f]<<" "; | |
f++; | |
} | |
f = 0; | |
while (f <= r) { | |
cout<<Queue[f]<<" "; | |
f++; | |
} | |
} | |
} | |
int main(){ | |
createQueue(); | |
int ch, val; | |
cout<<"1)Insert\n"; | |
cout<<"2)Delete\n"; | |
cout<<"3)Display\n"; | |
cout<<"4)Exit\n"; | |
do{ | |
cout<<"\nEnter choice : "<<endl; | |
cin>>ch; | |
switch(ch) { | |
case 1: | |
cout<<"\nInput for insertion: "<<endl; | |
cin>>val; | |
insert(val); | |
break; | |
case 2: | |
deleteQ(); | |
break; | |
case 3: | |
display(); | |
break; | |
case 4: | |
cout<<"Exit\n"; | |
break; | |
default: cout<<"Incorrect!\n"; | |
} | |
} while(ch != 4); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*implementation of circular queue using linked list */ | |
#include<iostream> | |
#include<cstdlib> | |
using namespace std; | |
typedef struct OurQueue{ | |
int data; | |
struct OurQueue *next; | |
}node; | |
node * createQueue(){ | |
node * front=NULL,*rear=NULL; | |
int n, data; | |
cout<<("How many Element you want to create in Queue:: "); | |
cin>>n; | |
int i=0; | |
while(i<=(n-1)){ | |
cout<<"Enter data: "; | |
cin>>data; | |
node *newNode = (node *)malloc(sizeof(node)); | |
newNode->data = data; | |
if(i==0){ | |
newNode->next = NULL; | |
rear = newNode; | |
front=newNode; | |
}else{ | |
newNode->next=front; | |
rear->next=newNode; | |
rear = newNode; | |
} | |
++i; | |
} | |
return rear; | |
} | |
void enqueue(node **rear,int data){ | |
node *newNode = (node *)malloc(sizeof(node)); | |
newNode->data = data; | |
if(!(*rear)){ | |
newNode->next=NULL; | |
*rear = newNode; | |
return; | |
} | |
node *temp = *rear; | |
if(!(*rear)->next){ | |
newNode->next = temp; | |
}else{ | |
newNode->next = temp->next; | |
} | |
temp->next = newNode; | |
*rear = newNode; | |
} | |
int dequeue(node *rear){ | |
int data; | |
if(!rear){ | |
cout<<("\nNO ITEMS IN THE QUEUE\n"); | |
return 0; | |
} | |
if(!rear->next){ | |
data = rear->data; | |
free(rear); | |
return data; | |
} | |
node *temp = rear->next; | |
data = temp->data; | |
rear->next = temp->next; | |
free(temp); | |
return data; | |
} | |
void display(node *rear, node *front){ | |
if(!rear){ | |
cout<<("\nNOTHING TO DISPLAY\n"); | |
return; | |
} | |
node *temp = front; | |
if(!rear->next){ | |
cout<<rear->data<<" "; | |
return; | |
} | |
if(temp!=rear){ | |
cout<<temp->data<<" "; | |
display(rear, temp->next); | |
}else{ | |
cout<<temp->data<<" "; | |
} | |
} | |
int main(){ | |
node *rear = createQueue(); | |
display(rear,rear->next); | |
enqueue(&rear,99); | |
cout<<"\nAfter Adding item"<<endl; | |
display(rear,rear->next); | |
int data = dequeue(rear); | |
cout<<"\nAfter Deleting item: "<<data<<endl; | |
display(rear,rear->next); | |
return 0; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment