双向链表的删除操作,输入要删除的信息后无法运行出结果,求问什么地方错了。
create函数等已经验证是正确的,只有Delete函数中有错误。错误部分代码如下:voidDelete(stu*pHead,char*x){stu*p;char*y;p...
create函数等已经验证是正确的,只有Delete函数中有错误。
错误部分代码如下:
void Delete (stu *pHead, char *x)
{
stu *p;
char *y;
p = pHead->rlink;
while(p->rlink != pHead)
{
y = p->name;
if ( strcmp(y,x) == 0 )
{
(p->llink)->rlink = p->rlink;
(p->rlink)->llink = p->llink;
free(p);
}
else
p = p->rlink;
}
}
int main (void)
{
int n;
char stuname[20];
stu *pHead,*searchpoint;
printf("input the number of the students:");
scanf ("%d",&n);
pHead = create (n);
print (pHead);
printf ("input the name you want to delete:");
scanf("%s",stuname);
Delete (pHead, stuname);
print (pHead);
return 0;
} 展开
错误部分代码如下:
void Delete (stu *pHead, char *x)
{
stu *p;
char *y;
p = pHead->rlink;
while(p->rlink != pHead)
{
y = p->name;
if ( strcmp(y,x) == 0 )
{
(p->llink)->rlink = p->rlink;
(p->rlink)->llink = p->llink;
free(p);
}
else
p = p->rlink;
}
}
int main (void)
{
int n;
char stuname[20];
stu *pHead,*searchpoint;
printf("input the number of the students:");
scanf ("%d",&n);
pHead = create (n);
print (pHead);
printf ("input the name you want to delete:");
scanf("%s",stuname);
Delete (pHead, stuname);
print (pHead);
return 0;
} 展开
1个回答
展开全部
//定义插入:s->prior=p->prior;p->prior->next=s;s->next=p;p->prior=s;//第一句必须在第四句之前完成,否则*p的前驱结点就丢掉了。删除:p->prior->next=p->next;p->next->prior=p->prior;free(p);//绕过p
追问
(p->llink)->rlink = p->rlink;
(p->rlink)->llink = p->llink;
free(p);
就是这个,但是运行时不行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询