#include<stdio.h>
#include<stdlib.h>
void insert();
void display();
int length();
struct node
{
int data;
struct node* next;
};
root=NULL;
void insert()
{
int loc;
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter Data\n");
scanf("%d",&temp->data);
temp->next=NULL;
struct node* p;
p=root;
printf("Enter location where you want to insert node \n");
scanf("%d",&loc);
if(root==NULL)
root=temp;
else if(loc>length())
printf("insertion not possible\n");
else if(loc==1)
{
temp->next=root;
root=temp;
}
else
{
int i;
for(i=1;i<loc-1;i++)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
}
}
int length()
{
struct node *p;
p=root;
int count =0;
while(p!=NULL)
{
count++;
p=p->next;
}
return count;
}
void display()
{
struct node * temp;
temp=root;
if(temp==NULL)
printf("List is Empty\n");
else
{
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
}
int main()
{
int ch,len;
while(1)
{
printf("1.Insert element between the Link list\n");
printf("2.Length of the list\n");
printf("3.Display element\n");
printf("4.exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 3:
display();
break;
case 4:
exit(1);
case 2:
len =length();
printf("%d\n",len);
break;
default:
printf("Please enter right choice\n");
}
}
}
0 Comments