/*Serial No.144 [swami126.cpp]*/
#include<stdio.h>
#include<conio.h>
#define maxsize 4
int front=-1,rear=-1,cqueue[maxsize];
void enqueue()
{
if((front==0&&rear==(maxsize-1))|(front==rear+1))
{
printf("\nQueue is full. Value can't be inserted.");
return;
}
else
{
int ele;
printf("\nEnter element to insert= ");
scanf("%d",&ele);
if(rear==maxsize-1) /*if it is first node*/
rear=0;
else /*if it is other than first node*/
rear++;
cqueue[rear]=ele;
if(front==-1)
front++;
}
}
void display()
{
int i;
printf("\nElements in the list are:\n ");
if(front==-1)
printf("\nQueue is Empty.");
else if(front==rear) /*if only single node is present.*/
{
printf("%d ",cqueue[rear]);
return;
}
else /*if more than one node present*/
{
if(front<rear)
{
for(int i=front;i<=rear;i++)
printf("%d ",cqueue[i]);
}
else
{
for(i=front;i<maxsize;i++)
printf("%d ",cqueue[i]);
for(i=0;i<=rear;i++)
printf("%d ",cqueue[i]);
}
}
}
void dequeue()
{
if(front==-1)
{
printf("\nQueue is Empty. Element can't be deleted.");
return;
}
if(front==rear) /*if single node is present*/
{
front=rear=-1;
return;
}
if(front==maxsize-1)
front=0;
else /*if more than one node present*/
front++;
}
void main()
{
int choice;
clrscr();
printf("\n\t[1] Enqueue");
printf("\n\t[2] Dequeue");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
do
{
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
}
}while(choice!=7);
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...