-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.c
More file actions
274 lines (262 loc) · 4.67 KB
/
linkedlist.c
File metadata and controls
274 lines (262 loc) · 4.67 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *first=NULL;
struct node *third=NULL;
struct node *second=NULL;
void create(int a[],int n)
{
int i;
struct node *t,*last;
first=(struct node*)malloc(sizeof(struct node));
first->data=a[0];
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
t=(struct node*)malloc(sizeof(struct node));
t->data=a[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void display(struct node*p)
{
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
void rec_display(struct node *p)
{
if(p!=NULL)
{
printf("%d ",p->data);
rec_display(p->next);
}
}
void reverse(struct node*p)
{
if(p!=NULL)
{
reverse(p->next);
printf("%d ",p->data);
}
}
int count(struct node *p)
{
if(p=0)
return 0;
else
return count(p->next)+1;
}
int add(struct node *p)
{
int sum=0;
while(p)
{
sum=sum+p->data;
p=p->next;
}
return sum;
}
int max(struct node*p)
{
int max=-__INT32_MAX__;
while(p)
{
if(p->data>max)
{
max=p->data;
p=p->next;
}
}
return max;
}
struct node * search(struct node*p,int key)
{
while(p)
{
if(key==p->data)
return p;
p=p->next;
}
return NULL;
}
void insert(struct node*p,int pos,int x)
{
struct node*t;
if(pos==0)
{
t=(struct node*)malloc(sizeof(struct node));
t->data=x;
t->next=first;
first=t;
}
else if(pos>0)
{
int i=0;
p=first;
for(i=0;i<pos-1&&p;i++)
p=p->next;
if(p)
{
t=(struct node*)malloc(sizeof(struct node));
t->data=x;
t->next=p->next;
p->next=t;
}
}
//printf("hi");
}
int delete(struct node *p,int pos)
{
struct node *q,*r;
int x=-1,i;
if(pos==1)
{
x=p->data;
q=p;
p=p->next;
free(q);
}
else
{
q=p;
r=NULL;
for(i=0;i<pos-1;i++)
{
r=q;
q=q->next;
}
if(q)
{
x=q->data;
r->next=q->next;
free(q);
}
}
}
void sorted_insert(struct node *p,int x)//problem
{
struct node *q;
q=NULL;
struct node*t;
t=(struct node*)malloc(sizeof(struct node));
t->data=x;
t->next=NULL;
if(p==NULL)
first=t;
else
{
while(p&&p->data<x)
{
q=p;
p=p->next;
}
if(p==first)
{
t->next=first;
first=t;
}
else{
t->next=q->next;
q->next=t;
}
}
}
void remove_duplicate()
{
struct node *p,*q;
p=first;q=p->next;
while(q!=NULL)
{
if(q->data!=p->data)
{
p=q;
q=q->next;
}
else
{
p->next=q->next;
free(q);
q=p->next;
}
}
}
/*void create2(int a[],int n)
{
int i;
struct node *t,*last;
second=(struct node*)malloc(sizeof(struct node));
second->data=a[0];
second->next=NULL;
last=second;
for(i=1;i<n;i++)
{
t=(struct node*)malloc(sizeof(struct node));
t->data=a[i];
t->next=NULL;
last->next=t;
last=t;
}
}*/
void merge(struct node*p,struct node *q)//requires two array and one more create function to create a second list//
{
struct node*last;
if(p->data<q->data)
{
third=last=p;
p=p->next;
third->next=NULL;
}
else
{
third=last=q;
q=q->next;
third->next=NULL;
}
while(p &&q)
{
if(p->data < q->data)
{
last->next=p;
last=p;
p=p->next;
last->next=NULL;
}
else
{
last->next=q;
last=q;
q=q->next;
last->next=NULL;
}
if(p)
last->next=p;
if(q)
last->next=q;
}
}
void main()
{
int a[]={3,4,5,6,8};
create(a,5);
display(first);
// printf("\n");
//reverse(first);
count(first);
add(first);
max(first);
struct node *t= search(first,5);
insert(first,2,7);
// sorted_insert(first,7);
display(first);
//delete(first,3);
}