|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <malloc.h>
- #define LEN sizeof(struct student )
- int n=0;//for whole
- struct student
- {
- int num;
- float score;
- struct student *next;
- };
- struct student *creat();//creat declearation
- void print(struct student *head); //declear of print
- struct student *del(int i,struct student *head);
- struct student *insert(struct student *head,struct student *pstu2);
- int main(void)
- {
- int i;// for del
- struct student *head_for_del;
- struct student *head_for_insert;
- struct student stu;
- struct student *head;
-
- struct student *stu2;
- head=creat();
- print(head);
-
- stu2= (struct student *)malloc(LEN);
-
- printf("\n\n");
- printf("Now please input a new member:\n");
- printf("plz input num:");
- scanf("%d",&(stu2->num));
- printf("plez input score:");
- scanf("%f",&(stu2->score));
- printf("\n");
-
- insert(head,stu2);
-
- // printf("now the inserted order is:\n");
- // print(insert(stu2,head));
- print(insert(head,stu2));
- return 0;
- }
- struct student *creat()
- {
- struct student *p1,*p2,*head;
- head=p1=(struct student *)malloc(LEN);//len is sizeof (struct student)
-
- printf("plz input num:");
- scanf("%d",&(p1->num));
- printf("plez input score:");
- scanf("%f",&(p1->score));
- printf("\n");
-
- if (p1->num==0)
- {
- head=NULL;
- n=0;
- goto end;
- }
-
- /* while (p1->num!=0)
- {
-
- p2=p1;
- p1=(struct student *)malloc(LEN);
- p2->next = p1;
-
- printf("plz input num:");
- scanf("%d",&(p1->num));
- printf("plez input score:");
- scanf("%f",&(p1->score));
- printf("\n");
- n=n+1;
- }
- */
-
- //p1->next=NULL;
-
-
- end:
- return head;
- }
- void print(struct student *head)
- {
- printf("there are %d record is this groop!\n",n);
- struct student *p;
- p=head;
- // printf("%0x",head); // 在这里的时候 head ==0 63行代码
- if(head!=NULL&&p->next==NULL)
- {
- printf("%d\t",p->num);
- printf("%f\n",p->score);
- }
-
- /* while(head!=NULL&&p->next!=NULL)
- {
-
- printf("%d\t",p->num);
- printf("%f\n",p->score);
- p=p->next;
-
- }*/
- }
- struct student *insert(struct student *head,struct student *pstu2)
- {
- struct student *p,*p1;
-
- //p=head;
-
- if(head==NULL) //空链表
- {
-
- printf("daima 128 n=%d\n",n); // 128行代码
- head=pstu2;
- pstu2->next=NULL;
- goto end;
- }
- /* if(p->next==NULL)
- {
-
- }
- */
-
-
-
-
- end :
- return head;
- }
复制代码 为什么128行代码 执行了2次?
运行时 首先请输入 学号为0 ,才会创建一个空的链表
会发现错误
|
|