You are on page 1of 11

Linked list insertion at beginning

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node*link;
};
struct node*first;
void main()
{
void create();
void insert_end();
void traverse();
clrscr();
create();
insert_end();
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Input first node information\n");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof (struct node));
printf("Input next node information\n");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("press Y/N for more node\n");
ch=getch();
}while(ch=='y');
ptr->link=NULL;
}
void traverse()
{
struct node*ptr;
printf("Traversing of linked list:\n");
ptr=first;
while(ptr!=NULL)
{
printf("Data:\t%d\n",ptr->info);

printf("Link:\t%d\n",ptr->link);
ptr=ptr->link;
}
}
void insert_end()
{
struct node *ptr,*cpt;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Input insertion-node information\n");
scanf("%d",&ptr->info);
printf("After insertion at end\n");
cpt=first;
while(cpt->link!=NULL)
cpt=cpt->link;
cpt->link=ptr;
ptr->link=NULL;
}

OUTPUT

OUTPUT

Linked list insertion at end


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node*link;
};
struct node*first;
void main()
{
void create();
void insert_end();
void traverse();
clrscr();
create();
insert_end();
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("input first node information\n");

scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof (struct node));
printf("input next node information\n");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("press Y/N for more node\n");
ch=getch();
}while(ch=='y');
ptr->link=NULL;
}
void traverse()
{
struct node*ptr;
printf("traversing of linked list:\n");
ptr=first;
while(ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->link;
}
}
void insert_end()
{

struct node *ptr,*cpt;


ptr=(struct node*)malloc(sizeof(struct node));
printf("input new node information\n");
scanf("%d",&ptr->info);
cpt=first;
while(cpt->link!=NULL)
cpt=cpt->link;
cpt->link=ptr;
ptr->link=NULL;
}

OUTPUT

Linked list insertion at a given location


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node*link;
};
struct node*first;
void main()
{
void create();
void insert_given();
void traverse();
clrscr();
create();
insert_given();
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Input first node information\n");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof (struct node));
printf("Input next node information\n");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("press Y/N for more node\n");
ch=getch();
}while(ch=='y');
ptr->link=NULL;
}
void traverse()
{
struct node*ptr;
printf("Traversing of linked list:\n");
ptr=first;
while(ptr!=NULL)
{
printf("Data:\t%d\n",ptr->info);

printf("Link:\t%d\n",ptr->link);
ptr=ptr->link;
}
}
void insert_given()
{
struct node *ptr,*cpt;
int data;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Input insertion-node information\n");
scanf("%d",&ptr->info);
printf("Input information of the node after which insertion is to be made\n");
scanf("%d",&data);
cpt=first;
while(cpt->info!=data)
cpt=cpt->link;
ptr->link=cpt->link;
cpt->link=ptr;
}

OUTPUT

You might also like