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 7772135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:49:08+00:00 2026-06-01T16:49:08+00:00

I have a file with a double-linked-list that contains a set of process identifiers

  • 0

I have a file with a double-linked-list that contains a set of process identifiers and some state information.

struct pr7_process 
{ 
  pid_t pid;        /* process ID, supplied from fork() */ 
                /* if 0, this entry is currently not in use */ 
  int   state;      /* process state, your own definition */ 
  int   exit_status;    /* supplied from wait() if process has finished */
  struct pr7_process *next;   // a pointer to the next process
  struct pr7_process *prev;
};

/* the process list */

struct process_list
{
   struct pr7_process *head;
   struct pr7_process *tail;
};

I have a method to remove an element of my list:

{
struct pr7_process *cur;
  for(cur = list->head; cur != NULL; cur = cur->next)
    {
      if (cur->pid == pid)
        {
          printf("cur pid: %d\n", cur->pid);
          cur->state = STATE_NONE;
          if(list->head == list->tail)
         {
           free(cur);
         }
         else
          {
            cur->prev->next = cur->next;
            cur->next->prev = cur->prev;
            free(cur);
          }
          break;
        }
     } 
  } 

What is wrong with my remove function? I seem to get an infinite loop when I try to print my list. Previously I thought it was the way I used free() but apparently not from the replies 🙂

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-01T16:49:09+00:00Added an answer on June 1, 2026 at 4:49 pm

    When you add a node set next to NULL.

    Then when you free all, free until next == NULL.

    When you remove a node. Update links and free node.

    Also; free on NULL is an noop.

    Valgrind is a invaluable tool when working on such things.


    Believe you have to do some more checks; I.e.:

    struct pr7_process {
        int pid;
        ...
    } const new_proc = {
        0, 44, 0, NULL, NULL
    };
    
    void del(struct process_list *list, int pid)
    {
        struct pr7_process *cur;
    
        for (cur = list->head; cur != NULL; cur = cur->next) {
            if (cur->pid == pid) {
    
                printf("cur pid: %d\n", cur->pid);
    
                if(list->head == list->tail) {
                    free(cur);
                    list->head = NULL;
                    list->tail = NULL;
                } else if (cur == list->head) {
                    list->head = list->head->next;
                    free(cur);
                    list->head->prev = NULL;
                } else if (cur == list->tail) {
                    list->tail = cur->prev;
                    free(cur);
                    list->tail->next = NULL;
                } else {
                    cur->prev->next = cur->next;
                    cur->next->prev = cur->prev;
                    free(cur);
                }
                break;
            }
        }
    }
    

    Given that you build the list something like i.e.:

    int push(struct process_list *list, int pid, int state)
    {
        if (list->head == NULL) { /* or move this to where ever you see fit */
            if ((list->head  = malloc(sizeof(struct pr7_process))) == NULL)
                return -1;
            list->tail  = list->head;
            *list->tail = new_proc;
        } else {
            if ((list->tail->next  = malloc(sizeof(struct pr7_process))) == NULL)
                return -1;
            *list->tail->next = new_proc;
            list->tail->next->prev = list->tail;
            list->tail = list->tail->next;
        }
        list->tail->pid = pid;
        list->tail->state = state;
    
        return 0;
    }
    
    void wipe(struct process_list *list)
    {
        struct pr7_process *node = list->tail;
    
        while (node != list->head) {
            node = list->tail->prev;
            free(list->tail);
            list->tail = node;
        }
        free(list->head);
        list->head = NULL;
        list->tail = NULL;
    }
    
    void prnt(struct process_list list, int dir)
    {
        if (dir == 1) {
            while (list.head != NULL) {
                printf("%4d: %d\n", list.head->pid, list.head->state);
                list.head = list.head->next;
            }
        } else {
            while (list.tail != NULL) {
                printf("%4d: %d\n", list.tail->pid, list.tail->state);
                list.tail = list.tail->prev;
            }
        }
    }
    
    int main(void)
    {
        struct process_list list = {NULL, NULL};
    
        push(&list, 331, 2); /* if(push() != -1) ... */
        push(&list, 332, 66);
        push(&list, 333, 47);
    
        prnt(list, 1);
    
        del(&list, 332);
        prnt(list, 1);
    
        wipe(&list);
        prnt(list, 1);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that have strings enclosed within double-quotes. I want to
I have a 2884765579 bytes file. This is double checked with this function, that
I have a file containing a [Double] serialized by Data.Binary that I'd like to
i have to do a generic double linked list, and i made it in
I have an exe file which runs OK when I double clik on it
I have a program with an associated file type. However, when I double-click a
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts
I have a file containing lines of the form, double mass, string seq, int
I've got a weird linker issue. I have code that looks like so: double
Lets say, I have the following text file: Listing 1: Endianess=little AddressModel=32 typedef struct{

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.