Insertion in Queue of type Linked list by R4R Team


program-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL;
void create()
{
struct node *ptr,*cpt;
char ch;
printf("Enter item\n");
ptr=(struct node*)malloc(sizeof(struct node));
scanf("%d",&ptr->data);
front=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter item\n");
scanf("%d",&cpt->data);
ptr->next=cpt;
ptr=cpt;
printf("Continue(y/n)?");
ch=getch();
}while(ch=='y');
ptr->next=NULL;
rear=ptr;
}
void display()
{
struct node *ptr;
ptr=front;
printf("Queue is : \n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}

void insertion()
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter item to insert\n");
scanf("%d",&ptr->data);
rear->next=ptr;
ptr->next=NULL;
rear=ptr;
}

void main()
{
clrscr();
create();
display();
insertion();
display();
getch();
}

output-

Enter item
9
Enter item
5
Continue(y/n)?
Enter item
1
Continue(y/n)?
Enter item
6
Continue(y/n)?
Enter item
8
Continue(y/n)?
Queue is :
9 5 1 6 8
Enter item to insert
2
Queue is :
9 5 1 6 8 2

-In this program, we have three function create() and traverse(),insertion().
- create() : is used to create a queue using linked list.
- display() : is used to traverse the queue from front to rear.
- insertion() : is used to insert the item in the queue at rear side.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!