0% found this document useful (0 votes)
4 views

Queue Programs(Solution)

The document contains multiple C++ programs demonstrating various data structure implementations, including a queue reversal, sorting a dequeue, a circular dequeue, and checking if two queues are identical. Each section provides code snippets along with function definitions for insertion, deletion, and display operations. The programs utilize linked lists to manage the data structures and include user interaction for executing different operations.

Uploaded by

umefarwarizvi111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Queue Programs(Solution)

The document contains multiple C++ programs demonstrating various data structure implementations, including a queue reversal, sorting a dequeue, a circular dequeue, and checking if two queues are identical. Each section provides code snippets along with function definitions for insertion, deletion, and display operations. The programs utilize linked lists to manage the data structures and include user interaction for executing different operations.

Uploaded by

umefarwarizvi111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 65

DATA STRUCTURES USING C++ LAB WORK QUEUE

1. Implement a C++ program to reverse the order of elements in a queue using only
queue operations.

Solution:

# include <iostream>
# include <malloc.h>

using namespace std;

struct node
{

int info;

struct node *link;

}*front=NULL, *rear=NULL;

void insert(int added_item){


struct node *tmp;

tmp=(struct node *)malloc(sizeof(struct node));

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(){

struct node *ptr;


ptr=front;
if(front==NULL){
cout<<"Queue is empty";
return;
}

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

struct node *current, *prev, *next;


current = front;
prev = NULL;
next = NULL;
while (current != NULL) {
next = current->link;
current->link = prev;
prev = current;
current = next;
}

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

2. Implement a C++ program to sort elements of a dequeue in descending order using


any sorting algorithm.

#include<iostream>

using namespace std;

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

cout << "Sorted...." << endl;


}

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 insert_lft(int added_item)


{

struct node *q,*tmp;


tmp=new(struct node);
tmp->data=added_item;
tmp->next=NULL;
tmp->prev=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
tmp->next=start;
start->prev=tmp;
// q=start;
// q->prev=tmp;
// tmp->next=q;
}
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=NULL;
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!=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>

using namespace std;

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

void insert_rght(int added_item)


{
struct node *q,*tmp;
tmp=new(struct node);
tmp->data=added_item;
tmp->next=NULL;
tmp->prev=NULL;
if(start==NULL)
{
start=tmp;
}
else if(start->next==NULL && start->prev==NULL){
tmp->prev=start;
tmp->next=start;
start->prev=tmp;
rear->next=tmp;
}
else
{
tmp->next=start;
tmp->prev=rear;
start->prev=tmp;
rear->next=tmp;

}
rear=tmp;

void insert_lft(int added_item)


{

struct node *q,*tmp;


tmp=new(struct node);
tmp->data=added_item;
tmp->next=NULL;
tmp->prev=NULL;
if(start==NULL)
{
start=tmp;
rear=tmp;
}
else if(start->next==NULL && start->prev==NULL){
tmp->next=start;
tmp->prev=rear;
start->prev=tmp;
start->next=tmp;
}
else
{
tmp->next=start;
tmp->prev=rear;
rear->next=tmp;
start->prev=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;

//1st queue start


void create1(int data){
node *tmp;
tmp=new(node);
tmp->info=data;
tmp->link=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);
}

cout<<"Enter Nodes You Want For Queue-2:";


cin>>q2;
cout<<"Enter Queue-2 Elements:";
for(int i=0;i<q2;i++){
cin>>qe2;
create2(qe2);
}
cout<<"Displaying Queue-1:"<<endl;
display1();
cout<<"Displaying Queue-2:"<<endl;
display2();
check_identical(q1,q2);

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

} *lft = NULL, *rgt = NULL;

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()
{

if (count >= num)


{
cout << "Stack overflow\n";
return;
}

int added_item, input;


node *temp;

cout << "Input element for adding in queue: ";


cin >> added_item;

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;

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)
{
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;
}

cout << "Elements in queue are:\n";


cout << "]";
while (q != rgt)
{
cout << "[" << q->info << "]";
q = q->next;
}
cout << "[" << q->info << "]";
cout << "[\n";
checkpalindrome();
}

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>

using namespace std;

// Define a structure to represent a customer inquiry


struct Inquiry {
string type;
string message;
Inquiry* next;
};

class Queue {

public:
// Constructor
Inquiry* front;
Inquiry* rear;

Queue() : front(NULL), rear(NULL) {}

// Destructor
~Queue() {
while (front != NULL) {
Inquiry* temp = front;
front = front->next;
delete temp;
}
rear = NULL;
}

// Function to check if the queue is empty


bool empty() {
return front == NULL;
}

// Function to enqueue an inquiry


void push(string type,string inquiry) {
Inquiry* newNode = new Inquiry;
newNode->type = type;
newNode->message = inquiry;
newNode->next = NULL;

if (empty()) {
front = newNode;
} else {
rear->next = newNode;
}
rear = newNode;
}

// Function to dequeue an inquiry


void pop() {
if (empty()) {
cout << "Queue is empty." << endl;
return;
}
Inquiry* temp = front;
front = front->next;
delete temp;
if (front == NULL) {
rear = NULL;
}
}

// Function to process inquiries


void processInquiries() {
while (!empty()) {
cout << "Processing inquiry: " << front->message << endl;
pop();
}
}
};

// Define a call center class


class CallCenter {
private:
Queue urgentQueue;
Queue normalQueue;

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

// Function to process inquiries


void processInquiries() {
// Process urgent inquiries first
while (!urgentQueue.empty()) {
Inquiry inquiry = *(urgentQueue.front);
urgentQueue.pop();
cout << "Processing urgent inquiry: " << inquiry.message << endl;
}

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;

// Add some sample inquiries


callCenter.addInquiry("urgent", "Issue with payment processing");
callCenter.addInquiry("normal", "Product information request");
callCenter.addInquiry("urgent", "Service outage report");
callCenter.addInquiry("normal", "Delivery status inquiry");

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

using namespace std;

struct Node {
int data;
Node* next;
};

Node* front1 = NULL;


Node* rear1 = NULL;
Node* front2 = NULL;
Node* rear2 = NULL;

// Function to push an element to the front of the deque


void push_front(int data) {
Node* temp;
temp = new (Node);
temp->data = data;
temp->next = NULL;

if (front1 == NULL) {
front1 = temp;
rear1 = temp;
} else {
temp->next = front1;
front1 = temp;
}
}

// Function to push an element to the back of the deque


void push_back(int data) {
Node* temp ;
temp= new (Node);
temp->data = data;
temp->next = NULL;

if (front2 == NULL) {
front2 = temp;
rear2 = temp;
} else {
rear2->next = temp;
rear2 = temp;
}
}

// Function to pop an element from the front of the deque


void pop_front() {
Node* q ;
q = front1;
if (front1 == NULL && front2 == NULL) {
cout << "Deque is empty. Cannot pop from front.\n";
return;
}
if (front1 != NULL) {

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

// Function to pop an element from the back of the deque


void pop_back() {
if (front1 == NULL && front2 == NULL) {
cout << "Deque is empty. Cannot pop from back.\n";
return;
}
if (front2 != NULL) {
Node* temp = front2;
Node* q = NULL;
while (temp->next != NULL) {
q = temp;
temp = temp->next;
}
if (q == NULL) {
delete (temp);
front2 = NULL;
rear2 = NULL;
} else {
q->next = NULL;
rear2 = q;
delete (temp);
}
} else {
// Move all elements from front1 to front2 except the first one
while (front1->next != NULL) {
push_back(front1->data);
Node* q = front1;
front1 = front1->next;
delete (q);
}
// Remove the first element from front1
Node* q = front1;
front1 = front1->next;
delete q;
}
}

// Function to display the elements of the deque


void display() {
cout << "Deque: ";
Node* q = front1;
while (q != NULL) {
cout << q->data << " ";
q = q->next;
}
q = front2;
while (q != NULL) {
cout << q->data << " ";
q = q->next;
}
cout << endl;
}

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>

using namespace std;

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";

}
}
}

10. Split two single dequeues in three equal dequeues.

#include <iostream>
using namespace std;
//Split two single dequeues in three equal dequeues
struct node {
int data;
node *link;
};

node *left1 = NULL, *right1 = NULL;


node *left2 = NULL, *right2 = NULL;

void insert_right1(int data);


void insert_right2(int data);
void display1();
void display2();
void concatenate();
void split();
int main() {
int choice;
while (true) {
cout << "1. Insert at right of dequeue 1" << endl;
cout << "2. Display dequeue 1" << endl;
cout << "3. Insert at right of dequeue 2" << endl;
cout << "4. Display dequeue 2" << endl;
cout << "5. Concatenate" << endl;
cout << "6. Split" << endl;
cout << "7. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;

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 insert_right1(int data) {


node *temp = new node;
temp->data = data;
temp->link = NULL;
if (left1 == NULL) {
left1 = temp;
right1 = temp;
} else {
right1->link = temp;
right1 = temp;
}
}

void insert_right2(int data) {


node *temp = new node;
temp->data = data;
temp->link = NULL;
if (left2 == NULL) {
left2 = temp;
right2 = temp;
} else {
right2->link = temp;
right2 = temp;
}
}

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;

cout << "Enter ur choice: " ;


cin >> choice;
switch (choice)
{
case 1:
queue1();
break;

case 2:
queue2();
break;

case 3:

count();
cout<< "Total number of elements you can enter in queue 3 are: "<< c <<endl;

cout<<" Enter the elements in the queue 3"<<endl;


for(int i = 0; i < c; i++){
cout<< "Enter the elements: ";
cin>>m;
insert(f3,r3,m);
}

cout << "Elements in queue 3 are: ";


display(f3, r3);
break;

case 4:
exit(1);

default : cout<<"Invalid choice";


break;

}
}

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;

cout << "Enter ur choice: " ;


cin >> choice;
switch (choice)
{
case 1:
cout << "Enter how many Elements to add to Queue: " << endl;
cin>>n;
for(int i = 0; i<n; i++){
cout<<"Enter the element: ";
cin>>m;
insert(f1,r1,m);
}
break;

case 2:
del(f1,r1);
break;

case 3:
display(f1,r1);
break;

case 4:
return;

default : cout<<"Invalid choice";


break;

}
}
}

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;

cout << "Enter ur choice: " ;


cin >> choice;
switch (choice)
{
case 1:
cout << "Enter how many Elements to add to Queue: " << endl;
cin>>n;
for(int i = 0; i<n; i++){
cout<<"Enter the element: ";
cin>>m;
insert(f2,r2,m);
}
break;

case 2:
del(f2,r2);
break;

case 3:
display(f2,r2);
break;
case 4:
return;

default : cout<<"Invalid choice";


break;

}
}
}

void insert(node*& f, node*& r, int data) {


node* tmp = new node(data);
if (f == NULL) {
f = tmp;
r = tmp;
} else {
r->link = tmp;
r = tmp;
}
}

void del(node*& f, node*& r) {


if (f == NULL) {
cout << "Queue is empty" << endl;
return;
}
node* tmp = f;
f = f->link;
cout << tmp->info << " is deleted from the queue" << endl;
delete tmp;
}

void display(node* f, node* r) {


if (f == NULL) {
cout << "Queue is empty" << endl;
return;
}
node* tmp = f;
while (tmp != NULL) {
cout << tmp->info << " ";
tmp = tmp->link;
}
cout << endl;
}
void count(){
node* tmp1 = f1;
while(tmp1!=NULL){
c++;
tmp1=tmp1->link;
}
tmp1 = f2;
while(tmp1!=NULL){
c++;
tmp1=tmp1->link;
}
}

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

Node* front1 = NULL;


Node* rear1 = NULL;
Node* front2 = NULL;
Node* rear2 = NULL;

void insert_right_deque1(int data);


void insert_right_deque2(int data);
void delete_left_deque1();
void delete_left_deque2();
void display_queue_deque1();
void display_queue_deque2();
void sort_queue_deque1();
void sort_queue_deque2();
void concatenate();

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 insert_right_deque1(int data) {


Node* temp = new Node;
temp->data = data;
temp->next = NULL;
if (front1 == NULL) {
front1 = temp;
rear1 = temp;
} else {
rear1->next = temp;
rear1 = temp;
}
}

void insert_right_deque2(int data) {


Node* temp = new Node;
temp->data = data;
temp->next = NULL;
if (front2 == NULL) {
front2 = temp;
rear2 = temp;
} else {
rear2->next = temp;
rear2 = temp;
}
}

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

Node* current = front1;


Node* index = NULL;
int temp;

while (current != NULL) {


index = current->next;

while (index != NULL) {


if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
cout << "First queue sorted successfully." << endl;
}

void sort_queue_deque2() {
if (front2 == NULL) {
cout << "Second queue is empty. No elements to sort." << endl;
return;
}

Node* current = front2;


Node* index = NULL;
int temp;

while (current != NULL) {


index = current->next;

while (index != NULL) {


if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
cout << "Second queue sorted successfully." << endl;
}

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;

cout << "Concatenated queue elements: ";


Node* temp = front1;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << 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;

while (temp != NULL) {


count++;
temp = temp->link;
}

int middlePosition = count / 2;


if (count % 2 == 0) {
middlePosition--;
}

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

You might also like