I have tried to implement all the queue operations in a single code using linked list. If you are not familiar with linked list then you are suggested to look at my previous post illustrating linked list only. thank you.
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
}*node;
struct head
{
int count;
struct node *frnt;
struct node *rear;
}*head;
void create_node()
{
node = (struct node*) malloc (sizeof(struct node));
node->next = NULL;
}
void create_head()
{
head = (struct head*) malloc (sizeof(struct head));
head->count = 0;
head->frnt = NULL;
head->rear = NULL;
}
void enqueue(int data)
{
create_node();
if(head->count==0)
{
node->data = data;
head->frnt = node;
head->rear = node;
}
else
{
node->data = data;
head->rear->next = node;
head->rear = node;
}
head->count++;
}
void dequeue()
{
if(head->count==1)
{
free(head->frnt);
head->frnt = NULL;
head->rear = NULL;
}
else
{
struct node *temp;
temp = head->frnt;
head->frnt = temp->next;
free(temp);
}
head->count--;
}
void print()
{
struct node *temp;
temp = head->frnt;
cout<<"\tThe data are : ";
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
cout<<"\tTotal number of data in queue is : "<<head->count<<endl;
}
void isempty()
{
if(head->count==0)
{
cout<<"\tThe queue is empty"<<endl;
}
else
{
cout<<"\tThe queue has currently "<<head->count<<" number of data"<<endl;
}
}
int size()
{
cout<<"\tThe size of the queue is : "<<head->count<<endl;
}
void front()
{
cout<<"\tThe front data is : "<<head->frnt->data<<endl;
}
void rear()
{
cout<<"\tThe rear data is : "<<head->rear->data<<endl;
}
void swap1(int a,int b)
{
struct node *temp,*temp1,*temp2;
int s=0,s1=0;
temp = head->frnt;
while(temp!=NULL)
{
if(temp->data==a)
{
s=1;
temp1 = temp;
}
if(temp->data == b)
{
s1=1;
temp2 = temp;
}
temp = temp->next;
}
if(s==1 && s1==1)
{
cout<<"\n\tBefore Swapping :\n";
print();
temp1->data = b;
temp2->data = a;
cout<<"\n\tAfter Swapping :\n";
print();
}
else
{
cout<<"\n\tOOPS!!!!!!!! Not Found."<<endl;
}
}
void destroy()
{
while(head->count!=0)
{
dequeue();
}
}
int main()
{
create_head();
int n,s,a,b;
cout<<"\t************************"<<endl;
cout<<"\tOVERALL QUEUE OPERATIONS"<<endl;
cout<<"\t************************"<<endl;
while(1)
{
cout<<"\n\t->1.Enqueue\n\t->2.Dequeue\n\t->3.Destroy\n\t->4.Head->front\n\t->5.Head->rear\n\t->6.Queue size\n\t->7.Isempty Or Not\n\t->8.Swap two numbers\n\t->9.Print Queue\n\t->10.Exit\n"<<endl;
cout<<"\tPlease enter your choice : ";
cin>>n;
switch(n)
{
case 1:
cout<<"\n\tEnter a data to enqueue : ";
cin>>s;
enqueue(s);
break;
case 2:
dequeue();
break;
case 3:
destroy();
break;
case 4:
front();
break;
case 5:
rear();
break;
case 6:
system("COLOR D5");
size();
break;
case 7:
isempty();
break;
case 8:
cout<<"\n\tPlease enter two numbers to swap : ";
cin>>a>>b;
swap1(a,b);
break;
case 9:
system("COLOR 6C");
print();
break;
case 10:
system("COLOR E9");
exit(0);
default:
cout<<"OOPS! Follow input criterion."<<endl;
break;
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
}*node;
struct head
{
int count;
struct node *frnt;
struct node *rear;
}*head;
void create_node()
{
node = (struct node*) malloc (sizeof(struct node));
node->next = NULL;
}
void create_head()
{
head = (struct head*) malloc (sizeof(struct head));
head->count = 0;
head->frnt = NULL;
head->rear = NULL;
}
void enqueue(int data)
{
create_node();
if(head->count==0)
{
node->data = data;
head->frnt = node;
head->rear = node;
}
else
{
node->data = data;
head->rear->next = node;
head->rear = node;
}
head->count++;
}
void dequeue()
{
if(head->count==1)
{
free(head->frnt);
head->frnt = NULL;
head->rear = NULL;
}
else
{
struct node *temp;
temp = head->frnt;
head->frnt = temp->next;
free(temp);
}
head->count--;
}
void print()
{
struct node *temp;
temp = head->frnt;
cout<<"\tThe data are : ";
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
cout<<"\tTotal number of data in queue is : "<<head->count<<endl;
}
void isempty()
{
if(head->count==0)
{
cout<<"\tThe queue is empty"<<endl;
}
else
{
cout<<"\tThe queue has currently "<<head->count<<" number of data"<<endl;
}
}
int size()
{
cout<<"\tThe size of the queue is : "<<head->count<<endl;
}
void front()
{
cout<<"\tThe front data is : "<<head->frnt->data<<endl;
}
void rear()
{
cout<<"\tThe rear data is : "<<head->rear->data<<endl;
}
void swap1(int a,int b)
{
struct node *temp,*temp1,*temp2;
int s=0,s1=0;
temp = head->frnt;
while(temp!=NULL)
{
if(temp->data==a)
{
s=1;
temp1 = temp;
}
if(temp->data == b)
{
s1=1;
temp2 = temp;
}
temp = temp->next;
}
if(s==1 && s1==1)
{
cout<<"\n\tBefore Swapping :\n";
print();
temp1->data = b;
temp2->data = a;
cout<<"\n\tAfter Swapping :\n";
print();
}
else
{
cout<<"\n\tOOPS!!!!!!!! Not Found."<<endl;
}
}
void destroy()
{
while(head->count!=0)
{
dequeue();
}
}
int main()
{
create_head();
int n,s,a,b;
cout<<"\t************************"<<endl;
cout<<"\tOVERALL QUEUE OPERATIONS"<<endl;
cout<<"\t************************"<<endl;
while(1)
{
cout<<"\n\t->1.Enqueue\n\t->2.Dequeue\n\t->3.Destroy\n\t->4.Head->front\n\t->5.Head->rear\n\t->6.Queue size\n\t->7.Isempty Or Not\n\t->8.Swap two numbers\n\t->9.Print Queue\n\t->10.Exit\n"<<endl;
cout<<"\tPlease enter your choice : ";
cin>>n;
switch(n)
{
case 1:
cout<<"\n\tEnter a data to enqueue : ";
cin>>s;
enqueue(s);
break;
case 2:
dequeue();
break;
case 3:
destroy();
break;
case 4:
front();
break;
case 5:
rear();
break;
case 6:
system("COLOR D5");
size();
break;
case 7:
isempty();
break;
case 8:
cout<<"\n\tPlease enter two numbers to swap : ";
cin>>a>>b;
swap1(a,b);
break;
case 9:
system("COLOR 6C");
print();
break;
case 10:
system("COLOR E9");
exit(0);
default:
cout<<"OOPS! Follow input criterion."<<endl;
break;
}
}
return 0;
}
No comments:
Post a Comment