Data structure Program for queue implementation using linked list source code
/* Program for queue implementation using linked list */
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;
void insert()
{
int n;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("\nenter the element u want to insert:- ");
scanf("%d",&n);
temp->info=n;
temp->link=NULL;
if(front==NULL)
front=temp;
else
rear->link=temp;
rear=temp;
}
void del()
{
struct node *temp;
temp=front;
if(front==NULL)
printf("\nqueue is empty");
else
{
printf("\n%d is deleted from queue\n",front->info);
front=front->link;
free(temp);
}
}
void display()
{
struct node *temp;
temp=front;
if(front==NULL || front==rear+1)
printf("\nqueueis empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for display");
printf("\n4:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
/* Program for queue implementation using linked list */
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;
void insert()
{
int n;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("\nenter the element u want to insert:- ");
scanf("%d",&n);
temp->info=n;
temp->link=NULL;
if(front==NULL)
front=temp;
else
rear->link=temp;
rear=temp;
}
void del()
{
struct node *temp;
temp=front;
if(front==NULL)
printf("\nqueue is empty");
else
{
printf("\n%d is deleted from queue\n",front->info);
front=front->link;
free(temp);
}
}
void display()
{
struct node *temp;
temp=front;
if(front==NULL || front==rear+1)
printf("\nqueueis empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for display");
printf("\n4:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
0 comments: