|
30鱼币
直接上代码第一个是没有报错的 可以自行编译
第二个是我一开始犯的错误.......
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #define LEN sizeof(struct student) // student结构的大小
- struct student *creat(); //创建链表
- void print(struct student *head); //打印链表
- int n;//定义这个是第几个学生
- struct student{
- int num;
- float score;
- struct student *next;
- };//链表结构
- void main(){//首先定义一个可以让函数调用的变量,然后调用创建,之后打印出来
- struct student *p;//指向我们自己创建的头
-
- p=creat();
- print(p);
- printf("\n\n");
- system("pause");
- }
- struct student *creat(){
- struct student *head;//最后返回的是head
- struct student *p1,*p2;//这个是两个小精灵似的东西来回跑和定义
-
- p1 =(struct student *)malloc(LEN);//malloc是在内存创建len长度的空间
- printf("one num:");
- scanf("%d",&p1->num);
- printf("one score");
- scanf("%f",&p1->score);
- p2=p1;
- head=NULL;
- n=0;
- while (p1->num!=0){
- n++;
- if (n==1){
- head=p1;
- }
- else{
- p2->next=p1;
- }
- p2=p1;
- p1=(struct student *) malloc(LEN);
-
- printf("\nshu ru num:");
- scanf("%d",&p1->num);
- printf("shu ru score:");
- scanf("%f",&p1->score);
- }
- p2->next = NULL;
- return head;
- }
- void print(struct student *head){
- struct student *p;
- p=head;
- printf("%d个\n",n);
- if (NULL!=head){
- do{
- printf("%d号学生是%f分\n",p->num,p->score);
- p=p->next;
- }while(p!=NULL);//NULL != p 别这样0!=p->num OTZ!!!
- }
- }
复制代码 主要就是最后的那个东西 最后的那个print 我注释起来了
第二个 我郁闷!!!
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #define LEN sizeof(struct student) // student结构的大小
- struct student *creat(); //创建链表
- void print(struct student *head); //打印链表
- int n;//定义这个是第几个学生
- struct student{
- int num;
- float score;
- struct student *next;
- };//链表结构
- void main(){//首先定义一个可以让函数调用的变量,然后调用创建,之后打印出来
- struct student *p;//指向我们自己创建的头
-
- p=creat();
- print(p);
- printf("\n\n");
- system("pause");
- }
- struct student *creat(){
- struct student *head;//最后返回的是head
- struct student *p1,*p2;//这个是两个小精灵似的东西来回跑和定义
-
- p1 =(struct student *)malloc(LEN);//malloc是在内存创建len长度的空间
- printf("one num:");
- scanf("%d",&p1->num);
- printf("one score");
- scanf("%f",&p1->score);
- p2=p1;
- head=NULL;
- n=0;
- while (p1->num!=0){
- n++;
- if (n==1){
- head=p1;
- }
- else{
- p2->next=p1;
- }
- p2=p1;
- p1=(struct student *) malloc(LEN);
-
- printf("\nshu ru num:");
- scanf("%d",&p1->num);
- printf("shu ru score:");
- scanf("%f",&p1->score);
- }
- p2->next = NULL;
- return head;
- }
- void print(struct student *head){
- struct student *p;
- p=head;
- printf("%d个\n",n);
- if (NULL!=head){
- do{
- printf("%d号学生是%f分\n",p->num,p->score);
- p=p->next;
- }while(0!=p->num);//NULL != p 别这样0!=p->num OTZ!!!
- }
- }
复制代码
|
最佳答案
查看完整内容
大哥,当然不一样了,p->num是结构里面的值。地址是一样,但是地址是这样表示的:&(p->num)
所以你要改成这样也是可以的: 0!=&(p->num),不过不推荐这样
|