Having trouble deleting a node from a linked list
Posted
by
Requiem
on Stack Overflow
See other posts from Stack Overflow
or by Requiem
Published on 2012-10-23T04:03:59Z
Indexed on
2012/10/23
5:04 UTC
Read the original article
Hit count: 86
c
|linked-list
I've been working on this code for my shell that I'm creating and for some reason it isn't working. I'm implementing a watchuser function that watch's a user when an argument is given (args[1]). However, when a second argument (args[2]) of "off" is given, the user should be deleted from the linked list and should no longer be watched.
struct userList * goList;
goList = userInventory;
do{
if (strcmp(userInventory->username, args[1]) == 0){
printf("%s\n", args[1]);
printf("%s\n",userInventory->username);
struct userList * temp2;
temp2 = userInventory->next;
if (userInventory->next != NULL){
userInventory->next = temp2->next;
userInventory->next->prev = userInventory;
}
free(temp2);
}
goList = goList->next;
}while (goList != userInventory);
My global struct is also as follows:
struct userList{
char * username;
struct userList * prev;
struct userList * next;
}
For reason, this code won't delete the user node from my linked list. The adding works, but this remove function won't and I'm not sure why. The print statements are there just to make sure it's executing the condition, which it is.
If anyone could help me find the reasoning behind my error, I'd greatly appreciate it. Till then, I'll be trying to debug this.
Thanks.
© Stack Overflow or respective owner