Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:41:43+00:00 2026-06-13T05:41:43+00:00

I’ve been working on this code for my shell that I’m creating and for

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:41:44+00:00Added an answer on June 13, 2026 at 5:41 am

    If I understand anything about your code,

    Problem 1 (plausible):

    goList = userInventory;
    do {
        ...
        goList = goList->next;
    } while (goList != userInventory);
    

    Is this a circular list? If it’s not, the condition in while () isn’t going to become true.

    Problem 2:

    goList = userInventory;
    do {
        if (strcmp(userInventory->username, args[1]) == 0) {
        ...
        }
        goList = goList->next;
    } while (goList != userInventory);
    

    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 which userInventory points initially.

    Problem 3:

       temp2 = userInventory->next;
       if (userInventory->next != NULL) {
          userInventory->next = temp2->next;
          userInventory->next->prev = userInventory;
       }
       free(temp2);
    

    Let’s assume userInventory is already corrected to be goList:

       temp2 = goList->next;
       if (goList->next != NULL) {
          goList->next = temp2->next;
          goList->next->prev = goList;
       }
       free(temp2);
    

    First of all, it’s going to free() not the matching node, but the one after it (or maybe even NULL), 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):

       temp2 = goList;
       if (goList->next != NULL) {
          goList->next->prev = goList->prev; // now goList->next node points to goList->prev node
       }
       if (goList->prev != NULL) {
          goList->prev->next = goList->next; // now goList->prev node points to goList->next node
       }
       free(temp2);
    

    Problem 4:

    do {
        if (strcmp(goList->username, args[1]) == 0) {
            temp2 = goList;
            if (goList->next != NULL) {
                goList->next->prev = goList->prev; // now goList->next node points to goList->prev node
            }
            if (goList->prev != NULL) {
                goList->prev->next = goList->next; // now goList->prev node points to goList->next node
            }
            free(temp2);
        }
        goList = goList->next;
    } while (...);
    

    If the deletion succeeds, this line is going to access the just freed node and likely crash your program:

        goList = goList->next;
    

    So, you need to change the code to something like:

    do {
        if (strcmp(goList->username, args[1]) == 0) {
            temp2 = goList;
            if (goList->next != NULL) {
                goList->next->prev = goList->prev; // now goList->next node points to goList->prev node
            }
            if (goList->prev != NULL) {
                goList->prev->next = goList->next; // now goList->prev node points to goList->next node
            }
            goList = goList->next;
            free(temp2);
        }
        else
        {
            goList = goList->next;
        }
    } while (...);
    

    Problem 5:

    goList = userInventory;
    

    If you delete the list head (or is it tail?) node, you need to update userInventory to point to the next node after it. If you don’t, you will lose all access to the list because userInventory will point to freed memory and not to the remaining nodes, if any.

    Problem 6 (plausible):

            free(temp2);
    

    The above line does not free() the memory behind temp2->username. You want to free() it if it was malloc()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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.