我有一個雙鏈表的文件,其中包含一組進程標識符和一些狀態信息。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;
}
}
}
什麼是錯我的刪除功能?當我嘗試打印我的列表時,我似乎陷入了無限循環。以前我認爲這是我使用免費()的方式,但顯然不是從回覆:)
謝謝!
通常你通過使用malloc分配您在列表中插入一切克服它。 – 2012-04-08 03:27:27
**是如何分配的? – 2012-04-08 03:27:59