#include<stdio.h> #include<math.h> #include<algorithm> #include<string.h> #include<stdlib.h> #include<malloc.h> #define len sizeof(struct student) struct student { long num; float score; struct student *next; }; using namespace std; int n; struct student *creat() { struct student *p1,*p2,*head; n=0; p1=p2=(struct student *)malloc(len); //开辟了一个 长度为 len的空间用强制转换类型 得到它的首地址...赋给p1...p2.... scanf("%ld%f",&p1->num,&p1->score); //在地址前面加上 地址运算符,,表示对这个地址的数据进行操作.... head=NULL; //让头指针归零(没有什么卵用)......好像 while(p1->num!=0) // { n++; if(n==1) head=p1; else p2->next=p1; p2=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; if(head!=NULL) { do { print("%ld%f",p->num,p->score); p=p->next; }while(p!=NULL); } } void main() { struct student *head; head=creat(); print(head); }
---------------------------输出代码-----------------------------------------------------
#include<stdio.h> #include<stdlib.h> #define len sizeof(struct student) struct student { long num; float score; struct student *next; }; int n; void print(struct student *head) { struct student *p; p=head; if(head!=NULL) { do { printf("%ld%5.1f\n",p->num,p->score); p=p->next; }while(p!=NULL); } }
时间: 2024-11-06 21:45:33