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.
If I understand anything about your code,
Problem 1 (plausible):
Is this a circular list? If it’s not, the condition in
while ()isn’t going to become true.Problem 2:
Here you keep comparing the string in the head (or tail) of the list instead of comparing the string in the current node,
goList. Finding a match can only succeed in the above code, if the match is in the very first node (head/tail) to whichuserInventorypoints initially.Problem 3:
Let’s assume
userInventoryis already corrected to begoList:First of all, it’s going to
free()not the matching node, but the one after it (or maybe evenNULL), which is wrong.Secondly, this piece of code isn’t doing proper unlinking and relinking of nodes. What it should be (assuming the list isn’t circular):
Problem 4:
If the deletion succeeds, this line is going to access the just freed node and likely crash your program:
So, you need to change the code to something like:
Problem 5:
If you delete the list head (or is it tail?) node, you need to update
userInventoryto point to the next node after it. If you don’t, you will lose all access to the list becauseuserInventorywill point to freed memory and not to the remaining nodes, if any.Problem 6 (plausible):
The above line does not
free()the memory behindtemp2->username. You want tofree()it if it wasmalloc()ed.You should really approach problems one step at a time (e.g. first, iterating over a list, then unlinking/relinking nodes, then deleting nodes).
When things aren’t clear or aren’t working, use paper and a pencil (or a drawing board and a pen or a piece of chalk) to visualize the problem for yourself. Draw the objects, the arrows depicting pointers or some other connections between them, etc etc, scribble variable names next to the objects so you can clearly see how to progress from the diagram to code.