Sunday, May 22, 2011

Program to implement the following with the help of linear queue of integers: a) Insert the element in queue. b) Delete the element from the queue. c) Display queue after insert and delete operations d) Exit.

#include /*Header File*/
# include /*Header File*/
# define size 10 /*Header File*/
int front,rear;
main()
{
int Queue[size],elem,ch; /*Declaration of variables*/
void disp (int[]);
void insert (int[],int);
rear=-1;
front=-1;
while(1)
{
printf ("\n Main Menu");
printf ("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter element: ");
scanf("%d",&elem);
insert(Queue,elem);
break;
case 2: ele=delete (Queue);
printf("\n Deleted element is %d",elem);
break;
case 3: display (Queue);
break;
case 4: exit(0);
break;
default: printf("\n Wrong Choice"); /*Default Statement*/
break;
}
}
}

void insert (int Queue[],int elem)
{
if (rear==size-1)
printf("\n Queue is full");
else
if (rear==-1)
{
rear=0; front=0;
Queue[rear]=elem;
}
else
rear=rear+1;
Queue[rear]=elem;
}

int delete (int Queue[])
{
int elem;
if (front==-1)
{
printf("\n Queue is empty");
return(0);
}
else
{
elem =Queue[front];
if (front==rear)
{
front=-1;
rear=-1;
}
else
front=front+1;
return(elem);
}
}

void disp(int Queue[])
{
int count;
for ( count=front; count<=rear;count++)
printf("\t%d", Queue[count]);
}

OUTPUT

Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1

Enter the value of item: 34
Element inserted
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Elements are: 34
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Element deleted is: 34
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4

No comments:

Post a Comment