|
发表于 2014-9-27 09:24:24
|
显示全部楼层
- # include <stdio.h>
- # include <malloc.h>
- # define LEN sizeof(struct Student)
- struct Student
- {
- long num;
- float socre;
- struct Student * next;
- };
- int n;//定义全局变量n,n是结点个数
- struct Student * creat()//建立链表函数
- {
- //struct Student * head;
- struct Student * he;
- struct Student *p1,*p2;
- n = 0;
- p1 = p2 = (struct Student *)malloc(LEN);
- //scanf("%ld,%f",p1->num ,p1->socre );
- scanf("%ld,%f",&p1->num ,&p1->socre );
- he = NULL;
- while( p1->num != 0)
- {
- n = n+1;
- /*
- if(n == 1)
- head = p1;
- else
- p2->next = p1;
- p2 = p1;
- p1 = (struct Student *)malloc(LEN);
- scanf("%ld,%f",&p1->num ,&p1->socre );
- */
- if(n == 1)
- {
- he = p1;
- }
- else
- {
- p2 = (struct Student *)malloc(LEN);
- p1->next = p2;
- scanf("%ld,%f",&p2->num ,&p2->socre );
- p1 = p2;
- }
- }
- //p2->next = NULL;
- p1->next = NULL;
- return (he);
- }
- void print(struct Student * head)//输出链表函数
- {
- struct Student * p;
- printf("\nNow,These %d records are:\n",n);
- p = head;
- if(head != NULL)
- do
- {
- printf("%ld %5.1f\n",p->num ,p->socre );
- p = p->next;
- }while(p != NULL);
- }
- int main()
- {
- struct Student * head;
- head = creat();
- print( head);
- return 0;
- }
复制代码 |
|