2012-04-08 108 views
2

我有一个双链表的文件,其中包含一组进程标识符和一些状态信息。C编程链接列表并删除

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

什么是错我的删除功能?当我尝试打印我的列表时,我似乎陷入了无限循环。以前我认为这是我使用免费()的方式,但显然不是从回复:)

谢谢!

+0

通常你通过使用malloc分配您在列表中插入一切克服它。 – 2012-04-08 03:27:27

+1

**是如何分配的? – 2012-04-08 03:27:59

回答

1

当您将节点集next添加到NULL

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

当您删除节点。更新链接和免费节点。

另外;免费的NULL是一个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; 
} 
0

我知道你不能释放()不是由malloc分配的东西,我该如何克服?

有什么可以克服的?要么是动态分配的东西,你需要free()它,或者它被分配了自动存储时间,而你没有。这里没有问题。

通常情况下,这样一个点亮你将malloc的一切,让你可以可靠地释放的东西。否则,你不知道如何分配它们,并可能遇到未定义的行为。