You are on page 1of 6

11.

Write a C Program using dynamic variables and pointers to


construct a queue of integers using linked list and to
perform the following operations:
a: Insert
b: Delete
c: Display
The program should print appropriate messages for queue full
and queue empty.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct node
{
int info;
struct node *link;
};

typedef struct node NODE;

NODE *start=NULL;

void ins()
{

NODE *tmp,*curptr;
int item;
tmp=(NODE*)malloc(sizeof(NODE));
if(tmp==NULL)
{
printf("\nQueue Overflow..\n");
getch();
return;
}
printf("\nEnter the element to be inserted\n");
scanf("%d",&item);
tmp->info=item;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
for(curptr=start;curptr->link!=NULL;curptr=curptr->link)
{
}
curptr->link=tmp;
}
printf("\n%d Inserted..\n",tmp->info);
getch();
}
void del()
{
NODE *tmp;
int item;
if(start==NULL)
{
printf("\nQueue Underflow..\n");
getch();
return;
}
printf("\nElement to be Deleted is %d",start->info);
item=start->info;
tmp=start;
start=start->link;
free(tmp);
printf("\nElement Deleted is %d",item);
getch();
}

void display()
{
NODE *tmp;
if(start==NULL)
{
printf("\nQueue is empty..\n");
getch();
return;
}
printf("\nQueue elements are:\n");
for(tmp=start;tmp!=NULL;tmp=tmp->link)
{
printf("\n%d",tmp->info);
}
getch();
}

void main()
{
int ch;
for(;;)
{
clrscr();
printf("\nMenu\n\n1.Insert\n2.Delete\n3.Display\n4.exit\nEnter your choice..\n");
scanf("%d",&ch);

switch(ch)
{
case 1:ins();break;
case 2:del();break;
case 3:display();break;
case 4: exit(0);
default:printf("\nWrong menu choice\n");
getch();
}
}
}

OUTPUT

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
3

Queue is empty

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
1

Enter the element to be inserted


11

11 Inserted

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
1

Enter the element to be inserted


22

22 Inserted

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
1

Enter the element to be inserted


33
33 Inserted

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
1

Enter the element to be inserted


44

44 Inserted

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
1

Enter the element to be inserted


55

55 Inserted

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
3

Queue elements are:

11
22
33
44
55

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
2

Element to be Deleteped is 55

Element Deleteped is 55

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
2

Element to be Deleteped is 44

Element Deleteped is 44

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
2

Element to be Deleteped is 33

Element Deleteped is 33

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
2

Element to be Deleteped is 22

Element Deleteped is 22

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
2

Element to be Deleteped is 11

Element Deleteped is 11

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
2

Queue Underflow

Menu

1.Insert
2.Delete
3.Display
4.exit
Enter your choice..
4