- 论坛徽章:
- 0
|
怎么还不行吗?我原来给你发了个不行吗?在我机子上运行对的啊?
- #include <malloc.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define LEN sizeof(struct student)
- int n;
- struct student
- {
- long num;
- float score;
- struct student *next;
- };
- /* creat() */
- struct student *creat()
- {
- struct student *p1,*p2;
- struct student *head;
- n=0;
- p1=(struct student *)malloc(LEN);
- p2 = p1;
- printf("Input num and score:");
- scanf("%ld,%f",&p1->num,&(p1->score));
- head=NULL;
- while (p1->num != 0)
- {
- printf("num=%ld,score=%f\n",p1->num,p1->score);
- n=n+1;
- if(n == 1) head = p1;
- else p2->next = p1;
- p2 = p1;
- p1=(struct student *)malloc(LEN);
- printf("Input num and score:");
- scanf("%ld,%f",&p1->num,&p1->score);
- }
- p2->next=NULL;
- return(head);
- }
- /* print() */
- void print(struct student *head)
- {
- struct student *p;
- printf("\nNow,These %d record are:\n",n);
- p = head;
- if(head!=NULL)
- {
- do
- {
- printf("%ld,%5.1f\n",p->num, p->score);
- p=p->next;
- }while(p!=NULL);
- }
- }
- /* insert() */
- struct student *insert(struct student *head,int i)
- {
- struct student *p1=head,*p;
- struct student *q = NULL;
- int k = 1;
- q = (struct student *)malloc(sizeof(struct student));
- if (!q) printf("OVERFLOW!"),exit(0);
- printf("Input num and score:");
- scanf("%ld,%f", &q->num, &q->score);
- for (; k<i&&p1; k++)
- {
- p1 = p1->next;
- }
- if (!p1 && i<1) printf("input error!\n"),exit(0);
- q->next = p1->next;
- p1->next = q;
- return head;
- }
- struct student *delete(struct student *head, int num)
- {
- struct student *p1,*p2;
- p1=head;
- while((p1->num != num) && (p1!=NULL))
- {
- p2=p1;
- p1=p1->next;
- }
- if (!p1) printf("The List null!\n"),exit(0);
- else
- {
- p2->next = p1->next;
- free(p1);
- }
- return(head);
- }
- int main()
- {
- struct student *head,*q;
- long del_num;
- int ins_location;
- head=creat();
- print(head);
- printf("Input the ins_location:");
- scanf("%d",&ins_location);
- head = insert(head, ins_location);
- print(head);
- printf("delete:please input the num:");
- scanf("%ld",&del_num);
- head=delete(head,del_num);
- print(head);
- return 0;
- }
复制代码 |
|