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

  • Home
  • SEARCH
  • 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 6750723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:50:32+00:00 2026-05-26T12:50:32+00:00

I have a program in c which receives messages from different clients and servers.

  • 0

I have a program in c which receives messages from different clients and servers. When the messages come in it adds the message to that list. After the message is added to the list I print it on the screen and on the other servers. But i want to delete the node that contains the message after it is printed so when the the print function gets called only prints the new messages. How can I delete the node after print?

Here is my struct:

typedef struct trade_list {
    char* trader_msg;
    u_int32_t id_of_sender;
    int sender_timer;
    int local_time;

    struct trade_list *next;
}trade_list;

trade_list *head = NULL;

And here is how I print:

  void print_trades()
    {

        trade_list * newnode = head;
        trade_list *previous = NULL;

            while (newnode) {

             previous = newnode;

              if ((elapsed - newnode->local_time >= 8)) 

             printf ("%s\n", newnode->trader_msg);
             newnode = newnode->next;

                if (previous == NULL)
                    head = newnode->next;
                else
                    {
                    previous->next = newnode->next;

                    free(newnode);
                    }
                }
}

Thus gives me a segmentation fault. I tried changing the newnode->next to just newnode in the else part. previous->next = new node; It didn’t give me an error but it did not erase the node as it kept printing that node every time the print function was called

  • 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-05-26T12:50:33+00:00Added an answer on May 26, 2026 at 12:50 pm

    At the start of your function:

    trade_list *prev = NULL;
    

    At each iteration in your loop, before newnode = newnode->next; add prev = newnode.

    Then to delete:

    if (prev == NULL) /* replace the head */
       head = newnode->next;
    else
       prev->next = newnode->next;
    
    /* free newnode? */
    

    Fairly simple.

    EDIT: You should really have some functions like list_create, list_add, list_remove associated with your data structure, rather than just chucking like the remove code into a print function. That’s the first thing I do when I create any sort of data structure.

    Another option is to have your linked list like:

    typedef struct trade_node {
       char* trader_msg;
       u_int32_t id_of_sender;
       int sender_timer;
       int local_time;
    
       struct trade_node *next;
    } trade_node;
    
    typedef struct trade_list {
       trade_node *head;
       /* trade_node *foot; ? */
       size_t length;
    } trade_list;
    

    EDIT2: As for your edit, change print_trades to something like:

    void print_trades()
    {
       trade_list *prev = NULL, *next;
       trade_list *newnode = head;
    
       while (newnode) {
    
          if ((elapsed - newnode->local_time >= 8)) {    
             printf ("%s\n", newnode->trader_msg);
    
             /* temp variable for newnode->next */
             next = newnode->next;
    
             if (prev == NULL) /* replace the head */
                head = next;
             else
                prev->next = next;
    
             /* free newnode->trader_msg? */
             free(newnode);
    
             /* don't update prev */
             newnode = next;
          }
          else {
             prev = newnode;
             newnode = newnode->next;
          }
    
       }
    }
    

    In your code, previous will never be NULL since you set it to newnode at the start of the loop, and newnode also shouldn’t equal newnode->next until after newnode has been processed completely. You’re also using newnode after you’ve freed it.

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

Sidebar

Related Questions

I have java program which receives encrypted message through TCP/IP from one component of
I have to code a Java program that will receive messages from network and
Am using java to develop program that receives and processes SMS messages. I have
I have a Qt program which displays the data it receives over UDP. It
I have program which reads MSMQ using GetAllMessages but it does not remove messages
I've worked on a program that uses databases to send small messages from one
I have a Java program that continually listens for messages of a certain format
I have a complicated algorithm which receives data from a socket connections, transformate the
I have a program which needs to behave slightly differently on Tiger than on
I have a program which deliberately performs a divide by zero (and stores the

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.