-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedli.c
231 lines (222 loc) · 4.78 KB
/
linkedli.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include<stdio.h>
#include<stdlib.h>
struct node{
int info;
struct node* link;
};
struct node *insert(struct node*,int data);
struct node *del(struct node *);
struct node *insertafter(struct node *,int data,int pos);
struct node *del_after(struct node *,int pos);
struct node *insertbefore(struct node *,int data,int after);
struct node *del_before(struct node *,int data);
struct node *reverse(struct node *);
void display(struct node *);
int main(){
struct node *start = NULL;
int i,choice,data,pos;
printf("1.insert element\t2.insert after any element\t3.insert before any element\t4.delete last ele\t5.delete ele before any ele\t6.delete any ele after any ele\t7.reverse\t8.disply\t9.exit\n");
while(1){
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("enter data\n");
scanf("%d",&data);
start = insert(start,data);
break;
case 2:
printf("enter data n ele after \n");
scanf("%d",&data);
scanf("%d",&pos);
start = insertafter(start,data,pos);
break;
case 3:
printf("enter data n ele before\n");
scanf("%d",&data);
scanf("%d",&pos);
start = insertbefore(start,data,pos);
break;
case 4:
start = del(start);
break;
case 5:
printf("enter ele after\n");
scanf("%d",&data);
start = del_after(start,data);
break;
case 6:
printf("enter ele before\n");
scanf("%d",&data);
start = del_before(start,data);
break;
case 7:
start = reverse(start);
display(start);
break;
case 8:
display(start);
break;
default:
exit(0);
}
}
return 0;
}
struct node *insert(struct node* start,int data){
struct node *tmp,*ptr = start;
tmp = (struct node*)malloc(sizeof(struct node));
tmp->info = data;
tmp->link = NULL;
if(start == NULL){
start = tmp;
return start;
}
while(ptr->link !=NULL)
ptr=ptr->link;
ptr->link = tmp;
return start;
}
struct node *del(struct node *start){
struct node *tmp,*ptr = start;
if(start == NULL){
printf("no element to delete\n");
return start;
}
while(ptr->link->link !=NULL)
ptr=ptr->link;
tmp = ptr->link;
ptr->link=tmp->link;
free(tmp);
return start;
}
struct node *insertafter(struct node *start,int data,int item){
struct node *tmp,*ptr = start;
tmp = (struct node*)malloc(sizeof(struct node));
tmp->info = data;
tmp->link = NULL;
if(start == NULL){
printf("no element\n");
return start;
}
while(ptr!=NULL && ptr->info != item)
ptr=ptr->link;
if(ptr->info == item){
tmp->link = ptr->link;
ptr->link = tmp;
}
else if(ptr == NULL)
printf("not found %d ele\n",item);
return start;
}
struct node *del_after(struct node *start,int item){
struct node *tmp,*ptr = start;
if(start == NULL){
printf("no element to delete\n");
}
else if(start->info == item){
tmp = start;
start = start->link;
free(tmp);
return start;
}
while(ptr!=NULL && ptr->info != item)
ptr=ptr->link;
if(ptr->info == item){
tmp = ptr->link;
ptr->link = tmp->link;
free(tmp);
}
else if(ptr == NULL)
printf("not found %d ele to delete after it\n",item);
return start;
}
struct node *insertbefore(struct node *start,int data,int item){
struct node *tmp,*ptr = start,*q;
tmp = (struct node*)malloc(sizeof(struct node));
tmp->info = data;
tmp->link = NULL;
if(start == NULL){
printf("no element\n");
return start;
}
else if(start->info == item){
tmp->link=start;
start = tmp;
return start;
}
q=ptr->link;
while(q!=NULL && q->info != item){
ptr=ptr->link;
q = q->link;
}
if(q->info == item){
tmp->link = ptr->link;
ptr->link = tmp;
return start;
}
else if(q == NULL)
printf("no %d ele to insert before\n",item);
return start;
}
struct node *del_before(struct node *start,int item){
struct node *tmp,*ptr = start,*q;
if(start == NULL){
printf("no element to delete\n");
return start;
}
else if(start->info == item){
printf("not found %d ele to delete before it\n",item);
return start;
}
else if(start->link->info == item){
tmp = start;
start=start->link;
free(tmp);
return start;
}
q=ptr->link->link;
while(q!=NULL && q->info != item){
q=q->link;
ptr=ptr->link;
}
if(q->info == item){
tmp = ptr->link;
ptr->link = q;
free(tmp);
}
else if(q == NULL)
printf("not found %d ele to delete before it\n",item);
return start;
}
struct node *reverse(struct node *start){
struct node *prev=NULL,*ptr=start,*next;
if(start == NULL){
printf("no element to reverse\n");
return start;
}
else if(start->link == NULL){
return start;
}
while(ptr !=NULL){
next = ptr->link;
ptr->link = prev;
prev=ptr;
ptr=next;
}
start=prev;
return start;
}
void display(struct node *start){
struct node *ptr = start;
if(start == NULL){
printf("no element to display\n");
}
else{
while(ptr!=NULL){
printf("%d ",ptr->info);
ptr=ptr->link;
}
}
printf("\n");
}