请教关于链表的问题
请朋友们来帮我看看,这短代码在哪里出错了!
谢谢!
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
#include <stdlib.h>
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf( "enter num and score\n ");
scanf( "%ld %f ",&p1-> num,&p2-> score);
head=NULL;
while(p1-> num!=0)
{
n=n+1;
if(n==1)
head=p1;
p1=(struct student *)malloc(LEN);
scanf( "%ld %f ",&p1-> num,&p1-> score);
}
p2-> next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
do{
printf( "the number is %ld ,score is %f\n ",p-> num,p-> score);
p=p-> next;
}while(p-> next!=NULL);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf( "list null\n ");
goto end;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL)
{
p2=p1;
p1=p1-> next;
}
if(num==p1-> num)
{
if(p1==head)
head=p1-> next;
else
p2-> next=p1-> next;
printf( "delete:%ld\n ",num);
n=n-1;
}
else
printf( "%ld not been foung!\n ",num);
return(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0-> next=NULL;
}
else
{
while((p0-> num> p1-> num)&&(p1-> next!=NULL))
{
p2=p1;
p1=p1-> next;
if(p0-> num <=p1-> num)
{
if(head==p1)
head=p0;
else
p2-> next=p0;
p0-> next=p1;
}
else
{
p1-> next=p0;
p0-> next=NULL;
}
}
n=n+1;
return(head);
}
}
main()
{
struct student *head,stu;
long del_num;
printf( "input records\n ");
head=creat();
print(head);
printf( "\ninput the deleted number\n ");
scanf( "%ld ",&del_num);
head=del(head,del_num);
print(head);
pirntf( "input the inserted record: ");
scanf( "%ld %f ",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
system( "pause ");
return(0);
}
我电脑上的编译器老是提示有问题,但是我找不出来啊!
[解决办法]
居然用了goto啊,不建议使用
[解决办法]
#include <malloc.h>
#define NULL 0//这句删掉 ,重复定义
#include <stdio.h> //加上这句
#define LEN sizeof(struct student)
#include <stdlib.h>
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf( "enter num and score\n ");
scanf( "%ld %f ",&p1-> num,&p2-> score);
head=NULL;
while(p1-> num!=0)
{
n=n+1;
if(n==1)
head=p1;
p1=(struct student *)malloc(LEN);
scanf( "%ld %f ",&p1-> num,&p1-> score);
}
p2-> next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
do{
printf( "the number is %ld ,score is %f\n ",p-> num,p-> score);
p=p-> next;
}while(p-> next!=NULL);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf( "list null\n ");
goto end;//这句改成 return NULL;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL)
{
p2=p1;
p1=p1-> next;
}
if(num==p1-> num)
{
if(p1==head)
head=p1-> next;
else
p2-> next=p1-> next;
printf( "delete:%ld\n ",num);
n=n-1;
}
else
printf( "%ld not been foung!\n ",num);
return(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0-> next=NULL;
}
else
{
while((p0-> num> p1-> num)&&(p1-> next!=NULL))
{
p2=p1;
p1=p1-> next;
if(p0-> num <=p1-> num)
{
if(head==p1)
head=p0;
else
p2-> next=p0;
p0-> next=p1;
}
else
{
p1-> next=p0;
p0-> next=NULL;
}
}
n=n+1;
return(head);
}
}
main()
{
struct student *head,stu;
long del_num;
printf( "input records\n ");
head=creat();
print(head);
printf( "\ninput the deleted number\n ");
scanf( "%ld ",&del_num);
head=del(head,del_num);
print(head);
pirntf( "input the inserted record: ");//拼写错误 printf
scanf( "%ld %f ",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
system( "pause ");
return(0);
}
暂时只找到这些
[解决办法]
一开始最好照着书本写,不要养成一些坏习惯,比如用goto等.
[解决办法]
该改的地方都改了,以后多注意。
#include <malloc.h>
#include <stdio.h>
// 缺少头文件 #include <stdio.h>
#define NULL 0
#define LEN sizeof(struct student)
#include <stdlib.h>
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf( "enter num and score\n ");
scanf( "%ld %f ",&p1-> num,&p2-> score);
head=NULL;
while(p1-> num!=0)
{
n=n+1;
if(n==1)
head=p1;
// 很明显你这还有几句很关键的语句忘写了
else
{
p2 = p1;
}
p1=(struct student *)malloc(LEN);
scanf( "%ld %f ",&p1-> num,&p1-> score);
p2 -> next = p1;
}
p2-> next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
do{
printf( "the number is %ld ,score is %f\n ",p-> num,p-> score);
p=p-> next;
}//while(p-> next!=NULL);
while (p != NULL);// 这个地方错得很关键,导致最后一组数无法输出
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf( "list null\n ");
//goto end;
return head; //这样写使程序的流程更清晰
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL)
{
p2=p1;
p1=p1-> next;
}
if(num==p1-> num)
{
if(p1==head)
head=p1-> next;
else
p2-> next=p1-> next;
printf( "delete:%ld\n ",num);
n=n-1;
}
else
printf( "%ld not been foung!\n ",num);
return(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0-> next=NULL;
}
else
{
while((p0-> num> p1-> num)&&(p1-> next!=NULL))
{
p2=p1;
p1=p1-> next;
if(p0-> num <=p1-> num)
{
if(head==p1)
head=p0;
else
p2-> next=p0;
p0-> next=p1;
}
else
{
p1-> next=p0;
p0-> next=NULL;
}
}
n=n+1;
return(head);
}
}
main()
{
//struct student *head,stu;
struct student *head = NULL, stu; // 定义head的同时初始化它
long del_num;
printf( "input records\n ");
head=creat();
if (head == NULL)
{
puts ( "头指针空 ");
}
print(head);
printf( "\ninput the deleted number\n ");
scanf( "%ld ",&del_num);
head=del(head,del_num);
print(head);
//pirntf( "input the inserted record: ");
printf( "input the inserted record: ");// printf 写错了
scanf( "%ld %f ",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
system( "pause ");
return(0);
}
[解决办法]
上面那个在 insert 函数那一个块还有一些问题。
再次修改后的:
#include <malloc.h>
#include <stdio.h>
// 缺少头文件 #include <stdio.h>
#define NULL 0
#define LEN sizeof(struct student)
#include <stdlib.h>
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf( "enter num and score\n ");
scanf( "%ld %f ",&p1-> num,&p2-> score);
head=NULL;
while(p1-> num!=0)
{
n=n+1;
if(n==1)
head=p1;
// 很明显你这还有几句很关键的语句忘写了
else
{
p2 = p1;
}
p1=(struct student *)malloc(LEN);
scanf( "%ld %f ",&p1-> num,&p1-> score);
p2 -> next = p1;
}
p2-> next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
do{
printf( "the number is %ld ,score is %f\n ",p-> num,p-> score);
p=p-> next;
}//while(p-> next!=NULL);
while (p != NULL);// 这个地方错得很关键,导致最后一组数无法输出
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf( "list null\n ");
//goto end;
return head; //这样写使程序的流程更清晰
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL)
{
p2=p1;
p1=p1-> next;
}
if(num==p1-> num)
{
if(p1==head)
head=p1-> next;
else
p2-> next=p1-> next;
printf( "delete:%ld\n ",num);
n=n-1;
}
else
printf( "%ld not been foung!\n ",num);
return(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
//head=p0;
//p0-> next=NULL;
return head; // 这句比上面两句好多了
}
while((p0-> num > p1-> num)&&(p1-> next != NULL))
{
p2=p1;
p1=p1-> next;
}// 你的while语句到这就应该结束了
if(p0-> num <= p1-> num)
{
if(head==p1)
head=p0;
else
p2-> next=p0;
p0-> next=p1;
}
else
{
p1-> next=p0;
p0-> next=NULL;
}
n=n+1;
return(head);
}
main()
{
//struct student *head,stu;
struct student *head = NULL, stu; // 定义head的同时初始化它
long del_num;
printf( "input records\n ");
head=creat();
print(head);
printf( "\ninput the deleted number\n ");
scanf( "%ld ",&del_num);
head=del(head,del_num);
print(head);
//pirntf( "input the inserted record: ");
printf( "input the inserted record: ");// printf 写错了
scanf( "%ld %f ",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
system( "pause ");
return(0);
}