1
+ /* Code written
2
+ @author Shivendra k jha
3
+ some singly linkedlist code like create , display , max , sum , count, searching, insertion using normal method and using recursion
4
+
5
+ */
6
+ #include < stdio.h>
7
+ #include < stdlib.h>
8
+ struct Node
9
+ {
10
+ int data;
11
+ struct Node * next;
12
+ }*first=NULL ;
13
+ void Create (int A[], int n)
14
+ {
15
+ int i;
16
+ struct Node * last, * t;
17
+ first = (struct Node *)malloc (sizeof (struct Node ));
18
+ first->data = A[0 ];
19
+ first->next = NULL ;
20
+ last = first;
21
+
22
+ for (i = 0 ; i < n; i++)
23
+ {
24
+ t = (struct Node *)malloc (sizeof (struct Node ));
25
+ t->data = A[i];
26
+ t->next = NULL ;
27
+ last->next = t;
28
+ last = t;
29
+ }
30
+ }
31
+ void Display (struct Node * p)
32
+ {
33
+ while (p != NULL )
34
+ {
35
+ printf (" ->%d" , p->data );
36
+ p = p->next ;
37
+ }
38
+ }
39
+ void RDisplay (struct Node * p)
40
+ {
41
+ if (p!=NULL )
42
+ {
43
+ printf (" ->%d" , p->data );
44
+ RDisplay (p->next );
45
+ }
46
+ }
47
+
48
+ int count (struct Node * p)
49
+ {
50
+ int c = 0 ;
51
+ while (p != NULL )
52
+ {
53
+ c++;
54
+ p = p->next ;
55
+ }
56
+ return c;
57
+ }
58
+ int Rcount (struct Node * p)
59
+ {
60
+ if (p == 0 ) return 0 ;
61
+ else
62
+ return Rcount (p->next ) + 1 ;
63
+
64
+ }
65
+ int sum (struct Node * p)
66
+ {
67
+ int sum = 0 ;
68
+ while (p != NULL )
69
+ {
70
+ sum += p->data ;
71
+ p = p->next ;
72
+ }
73
+ return sum;
74
+ }
75
+ int Rsum (struct Node * p)
76
+ {
77
+ if (p == 0 )
78
+ return 0 ;
79
+ else
80
+ return Rsum (p->next ) + p->data ;
81
+ }
82
+
83
+ int max (struct Node * p)
84
+ {
85
+ int m = -3276 ;
86
+ while (p != NULL )
87
+ {
88
+ if (m < p->data )
89
+ m = p->data ;
90
+ p = p->next ;
91
+ }
92
+ return m;
93
+ }
94
+ int max1 (struct Node * p)
95
+ {
96
+ int x = 0 ;
97
+ if (p == 0 )
98
+ return INT_MIN;
99
+ else {
100
+ x = max1 (p->next );
101
+
102
+ if (x > p->data )
103
+ return x;
104
+ else return p->data ;
105
+
106
+ }
107
+ }
108
+ int Rmax (struct Node * p)
109
+ {
110
+ int x = 0 ;
111
+ if (p == 0 )
112
+ {
113
+ return INT_MIN;
114
+ }
115
+ else
116
+ x=Rmax (p->next );
117
+ return x > p->data ? x : p->data ; // using ternary operator
118
+ }
119
+
120
+ // Binary search is not possible in linkedlist // Binery search is only for sorted array
121
+
122
+ struct Node * LSearch (struct Node * p, int key)
123
+ {
124
+ struct Node * q=NULL ;
125
+
126
+ while (p != NULL )
127
+ {
128
+ if (key == p->data )
129
+ {
130
+ return p;
131
+ p = p->next ;
132
+ }
133
+
134
+ p = p->next ;
135
+ }
136
+ return NULL ;
137
+
138
+ }
139
+
140
+ struct Node * RSearch (struct Node * p, int key) // using recursion searching the elements in linkes list
141
+ {
142
+ if (p == NULL )
143
+ return NULL ;
144
+ if (key == p->data )
145
+ return p;
146
+ return RSearch (p->next , key);
147
+
148
+ }
149
+
150
+ // move to top approch
151
+ struct Node * Movetotop (struct Node * p, int key)
152
+ {
153
+ // for sending key element to top we need one extrea pointer i.e q
154
+ struct Node * q=NULL ;
155
+
156
+ while (p != NULL )
157
+ {
158
+ if (p->data == key)
159
+ {
160
+ q->next = p->next ;
161
+ p->next = first;
162
+ first = p;
163
+ return p;
164
+ }
165
+ q = p;
166
+ p = p->next ;
167
+
168
+ }
169
+ return NULL ;
170
+ }
171
+
172
+ // Inserting in linkedlist // in array we start index from 0 ll is independent of that
173
+ // we can insert by two pos
174
+ // Insert before first & insert after given position
175
+
176
+ void Insert (int pos, int x) // position at which element will be inserted and data (x) to be inserted
177
+ {
178
+ struct Node * t, * p; // creating node
179
+ if (pos == 0 ) // before 1st node
180
+ {
181
+ t = (struct Node *)malloc (sizeof (struct Node )); // creating node memory in heap
182
+ t->data = x; // data insertion on newly created node
183
+ t->next = first; // newly created first next (pointer) points to the old first (for linking purpose)
184
+ first = t; // Bring 1st on new node
185
+ }
186
+ else if (pos > 0 )
187
+ {
188
+ p = first;
189
+ for (int i = 0 ; i < pos - 1 && p; i++)
190
+ p = p->next ;
191
+ if (p)
192
+ {
193
+ t = (struct Node *)malloc (sizeof (struct Node ));
194
+ t->data = x;
195
+ t->next = p->next ;
196
+ p->next = t;
197
+ }
198
+ }
199
+ }
200
+
201
+ // Insert at last
202
+ void Insertlast (int y)
203
+ {
204
+ struct Node * tt;
205
+ struct Node * last;
206
+ tt = (struct Node * )malloc (sizeof (struct Node ));
207
+ last= (struct Node *)malloc (sizeof (struct Node ));
208
+ tt->data = y; // assigning data to node
209
+ tt->next = NULL ; // assigning node next with NUll
210
+ if (first == NULL ) // if the ll is alredy empty
211
+ {
212
+ first = last = tt;
213
+ }
214
+ else
215
+ {
216
+ last->next = tt;
217
+ last = tt;
218
+ }
219
+
220
+
221
+ }
222
+
223
+ void Sortedinsert (struct Node * p, int x)
224
+ {
225
+ struct Node * t, * q = NULL ;
226
+ t = (struct Node *)malloc (sizeof (struct Node ));
227
+ t->data = x;/* first node is ready*/
228
+ t->next = NULL ;
229
+ if (first == NULL )
230
+ {
231
+ first = t;
232
+ }
233
+ else
234
+ {
235
+ while (p && p->data < x)
236
+ {
237
+ q = p;
238
+ p = p->next ;
239
+ }
240
+ if (p == first)
241
+ {
242
+ t->next = first;
243
+ first = t;
244
+ }
245
+ else {
246
+ t->next = q->next ;
247
+ q->next = t;
248
+ }
249
+ }
250
+ }
251
+
252
+
253
+
254
+ int main ()
255
+ {
256
+ int a[] = { 2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,11 ,22 ,33 ,44 ,55 ,66 ,77 ,2 ,55 ,999 };
257
+ int b[] = { 10 ,20 ,30 ,40 ,50 };
258
+ Create (b, sizeof (b) / sizeof (int ));
259
+ Create (a, sizeof (a) / sizeof (int ));
260
+ struct Node * temp;
261
+ Display (first);
262
+ printf (" \n Display using Recursion\n " );
263
+ RDisplay (first);
264
+ printf (" \n The nodes in linkedlist is %d" ,count (first));
265
+ printf (" \n The nodes in linkedlist using Recursion is %d" , Rcount (first)); // size and space is o(n)
266
+ printf (" \n Sum of the linkedlist is = %d" , sum (first));
267
+ printf (" \n Sum of LL using Recursion is = %d" , Rsum (first));
268
+ printf (" \n Max in the linked list is %d" , max (first));
269
+ printf (" \n max in ll using another method %d" , max1 (first));
270
+ printf (" \n max using recursion %d" , Rmax (first));
271
+ temp = LSearch (first, 888 );
272
+ if (temp)
273
+ printf (" \n Data is found in ll using Linear search i.e : %d" , temp->data );
274
+ else
275
+ printf (" \n Data is not Found " );
276
+ temp = RSearch (first, 5 );
277
+ if (temp)
278
+ printf (" \n Data is found in ll using Recursive method i.e : %d" , temp->data );
279
+ else
280
+ printf (" \n Data is not Found" );
281
+ temp = Movetotop (first, 8 ); // this is for checking either that element is presnt or not
282
+ if (temp)
283
+ printf (" \n data is found in ll i.e % d and key element is moved to first position\n " , temp->data );
284
+ else
285
+ printf (" Element is not present\n " );
286
+ Display (first); // it will display after shifting to 1st position
287
+ printf (" \n " );
288
+ Insert (0 , 666 );// this is for inserting element in the ll( pos , data ) // o(n)
289
+ Insert (1 , 3 );
290
+ Insert (2 , 4 );
291
+ Insert (3 , 4 );
292
+ Display (first); // Displaying after insertion
293
+ // imp //Display(first); // We can Create a new linked list with insertion method after removing array and create line in main code
294
+ printf (" \n " );
295
+ Insertlast (28 ); // inserting at last
296
+ Display (first);
297
+ printf (" \n " );
298
+ Sortedinsert (first, 35 ); // this is for inserting in a sorted linked list i.e in create array b [] in main
299
+ Display (first);
300
+
301
+ return 0 ;
302
+
303
+ }
0 commit comments