/*Serial No.142 [swami124.cpp]*/
#include<stdio.h>
#include<conio.h>
#define maxsize 4
int front=-1,rear=-1;
int queue[maxsize];
void reverse()
{
if(front==-1&&rear==-1)
printf("\nQueue is empty.");
else
{
printf("\Queue elements are reversed.");
int j=rear,temp;
for(int i=front;i<j;i++,j--)
{
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
void add()
{
if(rear>=maxsize-1)
{
printf("\nQueue is full. Value can't be added.");
return;
}
int ele;
printf("\nEntere the element to insert= ");
scanf("%d",&ele);
if(front==-1)
{
front++;
}
rear++;
queue[rear]=ele;
}
void display()
{
if(front==-1&&rear==-1)
printf("\nQueue is empty.");
else
{
printf("\nElements in Queue are:\n");
for(int i=front;i<=rear;i++)
{
printf("%d ",queue[i]);
}
}
}
void delete_que()
{
int ele;
if(front==-1&&rear==-1)
{
printf("\nQueue is empty. Value can't be deleted.");
return;
}
else if(front==rear)
{
ele=queue[front];
front=-1;
rear=-1;
}
else
{
ele=queue[front];
front++;
}
printf("\nDeleted Element= %d",ele);
}
void main()
{
int choice;
clrscr();
printf("\n\t[1] Add");
printf("\n\t[2] Delete");
printf("\n\t[3] Display");
printf("\n\t[4] Reverse Queue");
printf("\n\t[7] Exit");
do
{
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: add();
break;
case 2: delete_que();
break;
case 3: display();
break;
case 4: reverse();
}
}while(choice!=7);
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...