Search and Delete In Linked List

Hello,
I need some help with my search and delete functions if anyone could help me figure out why they are not working correctly that would be wonderful!



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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct Node
{
    char firstName [15];
    char lastName [15];
    char Number [15];
    char searchName [15];
    struct Node *next;
} ;

struct Node *Head = NULL;

int printInfoRecieveOption () ;
void clearScreen () ;
void insertEntry ();
void printAll ();
void findEntry (char name []) ;
void deleteEntry () ;


int main ()
{
    int restart, userOption, otherOption, check, findUser;
    char userName [15];
    
    do{
    userOption = printInfoRecieveOption ();
    if(userOption == 1)
       {
          insertEntry ();
          printf("Contact Added Successfuly!\n\n\n");
       }
    else if(userOption == 2)
       {
          printf("Lookup A Contact\n");
          printf("___________________\n");
          printf("Please Enter The Last Name Of The Contact You Are Trying To Find\n");
          scanf("%s", &userName);
          getchar () ;
          clearScreen () ;
          printf("Contact To Find: %s\n", userName);
          printf("Is This Correct?\n");
          printf("(1) Yes\n");
          printf("(2) No\n");
          scanf("%i", &otherOption);
          getchar () ;
          clearScreen () ;
          if(otherOption > 2 || otherOption < 1)
            check = 3;
          else if(otherOption == 1 || otherOption == 2)
            check = 1;
          while(check == 3)
               {
          printf("Contact To Find: %s\n", userName);
          printf("Is This Correct?\n");
          printf("(1) Yes\n");
          printf("(2) No\n");
          scanf("%i", &otherOption);
          getchar () ;
          clearScreen () ;
          if(otherOption > 2 || otherOption < 1)
            check = 3;
          else if(otherOption == 1 || otherOption == 2)
            check = 1;
              }
                      
           findEntry (userName) ;
       }
    else if(userOption == 3)
       {
          deleteEntry () ;
       }
    else if(userOption == 4)
       {
          printAll ();
       }
    }  while(userOption != 5);
    if(userOption == 5)
    {
      clearScreen () ;
      printf("Goodbye!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      system("pause");
      return 0;
    }
    
}


void deleteEntry ()
{
   struct Node *curr = Head;
   char name [15], decide;
   int Decide;
   printf("Last Name To Delete\n");
   scanf("%s", &name);
   getchar ();
   while(curr != NULL)
      {
         if(curr->lastName == name)
           {
              printf("First Name: %s\n", curr->firstName);
              printf("Last Name: %s\n", curr->lastName);
              printf("Phone Number: %s\n", curr->Number);
              printf("Is This The Person You Would Like To Delete ? <Y/N>\n");
              scanf("%c", &decide);
              getchar ();
              
              if(decide == 'y' || decide == 'Y')
              Decide = 1;
              
              if(Decide == 1)
                free(curr);
           }
         curr = curr->next;
      }
}


void findEntry (char name [])
{
    struct Node *current = Head;
    char Name [15];
    int find = 0;

	if (current == NULL)
	{
		printf("----------------------------------------\n");
        printf("There Are No Contacts In The Phonebook\n");
        printf("----------------------------------------\n");
	}
	else
	{
		while (current != NULL)
		{
            strncpy(Name,current->lastName , sizeof(current->lastName));
            if(name == Name)
            {
            find = 1;
            }
			current = current ->next;
		}
	}

	if(find == 1)
    printf("Contact Was Found\n");
	else if(find != 1)
	printf("Contact Was Not Found\n");
}







void printAll ()
{
     	struct Node *current = Head;

	if (current == NULL)
	{
		printf("----------------------------------------\n");
        printf("There Are No Contacts In The Phonebook\n");
        printf("----------------------------------------\n");
	}
	else
	{
		while (current != NULL)
		{
            printf("| First Name: %s\n", current->firstName);
			printf("| Last Name: %s\n", current->lastName);
			printf("| Phone Number: %s\n\n\n", current->Number);
			current = current ->next;
		}
	}

	printf("\n");
     
}






void insertEntry ()
{
     char first [15], last [15], number [15];
     int option1, option2, check;
     
     do{   
     printf("Add A Contact\n");
     printf("_________________\n");
     printf("Please Enter Contacts First Name : ");
           scanf("%s", &first);
           getchar ();
           printf("\n");
     printf("Please Enter Contacts Last Name : ");
           scanf("%s", &last);
           getchar ();
           printf("\n");
     printf("Please Enter Contacts Phone Number (XXX-XXX-XXXX) : ");
           scanf("%s", &number);
           getchar ();
           clearScreen () ;
     printf("The Following Entry Will Be Added :\n");
            printf("First Name: %s\n", first);
            printf("Last Name: %s\n", last);
            printf("Phone Number: %s\n", number);
            printf("________________________\n");
            printf("Add This To The Phonebook ? \n");
            printf("( 1 ) Yes\n");
            printf("( 2 ) No\n");
            scanf("%i", &option1);
            getchar () ;
     clearScreen () ;
     if(option1 == 1 || option1 == 2)
     check = 2;
     else if(option1 > 2 || option1 < 1)
     check = 3;
     while(check == 3)
     {
        printf("ERROR: Please Use Either 1 For Yes or 2 For No\n");
        printf("First Name: %s\n", first);
        printf("Last Name: %s\n", last);
        printf("Phone Number: %s\n", number);
        printf("________________________\n");
        printf("Add This To The Phonebook ? \n");
        printf("( 1 ) Yes\n");
        printf("( 2 ) No\n");
        scanf("%i", &option1);
        getchar () ;
        clearScreen ();
        if(option1< 1 || option1 > 3)
        check = 3;
        else if(option1 == 1 || option1 == 2)
        check = 1; 
     }
} while(option1 == 2);
//---------------------------------------------------------------------------
    
     	struct Node *temp;
	temp = (struct Node *) malloc (sizeof(struct Node));

	if (temp != NULL)
	{
		strncpy(temp->firstName, first, sizeof(temp->firstName));
		strncpy(temp->lastName, last, sizeof(temp->lastName));
		strncpy(temp->Number, number, sizeof(temp->Number));

		if (Head == NULL)
		{
			Head = temp;
			Head -> next = NULL;
		}
		else
		{
			temp->next = Head;
			Head = temp;
		}
	}
}






int printInfoRecieveOption ()
{
    int restart;
    printf("1. Add A Contact\n");
    printf("2. Lookup A Contact\n");
    printf("3. Delete A Contact\n");
    printf("4. Display The Entire Phonebook\n");
    printf("5. Exit The Program\n");
    printf("_____________________________________\n");
    printf("What Would You Like To Do ?\n");
    scanf("%i", &restart);
    getchar () ;
    clearScreen () ;
    while(restart > 5 || restart <1)
    {
       printf("PLEASE USE THE NUMBERS 1 - 5\n");
       printf("1. Add A Contact\n");
       printf("2. Lookup A Contact\n");
       printf("3. Delete A Contact\n");
       printf("4. Display The Entire Phonebook\n");
       printf("5. Exit The Program\n");
       printf("_____________________________________\n");
       printf("What Would You Like To Do ?\n");
       scanf("%i", &restart);
       getchar () ;
       clearScreen () ;
    }
    
    return restart;
}


void clearScreen ()
{
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");     
}
why they are not working correctly that would be wonderful!


"Not working" is not enough information. In what way are they not working?

What do you expect them to do?
What are they actually doing?
Well basically I cannot get the delete function to delete the node with the same string as one entered by the user. Like "last name of the person you want to delete" and it would find the node with that last name, and delete it. And the search function is like the delete function, except it would search a person by their last name instead.
in your delete function:

1
2
3
4
5
              if(Decide == 1)
                free(curr);  //<- freeing here
           }
         curr = curr->next;  // <- can't use curr->next here
      }


you can't use curr->next after you free curr. curr will no longer exist.

You'll also need to adjust the pointers in the nodes so that the deleted node is removed from the list.

Say you have 4 nodes: A->B->C->D. And let's say you want to delete the C node. Not only will you need to free the C node, but you will also need to change the B node so that it points to D as its next node.

You want this as an end result: A->B->D
But with what you're doing, you have A->B->X D (B points to 'X', a deleted node, and D is lost/leaked because nothing points to it any more -- the only node that did point to it, C, no longer exists)
Thank You Disch for that, I ended up having to use strcmp in order to successfuly compare the string values, I wasn't doing this before. Then, I used your advice and got it working flawlessly, thank you!
Topic archived. No new replies allowed.