Header Ads Widget

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 element\n");
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 underflow\n");
else
{
t=queue;
printf("%d is deleted\n",t->data);
queue=t->link;
}
}

// function for display element
void display()
{
struct node *t;
t=queue;
if(queue==NULL)
printf("Queue is underflow\n");
else
{
while(t!=NULL)
{
printf("%d ",t->data);
t=t->link;
}
}
}
int main()
{
int ch;
printf(" 1. Insert element into queue\n");
printf(" 2. Delete element from the queue\n");
printf(" 3. display the elements of the queue\n");
printf(" 4. Exit\n");
while(1)
{
printf("Enter your choice\n");
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 key\n");
}
}
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




Post a Comment

0 Comments