implementation of Queue using Linked List.

 Code:-


#include<stdio.h>
#include<stdlib.h>
int *queue=NULL;
struct node
{
int data;
struct node *link;
};

// function for create node
struct node *createlinklist()
{
struct node *t;
t=(struct node *)malloc(sizeof(struct node));
return t;
}

// function for insert elements
void insert()
{
struct node *t,*tem;
tem=createlinklist();
printf(“Enter a elementn”);
scanf(“%d”,&tem->data);
tem->link=NULL;
t=queue;
if(queue==NULL)
queue=tem;
else
{
while(t->link!=NULL)
t=t->link;
t->link=tem;
}
}

// function for deletion
void delete()
{
struct node *t;
if(queue==NULL)
printf(“Queue is underflown”);
else
{
t=queue;
printf(“%d is deletedn”,t->data);
queue=t->link;
}
}

// function for display element
void display()
{
struct node *t;
t=queue;
if(queue==NULL)
printf(“Queue is underflown”);
else
{
while(t!=NULL)
{
printf(“%d “,t->data);
t=t->link;
}
}
}
int main()
{
int ch;
printf(” 1. Insert element into queuen”);
printf(” 2. Delete element from the queuen”);
printf(” 3. display the elements of the queuen”);
printf(” 4. Exitn”);
while(1)
{
printf(“Enter your choicen”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf(“Wrong keyn”);
}
}
return 0;
}

Output:-

1. Insert element into queue
2. Delete elemetn from the queue
3. display the element of the queue
4. Exit
Enter your choice
1
Enter a element
32
Enter your choice
1
Enter a element
21
Enter your choice
3
32 21
Enter your choice
2
32 is Deleted
Enter your choice
4



Leave a Reply

Your email address will not be published.