C语言链表的小问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 13:37:48
#include "stdio.h"
#define len sizeof(struct stu)
struct stu{
int num;
struct stu *next;};
struct stu *head;

head是链表的头指针,假设已经生成链表,head指向表头

void insert(struct stu *head,int a,int b) /*插入NUM 将值为B的接点插到值为A接点的后面*/
{
struct stu *p3,*q;
p3=head;
q=(struct stu *)malloc(len);
q->num=888;
q->next=NULL;
/*找a,插入b*/
do
{
p3=p3->next;
if(p3->num==a)
{
q->next=p3->next;
p3->next=q;
}
}while(p3->next!=NULL&&p3->num!=a);
}

上面那个插入的代码有问题
可我不知道哪里出问题
请教各位!

”插入NUM 将值为B的接点插到值为A接点的后面”你说的函数功能有问题...可能你没把完整代码贴上来吧,所以我只能修改你这个我不知道用来干吗的函数....
试试:

void insert(struct stu *head,int a,int b)
{
struct stu *p3,*q;
p3=head;
q=(struct stu *)malloc(len);
q->num=888;
/*找a,插入b*/
do
{
p3=p3->next;
if(p3->num==a)
{
q->next=p3->next;
p3->next=q;
}
}while(p3->next!=NULL);
}

至少是要加这个#include <stdlib.h>
或#include <malloc.h>
#define NULL 0
还有在insert()中b根本就是没有用到

不好意思我这里没有TC,不好调试了
给点鼓励吧!

循环的条件吧?改为||试试