【发布时间】:2019-12-15 13:36:07
【问题描述】:
当我删除链表中存在的任何节点时,我的代码可以完美运行。 假设我的链表有 10 个节点,如果我想删除第 12、13、14... 个节点,我的程序会给我预期的消息。
但如果我想删除 第 11 个 节点(与最后一个节点相邻),我的程序将以退出代码 -1073741819 (0xC0000005) 终止
int delete()
{
int position, count = 1;
printf( "\nwrite your position" );
scanf( "%d", &position );
struct node *p, *q;
p = head;
if ( position == 0 ) {
p = p->next;
head = p;
return 0;
}
while ( p != NULL && count != position ) {
count++;
p = p->next;
}
count = 0;
if ( p == NULL ) {
printf( "link list is empty or link not found\n" );
return 0;
}
else {
q = p->next;
p->next = q->next;
}
}
【问题讨论】:
-
拿一张纸和一支铅笔,考虑一下列表只包含一个节点(头)并且位置设置为1的情况。
-
^^^^ ... 或者列表为空(
head == NULL),位置为0。
标签: c linked-list singly-linked-list