Adding a find element in a simple linked list

So i have the code, the way i want it but i need to add a find to it.
The code is not mine, i found an example online, and i need to add find.

It should be added as an option 10, before the exit option but it does not matter, i can swap 10 and 9.


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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <stdio.h>  
#include <stdlib.h>  
  
//Structure containing a Data part & a  
//Link part to the next node in the List  
  
struct Node  
{  
 int Data;  
 struct Node *Next;  
}*Head;  
  
// Counting number of elements in the List  
  
int length()  
{  
  struct Node *cur_ptr;  
  int count=0;  
  
  cur_ptr=Head;  
  
  while(cur_ptr != NULL)  
  {  
     cur_ptr=cur_ptr->Next;  
     count++;  
  }  
  return(count);  
}  
  
// Deleting a node from List depending upon the data in the node.  
  
int delNodeData(int num)  
{  
  struct Node *prev_ptr, *cur_ptr;  
  
  cur_ptr=Head;  
  
  while(cur_ptr != NULL)  
  {  
     if(cur_ptr->Data == num)  
     {  
        if(cur_ptr==Head)  
        {  
           Head=cur_ptr->Next;  
           free(cur_ptr);  
           return 0;  
        }  
        else  
        {  
           prev_ptr->Next=cur_ptr->Next;  
           free(cur_ptr);  
           return 0;  
        }  
     }  
     else  
     {  
        prev_ptr=cur_ptr;  
        cur_ptr=cur_ptr->Next;  
     }  
  }  
  
  printf("\nElement %d is not found in the List", num);  
  return 1;  
}  
  
// Deleting a node from List depending upon the location in the list.  
  
int delNodeLoc(int loc)  
{  
  struct Node *prev_ptr, *cur_ptr;  
  int i;  
  
  cur_ptr=Head;  
  
  if(loc > (length()) || loc <= 0)  
  {  
      printf("\nDeletion of Node at given location is not possible\n ");  
  }  
  else  
  {  
      // If the location is starting of the list  
      if (loc == 1)  
      {  
          Head=cur_ptr->Next;  
          free(cur_ptr);  
          return 0;  
      }  
      else  
      {  
          for(i=1;i<loc;i++)  
          {  
              prev_ptr=cur_ptr;  
              cur_ptr=cur_ptr->Next;  
          }  
  
          prev_ptr->Next=cur_ptr->Next;  
          free(cur_ptr);  
      }  
  }  
  return 1;  
}  
  
//Adding a Node at the end of the list  
  
void addEnd(int num)  
{  
  struct Node *temp1, *temp2;  
  
  temp1=(struct Node *)malloc(sizeof(struct Node));  
  temp1->Data=num;  
  
  // Copying the Head location into another node.  
  temp2=Head;  
  
  if(Head == NULL)  
  {  
     // If List is empty we create First Node.  
     Head=temp1;  
     Head->Next=NULL;  
  }  
  else  
  {  
     // Traverse down to end of the list.  
     while(temp2->Next != NULL)  
     temp2=temp2->Next;  
  
     // Append at the end of the list.  
     temp1->Next=NULL;  
     temp2->Next=temp1;  
  }  
}  
  
// Adding a Node at the Beginning of the List  
  
void addBeg(int num)  
{  
  struct Node *temp;  
  
  temp=(struct Node *)malloc(sizeof(struct Node));  
  temp->Data = num;  
  
  if (Head == NULL)  
  {  
     //List is Empty  
     Head=temp;  
     Head->Next=NULL;  
  }  
  else  
  {  
     temp->Next=Head;  
     Head=temp;  
  }  
}  
  
// Adding a new Node at specified position  
  
void addAt(int num, int loc)  
{  
  int i;  
  struct Node *temp, *prev_ptr, *cur_ptr;  
  
  cur_ptr=Head;  
  
  if(loc > (length()+1) || loc <= 0)  
  {  
     printf("\nInsertion at given location is not possible\n ");  
  }  
  else  
  {  
      // If the location is starting of the list  
      if (loc == 1)  
      {  
          addBeg(num);  
      }  
      else  
      {  
          for(i=1;i<loc;i++)  
          {  
              prev_ptr=cur_ptr;  
              cur_ptr=cur_ptr->Next;  
          }  
  
          temp=(struct Node *)malloc(sizeof(struct Node));  
          temp->Data=num;  
  
          prev_ptr->Next=temp;  
          temp->Next=cur_ptr;  
      }  
  }  
}  
  
// Displaying list contents  
  
void display()  
{  
  struct Node *cur_ptr;  
  
  cur_ptr=Head;  
  
  if(cur_ptr==NULL)  
  {  
     printf("\nList is Empty");  
  }  
  else  
  {  
      printf("\nElements in the List: ");  
      //traverse the entire linked list  
      while(cur_ptr!=NULL)  
      {  
          printf(" -> %d ",cur_ptr->Data);  
          cur_ptr=cur_ptr->Next;  
      }  
      printf("\n");  
  }  
}  
  
//Reversesing a Linked List  
  
void reverse()  
{  
  struct Node *prev_ptr, *cur_ptr, *temp;  
  
  cur_ptr=Head;  
  prev_ptr=NULL;  
  
  while(cur_ptr != NULL)  
  {  
     temp=prev_ptr;  
     prev_ptr=cur_ptr;  
  
     cur_ptr=cur_ptr->Next;  
     prev_ptr->Next=temp;  
  }  
  
  Head=prev_ptr;  
}

  
/////////////////////////////
//Find an element in the list
/////////////////////////////

  
int main(int argc, char *argv[])  
{  
 int i=0;  
  
 //Set HEAD as NULL  
 Head=NULL;  
  
 while(1)  
 {  
    printf("\n####################################################\n");  
    printf("MAIN MENU\n");  
    printf("####################################################\n");  
    printf(" \nInsert a number \n1. At the Beginning");  
    printf(" \n2. At the End");  
    printf(" \n3. At a Particular Location in the List");  
    printf(" \n\n4. Print the Elements in the List");  
    printf(" \n5. Print number of elements in the List");  
    printf(" \n6. Reverse the linked List");  
    printf(" \n\nDelete a Node in the List");  
    printf(" \n7. Delete a node based on Value");  
    printf(" \n8. Delete a node based on Location\n");  
    printf(" \n\n9. Exit\n"); 
    printf(" \n\n10. Find an element in the list\n");
    printf(" \nChoose Option: ");  
    scanf("%d",&i);  
  
    switch(i)  
    {  
      case 1:  
      {  
          int num;  
          printf(" \nEnter a Number to insert in the List: ");  
          scanf("%d",&num);  
          addBeg(num);  
          display();  
          break;  
      }  
  
      case 2:  
      {  
          int num;  
          printf(" \nEnter the Number to insert: ");  
          scanf("%d",&num);  
          addEnd(num);  
          display();  
          break;  
      }  
  
      case 3:  
      {  
          int num, loc;  
          printf("\nEnter the Number to insert: ");  
          scanf("%d",&num);  
          printf("\nEnter the location Number in List at which \  
          the Number is inserted: ");  
          scanf("%d",&loc);  
          addAt(num,loc);  
          display();  
          break;  
      }  
  
      case 4:  
      {  
          display();  
          break;  
      }  
  
      case 5:  
      {  
          display();  
          printf(" \nTotal number of nodes in the List: %d",length());  
          break;  
      }  
  
      case 6:  
      {  
          reverse();  
          display();  
          break;  
      }  
  
      case 7:  
      {  
          int num;  
          printf(" \nEnter the number to be deleted from List: ");  
          scanf("%d",&num);  
          delNodeData(num);  
          display();  
          break;  
      }  
  
      case 8:  
      {  
          int num;  
          printf(" \nEnter the location of the node to \  
          be deleted from List: ");  
          scanf("%d",&num);  
          delNodeLoc(num);  
          display();  
          break;  
      }  
  
      case 9:  
      {  
          struct Node *temp;  
  
          while(Head!=NULL)  
          {  
              temp = Head->Next;  
              free(Head);  
              Head=temp;  
          }  
          exit(0);  
      }  
	  
	  case 10:
	  
	  	  
	            //Find an element in the list.
	  
  
      default:  
      {  
          printf("\nWrong Option choosen");  
      }  
    }/* end if switch */  
 }/* end of while */  
}/* end of main */
This is the format i think i should be having for find.
1
2
3
4
5
6
7
8
9
10
11
12
13
 
int find(Node *cur_ptr, int key)
   while(cur_ptr != NULL)
    {
        if(cur_ptr->Data == key)
        {
                return 1;
        }
        
        {
            cur_ptr = cur_ptr->next;
        }
    }


And for the case 10:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	  case 10:  
      {
           int Data;
           scanf("%d",&Data);
           int status = find(Head,Data);
           if(status)
           {
                   printf("Element Found\n");
           }
            else
            {
                    printf("Element Not Found\n");
            }
      } 


But if i select find from the menu nothing happens. There must be something i am not seeing.
Last edited on
So i added find, but there`s something wrong, the code compiled but the option for finding a number does not work.

Last edited on
I needed a break :) @ case 10


Now it works just fine.
Topic archived. No new replies allowed.