Queue Programs(Solution)
Queue Programs(Solution)
1. Implement a C++ program to reverse the order of elements in a queue using only
queue operations.
Solution:
# include <iostream>
# include <malloc.h>
struct node
{
int info;
}*front=NULL, *rear=NULL;
tmp->info=added_item;
tmp->link=NULL;
if(front==NULL){
front=tmp;
}
else{
rear->link=tmp;
}
rear=tmp;
}
//------------------------------------------------------------
void del(){
struct node *tmp;
if(front==NULL){
cout<<"Queue Underflow";
return;
}
else{
tmp=front;
cout<<"Deleted element is "<<tmp->info;
front=front->link;
free(tmp);
}
}
//------------------------------------------------------
void display(){
else{
cout<<"Queue Elements"<<endl;
while(ptr!=NULL){
cout<<ptr->info<<endl;
ptr=ptr->link;
}
}
void reverse() {
if (front == NULL) {
cout << "Queue is empty." << endl;
return;
}
front = prev;
}
int main()
{
int choice,added_item;
while(1)
{
cout<<endl;
cout<<"1 Insertin"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3. Display"<<endl;
cout<<"4.Reverse"<<endl;
cout<<"5.Quit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the element for adding in queue: ";
cin>>added_item;
insert(added_item);
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
reverse();
break;
case 5:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}
#include<iostream>
struct node
{
int data;
node *next;
node *prev;
}*start = NULL ,*rear=NULL;
void input_que();
void output_que();
void insert_rght(int);
void insert_lft(int);
void delete_lft();
void delete_rght();
void display_queue();
void decendingsort() {
if (start == NULL) {
cout << "Queue is empty" << endl;
return;
}
node *q;
while (true) { // Loop until no swaps are made
bool swapped = false;
q = start;
while (q->next != NULL) {
if (q->data < q->next->data) { // Swap if elements are in ascending order
int temp = q->data;
q->data = q->next->data;
q->next->data = temp;
swapped = true;
}
q = q->next;
}
if (!swapped) { // No swaps made, queue is sorted
break;
}
}
int main()
{
int choice;
while (true)
{
cout << "1.Input restricted dequeue\n";
cout << "2.Output restricted dequeue\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
input_que();
break;
case 2:
output_que();
break;
default:
cout << "Invalid choice\n";
break;
}
}
}
void input_que()
{
int choice,n;
while (true)
{
cout<<endl;
cout << "1.Insert at rght\n";
cout << "2.Delete from lft\n";
cout << "3.Delete from rght\n";
cout << "4.Display\n";
cout << "5.Sort\n";
cout << "6.Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout<<"Enter the number: ";
cin>>n;
insert_rght(n);
break;
case 2:
delete_lft();
break;
case 3:
delete_rght();
break;
case 4:
display_queue();
break;
case 5:
decendingsort();
break;
case 6:
exit(1);
default:
cout << "Invalid choice\n";
break;
}
}
}
void output_que()
{
int choice ,n;
while (1)
{
cout<<endl;
cout << "1.Insert at rght\n";
cout << "2.Insert at lft\n";
cout << "3.Delete from lft\n";
cout << "4.Display\n";
cout << "5.Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout<<"Enter the number: ";
cin>>n;
insert_rght(n);
break;
case 2:
cout<<"Enter the number: ";
cin>>n;
insert_lft(n);
break;
case 3:
delete_lft();
break;
case 4:
display_queue();
break;
case 5:
exit(1);
default:
cout << "Invalid choice\n";
break;
}
}
}
void insert_rght(int added_item)
{
struct node *q,*tmp;
tmp=new(struct node);
tmp->data=added_item;
tmp->next=NULL;
if(start==NULL)
{
tmp->prev=NULL;
start=tmp;
}
else
{
rear->next=tmp;
// q=start;
// while(q->next!=NULL)q=q->next;
// q->next=tmp;
// tmp->prev=q;
}
rear=tmp;
void delete_lft()
{
struct node *tmp,*q;
if(start==NULL){
cout << "Queue underflow ";
return;
}
if(start->next==NULL && rear->next==NULL){
start=NULL;
rear=NULL;
return;
}
tmp=start;
start=start->next; /*first element deleted*/
start->prev=NULL;
free(tmp);
return;
void delete_rght()
{
struct node *tmp,*q;
if(start==NULL){
cout << "Queue underflow ";
return;
}
q=start;
while(q->next->next!=NULL)
{
q=q->next;
}
if(q->next->next==NULL) /*last element deleted*/
{
tmp=q->next;
free(tmp);
q->next=NULL;
rear=q;
return;
}
void display_queue()
{
struct node *q;
if(start==NULL)
{
cout<<"Queue is empty"<<endl;
return;
}
q=start;
cout<<"Queue is:"<<endl;
while(q!=NULL)
{
cout<<q->data<<" ";
q=q->next;
}
cout<<endl;
}
3. Write a C++ program to implement a circular dequeue (double-ended queue) using
linked list. Include functions for insertion and deletion from both ends.
#include<iostream>
struct node
{
int data;
node *next;
node *prev;
}*start = NULL ,*rear=NULL;
void input_que();
void output_que();
void insert_rght(int);
void insert_lft(int);
void delete_lft();
void delete_rght();
void display_queue();
int main()
{
int choice;
while (true)
{
cout << "1.Input restricted dequeue\n";
cout << "2.Output restricted dequeue\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
input_que();
break;
case 2:
output_que();
break;
default:
cout << "Invalid choice\n";
break;
}
}
}
void input_que()
{
int choice,n;
while (true)
{
cout<<endl;
cout << "1.Insert at rght\n";
cout << "2.Delete from lft\n";
cout << "3.Delete from rght\n";
cout << "4.Display\n";
cout << "5.Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout<<"Enter the number: ";
cin>>n;
insert_rght(n);
break;
case 2:
delete_lft();
break;
case 3:
delete_rght();
break;
case 4:
display_queue();
break;
case 5:
exit(1);
default:
cout << "Invalid choice\n";
break;
}
}
}
void output_que()
{
int choice ,n;
while (1)
{
cout<<endl;
cout << "1.Insert at rght\n";
cout << "2.Insert at lft\n";
cout << "3.Delete from lft\n";
cout << "4.Display\n";
cout << "5.Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout<<"Enter the number: ";
cin>>n;
insert_rght(n);
break;
case 2:
cout<<"Enter the number: ";
cin>>n;
insert_lft(n);
break;
case 3:
delete_lft();
break;
case 4:
display_queue();
break;
case 5:
exit(1);
default:
cout << "Invalid choice\n";
break;
}
}
}
}
rear=tmp;
}
start=tmp;
void delete_lft()
{
struct node *tmp,*q;
if(start==NULL){
cout << "Queue underflow ";
return;
}
if(start->next==NULL && rear->next==NULL){
start=NULL;
rear=NULL;
return;
}
tmp=start;
start=start->next; //first element deleted
start->prev=tmp->prev;
rear->next=tmp->next;
free(tmp);
return;
void delete_rght()
{
struct node *tmp,*q;
if(start==NULL){
cout << "Queue underflow ";
return;
}
if(start->next==NULL && rear->next==NULL){
start=NULL;
rear=NULL;
return;
}
q=start;
while(q->next->next!=start)
{
q=q->next;
}
if(q->next->next==start) //last element deleted
{
tmp=q->next;
q->next=tmp->next;
free(tmp);
rear=q;
return;
}
void display_queue()
{
// cout<<start->prev->data<<endl;
struct node *q;
if(start==NULL)
{
cout<<"Queue is empty"<<endl;
return;
}
q=start;
cout<<"Queue is:"<<endl;
while(q->next!=start)
{
cout<<q->data<<" ";
q=q->next;
}
cout<<q->data<<" ";
cout<<endl;
}
4. Implement a C++ program to check if two queues are identical (having the same
elements in the same order).
#include<iostream>
#include<cstring>
using namespace std;
struct node{
int info;
node *link;
}
*front=NULL , *rear=NULL, *front1=NULL, *rear1=NULL;
if(front==NULL){
front=tmp;
rear=tmp;
}
else{
rear->link=tmp;
rear=tmp;
}
}
void display1(){
node *tmp;
if(front ==NULL){
cout<<"Queue Is Empty!";
}
else{
tmp=front;
while(tmp!=NULL){
cout<<tmp->info<<" ";
tmp=tmp->link;
}
cout<<endl;
}
}
//1st queue end
//2nd queue start
void create2(int data){
node *tmp;
tmp=new(node);
tmp->info=data;
tmp->link=NULL;
if(front1==NULL){
front1=tmp;
rear1=tmp;
}
else{
rear1->link=tmp;
rear1=tmp;
}
}
void display2(){
node *tmp;
if(front1 ==NULL){
cout<<"Queue Is Empty!";
}
else{
tmp=front1;
while(tmp!=NULL){
cout<<tmp->info<<" ";
tmp=tmp->link;
}
cout<<endl;
}
}
//2nd queue end
void check_identical(int q1, int q2){ //q1,q2 node variables passed from main.
node *p1,*p2;
int count;
p1=front;
p2=front1;
for(int i=0;i<q1;i++){
if(p1->info==p2->info){
count++;
}
else{
cout<<endl;
cout<<i+1<<"Element mis-match found ! "<<p2->info<<endl;
}
p1=p1->link;
p2=p2->link;
}
if(count==q1){
cout<<"Queue's are identical";
}
else{
cout<<"Queue's are not identical";
}
}
int main(){
int q1,q2,qe1,qe2;
cout<<"Enter Nodes You Want For Queue-1:";
cin>>q1;
cout<<"Enter Queue-1 Elements:";
for(int i=0;i<q1;i++){
cin>>qe1;
create1(qe1);
}
}
5. Design a C++ program to check if a given string is a valid palindrome by ignoring
nonalphanumeric characters using a dequeue.
#include <iostream>
using namespace std;
struct node
{
int info;
node *next;
node *prev;
int count = 0;
int num;
void input_que();
void output_que();
void insert_rgt();
void insert_lft();
void delete_lft();
void delete_rgt();
void display_queue();
void checkpalindrome();
int main()
{
int choice;
cout << "Enter the size of circular doubly entry linked list queue: ";
cin >> num;
while (true)
{
cout << "1.Input restricted dequeue\n";
cout << "2.Output restricted dequeue\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
input_que();
break;
case 2:
output_que();
break;
default:
cout << "Invalid choice\n";
break;
}
}
}
void input_que()
{
int choice;
while (1)
{
cout << "1.Insert at rgt\n";
cout << "2.Delete from lft\n";
cout << "3.Delete from Rgtrgt\n";
cout << "4.Display\n";
cout << "5.Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
insert_rgt();
break;
case 2:
delete_lft();
break;
case 3:
delete_rgt();
break;
case 4:
display_queue();
break;
case 5:
exit(1);
default:
cout << "Invalid choice\n";
break;
}
}
}
void output_que()
{
int choice;
while (1)
{
cout << "1.Insert at rgt\n";
cout << "2.Insert at lft\n";
cout << "3.Delete from lft\n";
cout << "4.Display\n";
cout << "5.Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
insert_rgt();
break;
case 2:
insert_lft();
break;
case 3:
delete_lft();
break;
case 4:
display_queue();
break;
case 5:
exit(1);
default:
cout << "Invalid choice\n";
break;
}
}
}
void insert_rgt()
{
temp=new node;
temp->info = added_item;
temp->next = NULL;
temp->prev = NULL;
if (lft == NULL)
{
lft = temp;
rgt = temp;
count++;
}
else
{
if (count < num)
{
rgt->next = temp;
temp->prev = rgt;
rgt = temp;
rgt->next = lft;
lft->prev = rgt;
count++;
}
}
cout << "Do you want to check palindrome 0/1: ";
cin >> input;
if (input == 1)
{
checkpalindrome();
}
}
void insert_lft()
{
if (count >= num)
{
cout << "Stack overflow\n";
return;
}
int added_item;
node *temp;
cout << "Input element for adding in queue: ";
cin >> added_item;
if (lft == NULL)
{
lft = temp;
rgt = temp;
count++;
}
else
{
if (count < num)
{
temp->next = lft;
lft->prev = temp;
lft = temp;
rgt->next = lft; // lower-arrow
lft->prev = rgt; // upper-arrow
count++;
}
}
}
void delete_lft()
{
node *q = lft;
if (lft == NULL || count == 0)
{
cout << "Queue underflow\n";
return;
}
cout << "Element deleted in queue: " << lft->info << endl;
if (lft == rgt)
{
lft = NULL;
rgt = NULL;
count--;
}
else
{
lft = lft->next;
delete (q);
rgt->next = lft;
lft->prev = rgt;
count--;
}
}
void delete_rgt()
{
node *q = rgt;
if (rgt == NULL || count == 0)
{
cout << "Queue underflow\n";
return;
}
cout << "Element deleted in queue: " << rgt->info << endl;
if (lft == rgt)
{
lft = NULL;
rgt = NULL;
count--;
}
else
{
rgt = rgt->prev;
delete (q);
rgt->next = lft;
lft->prev = rgt;
count--;
}
}
void display_queue()
{
node *q = lft;
if (lft == NULL || count == 0)
{
cout << "Queue is empty!\n Underflow Error:\n";
return;
}
void checkpalindrome()
{
int a = 0, n = count, counter, i = 0;
counter = n / 2;
node *p = lft;
node *q = rgt;
while (i < counter)
{
if (p->info != q->info)
{
a = 1;
break;
}
q = q->prev;
p = p->next;
i++;
}
if (a == 0)
cout << "Given number is a palindrome\n";
else
cout << "Given number is not a palindrome\n";
}
6. Write a C++ program to simulate a call center system using multiple queues. Each
queue represents a different type of customer inquiry, and the system should prioritize
queues based on the urgency of inquiries.
#include <iostream>
#include <string>
class Queue {
public:
// Constructor
Inquiry* front;
Inquiry* rear;
// Destructor
~Queue() {
while (front != NULL) {
Inquiry* temp = front;
front = front->next;
delete temp;
}
rear = NULL;
}
if (empty()) {
front = newNode;
} else {
rear->next = newNode;
}
rear = newNode;
}
public:
// Function to add an inquiry to the appropriate queue based on its type
void addInquiry(string type,string inquiry) {
if (type == "urgent") {
urgentQueue.push(type,inquiry);
} else {
normalQueue.push(type,inquiry);
}
}
cout<<endl;
// Process normal inquiries
while (!normalQueue.empty()) {
Inquiry inquiry = *(normalQueue.front);
normalQueue.pop();
cout << "Processing normal inquiry: " << inquiry.message << endl;
}
}
};
int main() {
// Create a CallCenter object
CallCenter callCenter;
// Process inquiries
callCenter.processInquiries();
return 0;
}
7. Design a C++ program to implement a deque (double-ended queue) using two stacks.
Include functions for insertion and deletion from both ends.
#include <iostream>
struct Node {
int data;
Node* next;
};
if (front1 == NULL) {
front1 = temp;
rear1 = temp;
} else {
temp->next = front1;
front1 = temp;
}
}
if (front2 == NULL) {
front2 = temp;
rear2 = temp;
} else {
rear2->next = temp;
rear2 = temp;
}
}
front1 = front1->next;
delete (q);
} else {
// Move all elements from front2 to front1 except the last one
while (front2->next != NULL) {
push_front(front2->data);
Node* q = front2;
front2 = front2->next;
delete (q);
}
// Remove the last element from front2
delete (front2);
front2 = NULL;
rear2 = NULL;
}
}
int main() {
int choice, data;
while (true) {
cout << "1. Push Front\n";
cout << "2. Push Back\n";
cout << "3. Pop Front\n";
cout << "4. Pop Back\n";
cout << "5. Display\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter data: ";
cin >> data;
push_front(data);
break;
case 2:
cout << "Enter data: ";
cin >> data;
push_back(data);
break;
case 3:
pop_front();
break;
case 4:
pop_back();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
}
return 0;
}
8. Implement a C++ program to find the kth largest element in a queue without modifying
the queue.
#include<iostream>
using namespace std;
struct node {
int info;
node *link;
}*front = NULL,*rare = NULL;
void push (int data) {
node *tmp;
tmp = new node();
tmp->info = data;
tmp->link = NULL;
if (front == NULL) {
front = tmp;
rare = tmp;
}
else {
// tmp->link = front;
front = tmp;
}
}
void pushback(int data) {
node *tmp;
tmp = new node();
tmp->info = data;
tmp->link = NULL;
if (front == NULL) {
front = tmp;
rare = tmp;
}
else {
rare->link = tmp;
rare = tmp;
}
}
void popfront() {
node *tmp;
if (front == NULL) {
cout << "deque is empty" << endl;
return;
}
else {
tmp = front;
front = front->link;
delete tmp;
}
}
void popback() {
node *tmp,*prev;
if (front == NULL) {
cout<<"deque is empty"<<endl;
return ;
}
tmp = front;
prev = NULL;
// now checking all nodes
while (tmp->link != NULL) {
prev = tmp;
tmp = tmp->link;
}
// if only we have single node
if (prev == NULL) {
front = NULL;
rare = NULL;
delete tmp;
}
else {
prev->link = NULL;
delete tmp;
rare = prev;
}
}
void display() {
node *q;
q = front;
if (front == NULL) {
cout<<"no data in deque to show:"<<endl;
}
else {
while (q != NULL) {
cout<<q->info<<endl;
q = q->link;
}
cout<<endl;
}
}
int findingkthlargest(int k) {
node *q;
q = front;
int length = 0;
while (q != NULL) {
length ++;
q = q->link;
}
int position;
position = length - k;
q = front;
for (int i =0; i < position ; i++) {
q = q->link;
}
return q->info;
}
int main() {
int choice,data;
while(1) {
cout << "1. Push Front" << endl;
cout << "2. Push Back" << endl;
cout << "3. Pop Front" << endl;
cout << "4. Pop Back" << endl;
cout << "5. Display" << endl;
cout << "6. Find kth Largest Element" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice: ";
cin>>choice;
switch (choice) {
case 1:
cout<<"enter data:"<<endl;
cin>>data;
push(data);
break;
case 2:
cout<<"enter data:"<<endl;
cin>>data;
pushback(data);
break;
case 3:
popfront();
break;
case 4 :
popback();
break;
case 5:
display();
break;
case 6:
int k ;
cout<<"enter k value:"<<endl;
cin>>k;
if (k <= 0) {
cout << "Invalid value of k." << endl;
} else if (front == NULL) {
cout << "Queue is empty." << endl;
} else {
cout << "The kth largest element is:" <<findingkthlargest(k)<<endl;
}
break;
case 7:
exit ;
break;
}
}
}
9. Design a C++ program to implement a queue using a priority queue. Each element
should have a priority, and the queue should be sorted based on priority.
#include<iostream>
struct node
{
int priority;
int info;
node *link;
}*front=NULL;
void insert()
{
node *temp,*q;
int added_item,item_priority;
temp=new (node);
cout<<"Input the item value to be added in the queue :";
cin>> added_item;
cout<<"Enter its priority :";
cin>>item_priority;
temp->info=added_item;
temp->priority= item_priority;
if(front==NULL || item_priority<front->priority)
{
temp->link=front;
front=temp;
}
else
{
q=front;
while(q->link!=NULL && q->link->priority<=item_priority)
{
q=q->link;
}
temp->link=q->link;
q->link=temp;
}
}
void del()
{
node *temp;
if(front==NULL)
{
cout<<"Queue is Underflow \n";
}
else
{
temp=front;
cout<<"Element of deleted is :"<<temp->info<<"\n";
front=front->link;
free(temp);
}
}
void display()
{
node *q;
q=front;
if(q==NULL)
{
cout<<"Queue is Empty \n";
}
else
{
cout<<"Queue is :\n";
cout<<"Priority item :\n";
while(q!=NULL)
{
cout<<q->priority<<" "<<q->info<<"\t\t";
q=q->link;
}
cout<<endl;
}
}
//...............................................................
int main()
{
int choice ;
while(true)
{
cout<<"1.Add Element \n";
cout<<"2.Delete Element \n";
cout<<"3.Display Elements \n";
cout<<"4.Exit \n";
cin>>choice;
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
cout<<"You entered invalid choice "<<"\n";
}
}
}
#include <iostream>
using namespace std;
//Split two single dequeues in three equal dequeues
struct node {
int data;
node *link;
};
switch (choice) {
case 1: {
int numNodes;
cout << "Enter the number of nodes you want to insert into dequeue 1: ";
cin >> numNodes;
for (int i = 0; i < numNodes; ++i) {
int data;
cout << "Enter data for node " << i + 1 << ": ";
cin >> data;
insert_right1(data);
}
break;
}
case 2:
display1();
break;
case 3: {
int numNodes;
cout << "Enter the number of nodes you want to insert into dequeue 2: ";
cin >> numNodes;
for (int i = 0; i < numNodes; ++i) {
int data;
cout << "Enter data for node " << i + 1 << ": ";
cin >> data;
insert_right2(data);
}
break;
}
case 4:
display2();
break;
case 5:
concatenate();
break;
case 6:
split();
break;
case 7:
exit(0);
default:
cout << "Invalid choice" << endl;
}
}
return 0;
}
void display1() {
if (left1 == NULL) {
cout << "Deque 1 is empty" << endl;
return;
}
node *temp = left1;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->link;
}
cout << endl;
}
void display2() {
if (left2 == NULL) {
cout << "Deque 2 is empty" << endl;
return;
}
node *temp = left2;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->link;
}
cout << endl;
}
void concatenate() {
if (left1 == NULL || left2 == NULL) {
cout << "Cannot concatenate. One of the deques is empty." << endl;
return;
}
right1->link = left2;
right1 = right2;
cout << "Concatenation successful" << endl;
}
void split() {
if (left1 == NULL) {
cout << "Deque 1 is empty. Cannot split." << endl;
return;
}
int count = 0;
node *temp = left1;
while (temp != NULL) {
count++;
temp = temp->link;
}
int splitSize = count / 3;
node *prev = NULL;
temp = left1;
int part = 1;
while (temp != NULL && part <= 3) {
cout << "dequeue" << part << ": ";
for (int i = 0; i < splitSize; ++i) {
cout << temp->data << " ";
temp = temp->link;
}
cout << endl;
part++;
}
if (temp != NULL) {
cout << "Remaining elements: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->link;
}
cout << endl;
}
prev->link = NULL;
}
11. Split two simple queues into 3 three queues. One queue should be equal to sum of
two other numbers of elements in these queues.
#include <iostream>
#include <cstdlib>
using namespace std;
struct node {
int info;
node* link;
node(int value) : info(value), link(NULL) {}
};
node* f1 = NULL;
node* r1 = NULL;
node* f2 = NULL;
node* r2 = NULL;
node* f3 = NULL;
node* r3 = NULL;
void insert(node*& f, node*& r, int data);
void del(node*& f, node*& r);
void display(node* f, node* r);
void count();
void queue1();
void queue2();
int c = 0;
int main() {
int data, choice,m,n;
while (1)
{
cout << "1. Queue 1" << endl;
cout << "2. Queue 2 " << endl;
cout << "3. Enter elements in queue 3" << endl;
cout << "4. Quit" << endl;
case 2:
queue2();
break;
case 3:
count();
cout<< "Total number of elements you can enter in queue 3 are: "<< c <<endl;
case 4:
exit(1);
}
}
void queue1(){
int data, choice,m,n;
while (1)
{
cout << "1. Add Element to Queue " << endl;
cout << "2. Delete from Queue" << endl;
cout << "3. Display Elements in Queue" << endl;
cout << "4.quit"<<endl;
case 2:
del(f1,r1);
break;
case 3:
display(f1,r1);
break;
case 4:
return;
}
}
}
void queue2(){
int data, choice,m,n;
while (1)
{
cout << "1. Add Element to Queue " << endl;
cout << "2. Delete from Queue" << endl;
cout << "3. Display Elements in Queue" << endl;
cout << "4.quit"<<endl;
case 2:
del(f2,r2);
break;
case 3:
display(f2,r2);
break;
case 4:
return;
}
}
}
12. Sort two dequeues and than concatenate them from rear side.
#include<iostream>
using namespace std;
//Sort two dequeues and than concatenate them from rear side
struct Node {
int data;
Node* next;
};
int main() {
int choice, n, data;
while (true) {
cout << "1. Insert into first queue (from rear)" << endl;
cout << "2. Insert into second queue (from rear)" << endl;
cout << "3. Delete from first queue (from front)" << endl;
cout << "4. Delete from second queue (from front)" << endl;
cout << "5. Display first queue" << endl;
cout << "6. Display second queue" << endl;
cout << "7. Sort first queue" << endl;
cout << "8. Sort second queue" << endl;
cout << "9. Concatenate" << endl;
cout << "10. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the number of nodes to insert into the first queue: ";
cin >> n;
for (int i = 0; i < n; ++i) {
cout << "Enter element " << i + 1 << " to insert into the first queue (from rear): ";
cin >> data;
insert_right_deque1(data);
}
break;
case 2:
cout << "Enter the number of nodes to insert into the second queue: ";
cin >> n;
for (int i = 0; i < n; ++i) {
cout << "Enter element " << i + 1 << " to insert into the second queue (from rear): ";
cin >> data;
insert_right_deque2(data);
}
break;
case 3:
delete_left_deque1();
break;
case 4:
delete_left_deque2();
break;
case 5:
display_queue_deque1();
break;
case 6:
display_queue_deque2();
break;
case 7:
sort_queue_deque1();
break;
case 8:
sort_queue_deque2();
break;
case 9:
concatenate();
break;
case 10:
exit(0);
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
}
return 0;
}
void delete_left_deque1() {
if (front1 == NULL) {
cout << "First queue underflow" << endl;
return;
}
Node* tmp = front1;
cout << "Element deleted from first queue: " << tmp->data << endl;
front1 = front1->next;
if (front1 == NULL) {
rear1 = NULL; // If the queue becomes empty after deletion, update rear pointer
}
delete tmp;
}
void delete_left_deque2() {
if (front2 == NULL) {
cout << "Second queue underflow" << endl;
return;
}
Node* tmp = front2;
cout << "Element deleted from second queue: " << tmp->data << endl;
front2 = front2->next;
if (front2 == NULL) {
rear2 = NULL; // If the queue becomes empty after deletion, update rear pointer
}
delete tmp;
}
void display_queue_deque1() {
if (front1 == NULL) {
cout << "First queue is empty" << endl;
return;
}
Node* temp = front1;
cout << "First queue elements: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void display_queue_deque2() {
if (front2 == NULL) {
cout << "Second queue is empty" << endl;
return;
}
Node* temp = front2;
cout << "Second queue elements: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void sort_queue_deque1() {
if (front1 == NULL) {
cout << "First queue is empty. No elements to sort." << endl;
return;
}
void sort_queue_deque2() {
if (front2 == NULL) {
cout << "Second queue is empty. No elements to sort." << endl;
return;
}
void concatenate() {
if (front1 == NULL) {
cout << "First queue is empty. Cannot concatenate." << endl;
return;
}
if (front2 == NULL) {
cout << "Second queue is empty. Cannot concatenate." << endl;
return;
}
rear1->next = front2;
rear1 = rear2;
cout << "Queues concatenated successfully." << endl;
13. Check queue and make function to print value of rear, front and middle value of
queue.
Solution:
#include<iostream>
using namespace std;
struct node{
int info;
node *link;
}*front=NULL,*rear=NULL;
void insertion();
void deleation();
void display();
void insertion(int data)
{
node *tmp;
tmp=new (struct node);
tmp->info=data;
tmp->link=NULL;
if(front==NULL)
{
front = rear = tmp;
}
else
{
rear->link=tmp;
rear=tmp;
}
//cout<<endl;
}
void deleation()
{
node *tmp;
if(front==NULL)
{
cout<<"Queue overflow.";
}
else
{
tmp=front;
cout<<"Delete element in the queue is : "<<tmp->info;
cout<<endl;
front=front->link;
delete tmp;
if (front == NULL)
{
rear = NULL;
}
}
//cout<<endl;
}
void display()
{
node *q;
q=front;
if(front==NULL)
{
cout<<"Queue is empty.";
}
else
{
while(q!=NULL)
{
cout<<q->info;
q=q->link;
}
}
cout<<endl;
}
void printRear() {
if (front == NULL)
cout << "Queue is empty." << endl;
else
cout << "Rear element: " << rear->info << endl;
}
void printFront() {
if (front == NULL)
cout << "Queue is empty." << endl;
else
cout << "Front element: " << front->info << endl;
}
void printMiddle() {
if (front == NULL) {
cout << "Queue is empty." << endl;
return;
}
int count = 0;
node* temp = front;
temp = front;
for (int i = 0; i < middlePosition; i++) {
temp = temp->link;
}
if (count % 2 != 0) {
cout << "Middle element: " << temp->info << endl;
} else {
cout << "Middle elements: " << temp->info << " and " << temp->link->info << endl;
}
}
int main()
{
int choice,n,m;
while(1)
{
cout<<"1.Insertion."<<endl;
cout<<"2.Dealation."<<endl;
cout<<"3.Display."<<endl;
cout<<"4.Front value."<<endl;
cout<<"5.Rear value."<<endl;
cout<<"6.Middel value."<<endl;
cout<<"7.Quit."<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the queue size : ";
cin>>n;
for(int i=0;i<n;++i)
{
cout<<"enter elements : ";
cin>>m;
insertion(m);
}
break;
case 2:
deleation();
break;
case 3:
display();
break;
case 4:
printFront();
break;
case 5:
printRear();
break;
case 6:
printMiddle();
break;
case 7:
exit(1);
break;
default:
cout<<"WRONGE CHOICE, ENTER CORRECT CHOICE.";
break;
}
}
return 0;
}
.
14. Make queue and print the values of 1, 3, 6, 10 and so on indices from the front.
#include<iostream>
using namespace std;
struct node{
int info;
node *link;
}*front=NULL,*rear=NULL;
void insertion();
void deleation();
void display();
void printSpecialSequence();
void insertion(int data)
{
node *tmp;
tmp=new (struct node);
tmp->info=data;
tmp->link=NULL;
if(front==NULL)
{
front = rear = tmp;
}
else
{
rear->link=tmp;
rear=tmp;
}
//cout<<endl;
}
void deleation()
{
node *tmp;
if(front==NULL)
{
cout<<"Queue overflow.";
}
else
{
tmp=front;
cout<<"Delete element in the queue is : "<<tmp->info;
cout<<endl;
front=front->link;
delete tmp;
if (front == NULL)
{
rear = NULL;
}
}
//cout<<endl;
}
void display()
{
node *q;
q=front;
if(front==NULL)
{
cout<<"Queue is empty.";
}
else
{
while(q!=NULL)
{
cout<<q->info<<" ";
q=q->link;
}
}
cout<<endl;
}
void printSpecialSequence() {
if (front == NULL) {
cout << "Queue is empty." << endl;
return;
}
int n = 1;
int targetPosition = n * (n + 1) / 2;
int currentPosition = 0;
while (front != NULL) {
currentPosition++;
if (currentPosition == targetPosition) {
cout << front->info << " ";
n++;
targetPosition = n * (n + 1) / 2;
}
front = front->link;
}
cout << endl;
}
int main()
{
int choice,n,m;
while(1)
{
cout<<"1.Insertion."<<endl;
cout<<"2.Dealation."<<endl;
cout<<"3.Diaplay."<<endl;
cout<<"4.Print special sequence."<<endl;
cout<<"5.Quit."<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the queue size : ";
cin>>n;
for(int i=0;i<n;++i)
{
cout<<"enter elements : ";
cin>>m;
insertion(m);
}
break;
case 2:
deleation();
break;
case 3:
display();
break;
case 4:
printSpecialSequence();
break;
case 5:
exit(1);
break;
default:
cout<<"WRONGE CHOICE, ENTER CORRECT CHOICE.";
break;
}
}
return 0;
}
15. Check the size of the dequeue; if it is greater than 3 remove center value every time
until it reaches to 3 and then print those three values.
#include<iostream>
using namespace std;
struct node{
int info;
node *link;
}*front=NULL,*rear=NULL;
void insertion();
void deleation();
void display();
void printSpecialSequence();
void insertion(int data)
{
node *tmp;
tmp=new (struct node);
tmp->info=data;
tmp->link=NULL;
if(front==NULL)
{
front = rear = tmp;
}
else
{
rear->link=tmp;
rear=tmp;
}
//cout<<endl;
}
void deleation()
{
node *tmp;
if(front==NULL)
{
cout<<"Queue overflow.";
}
else
{
tmp=front;
cout<<"Delete element in the queue is : "<<tmp->info;
cout<<endl;
front=front->link;
delete tmp;
if (front == NULL)
{
rear = NULL;
}
}
//cout<<endl;
}
void display()
{
node *q;
q=front;
if(front==NULL)
{
cout<<"Queue is empty.";
}
else
{
while(q!=NULL)
{
cout<<q->info<<" ";
q=q->link;
}
}
cout<<endl;
}
void printSpecialSequence() {
if (front == NULL) {
cout << "Queue is empty." << endl;
return;
}
int n = 1;
int targetPosition = n * (n + 1) / 2;
int currentPosition = 0;
while (front != NULL) {
currentPosition++;
if (currentPosition == targetPosition) {
cout << front->info << " ";
n++;
targetPosition = n * (n + 1) / 2;
}
front = front->link;
}
cout << endl;
}
int main()
{
int choice,n,m;
while(1)
{
cout<<"1.Insertion."<<endl;
cout<<"2.Dealation."<<endl;
cout<<"3.Diaplay."<<endl;
cout<<"4.Print special sequence."<<endl;
cout<<"5.Quit."<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the queue size : ";
cin>>n;
for(int i=0;i<n;++i)
{
cout<<"enter elements : ";
cin>>m;
insertion(m);
}
break;
case 2:
deleation();
break;
case 3:
display();
break;
case 4:
printSpecialSequence();
break;
case 5:
exit(1);
break;
default:
cout<<"WRONGE CHOICE, ENTER CORRECT CHOICE.";
break;
}
}
}