-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlinklistinsertion.c
More file actions
105 lines (103 loc) · 2.06 KB
/
linklistinsertion.c
File metadata and controls
105 lines (103 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int info;
struct Node *link;
} Node;
void Display();
void Ins(int,int);
Node *start;
int main()
{
start=NULL;
int ch1,ch2,e,pos;
while(1)
{
printf("\nEnter 1 to insert\nEnter 2 to display\nEnter 3 to exit\nChoice : ");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter Value to be inserted : ");
scanf("%d",&e);
printf("\nEnter 1 to insert at the beginning\nEnter 2 to insert at the back\nEnter 3 to insert at a position\nChoice : ");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
Ins(1,e);
break;
case 2:
Ins(-1,e);
break;
case 3:
printf("Enter position : ");
scanf("%d",&pos);
Ins(pos,e);
break;
}
break;
case 2:
Display();
break;
case 3:
exit(0);
break;
default:
printf("Invalid Choice");
break;
}
}
return 0;
}
void Ins(int pos,int val)
{
int c=1;
Node *tmp,*q,*p;
tmp=(Node*)malloc(sizeof(Node));
tmp->info=val;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
if(pos==1)
{
tmp->link=start;
start=tmp;
}
else if(pos==-1)
{
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
else
{
while(q->link!=NULL)
{
if(c==pos-1)
break;
c+=1;
q=q->link;
}
p=q;
q=q->link;
p->link=tmp;
tmp->link=q;
}
}
}
void Display()
{
Node *q;
q=start;
while(q!=NULL)
{
printf("%d ",q->info);
q=q->link;
}
printf("\n\n");
}