Queue using Linked List

Queue using Linked List :

#include<stdio.h>
#include<malloc.h>
void insert();
void delete();
void display();
int x;
struct node
{
int info;
struct node *next;
};

struct node *f=NULL;
struct node *r=NULL;
void main()
{
int ch;
printf("1-Insertion\n2-Deletion\n3-Display\nEnter your choice\n");
scanf("%d",&ch);
do
{
switch(ch)
{
case 1:
insert();
break;

case 2:
delete();
break;

case 3:
display();
break;
}
printf("Enter your choice\n");
scanf("%d",&ch);
}while(ch!=0);
}

void insert()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter number\n");
scanf("%d",&x);
temp->info=x;
temp->next=NULL;
if(f==NULL)
{
f=r=temp;
}
else
{
r->next=temp;
r=temp;
}
}

void delete()
{
struct node *temp=f;
if(f==NULL)
{
printf("Queue is empty\n");
}
else
{
f=f->next;
printf("%d\n",temp->info);
free(temp);
}
}

void display()
{
struct node *temp=f;
if(f==NULL)
{
printf("Queue is empty\n");
}
else
{
while(temp->next!=NULL)
{
printf("%d ",temp->info);
temp=temp->next;
}
}
printf("%d ",temp->info);
}

Post a Comment

0 Comments