#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}
NODE;
NODE * get_node()
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
printf("Enter data:");
scanf("%d",&p->data);
p->link = NULL;
return p;
}
NODE * create_sll()
{
int n,i;
NODE *first,*last,*q;
printf("Enter no.of nodes:");
scanf("%d",&n);
first = last = NULL;
for(i=1;i<=n;i++)
{
q = get_node();
if(first==NULL)
first = q;
else
last->link = q;
last = q;
}
return first;
}
void display(NODE *h)
{
while(h!=NULL)
{
printf("%4d",h->data);
h=h->link;
}
printf("\n");
}
NODE* insert_last(NODE *h)
{
NODE *q,*p;
q = get_node();
if(h==NULL)
h = q;
else
{
p = h;
while(p->link!=NULL)
p = p->link;
p->link = q;
}
return h;
}
NODE * insert_pos(NODE *h, int pos)
{
NODE *p,*q,*t;
int i;
if(pos<=0)
{
printf("Invalid position: %d\n",pos);
return h;
}
i=1;
p = q = h;
while(p!=NULL && i<pos)
{
q=p;
p=p->link;
i++;
}
if(p==NULL)
{
printf("Invalid position: %d\n",pos);
return h;
}
t = get_node();
if(pos==1)
{
t->link = h;
h = t;
}
else
{
q->link = t;
t->link = p;
}
return h;
}
void main()
{
NODE *h=NULL;
int ch,pos;
while(1)
{
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert at specific position\n");
printf("4.Exit\n");
printf("Enter your choice (1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
h = create_sll();
break;
case 2:
display(h);
break;
case 3:
printf("Enter position to insert node:");
scanf("%d",&pos);
h = insert_pos(h,pos);
break;
case 4:
exit(0);
}
getch();
}
}
Output-
1.Create
2.Display
3.Insert at specific position
4.Exit
Enter your choice (1-4):1
Enter no.of nodes:2
Enter data:1
Enter data:2
1.Create
2.Display
3.Insert at specific position
4.Exit
Enter your choice (1-4):3
Enter position to insert node:2
Enter data:5
1.Create
2.Display
3.Insert at specific position
4.Exit
Enter your choice (1-4):2
1 5 2
1.Create
2.Display
3.Insert at specific position
4.Exit
Enter your choice (1-4):4
Recent Posts
Posted on 2019-07-18
Posted on 2019-07-18
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-05-28
Posted on 2019-05-24
Posted on 2019-05-24
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23