【问题标题】:C programming linked-list and removeC 编程链表和删除
【发布时间】:2012-04-21 01:18:57
【问题描述】:

我有一个带有双链表的文件,其中包含一组进程标识符和一些状态信息。

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;
};

我有一种方法可以删除列表中的一个元素:

{
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;
        }
     } 
  } 

我的删除功能有什么问题?当我尝试打印我的列表时,我似乎得到了一个无限循环。以前我认为这是我使用 free() 的方式,但显然不是来自回复:)

谢谢!

【问题讨论】:

  • 通常你通过使用 malloc 来分配你在列表中插入的所有内容来克服它。
  • 当时是如何分配的?

标签: c list linked-list malloc free


【解决方案1】:

我知道你不能 free() 不是由 malloc 分配的东西,我该如何克服这个问题?

有什么需要克服的?要么是动态分配的,你需要free() 它,要么是分配了自动存储持续时间,而你不需要。这里没有问题。

通常使用这样的照明,您将malloc 一切,以便您可以可靠地释放东西。否则您不知道它们是如何分配的,并且可能会遇到未定义的行为。

【讨论】:

    【解决方案2】:

    当您将节点集 next 添加到 NULL 时。

    然后当你释放所有,释放直到下一个 == NULL。

    当您删除一个节点时。更新链接和空闲节点。

    还有; NULL 上的 free 是一个 noop。

    在处理此类事情时,Valgrind 是一个非常宝贵的工具。


    相信你必须做更多的检查;即:

    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;
            }
        }
    }
    

    鉴于您构建了列表某事,例如:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 2018-10-14
      • 2019-05-10
      • 2021-07-25
      相关资源
      最近更新 更多