在turbo c 2.0 下调试通过.(把汉语注释去掉)
- #include"stdio.h"
- #include"malloc.h"
- #define NULL 0
- #define L sizeof(struct integer)
- struct integer
- {
- int num;
- int zhengshu;
- struct integer *next;
- };
- int n;
- struct integer *creat(void)
- {
- struct integer *head;
- struct integer *p1,*p2;
- n=0;
- p1=p2=(struct integer *)malloc(L);
- scanf("%d,%d",&p1->num,&p1->zhengshu);
- head=NULL;
- while(p1->num!=0)
- {
- n=n+1;
- if(n==1) head=p1;
- else p2->next=p1;
- p2=p1;
- p1=(struct integer *)malloc(L);
- scanf("%d,%d",&p1->num,&p1->zhengshu);
- }
- p2->next=NULL;
- return(head);
- }
-
- void print(struct integer *head)
- {
- struct integer *p;
- printf("Now %d biaohao and zhengshu are :n",n);
- p=head;
- if(head!=NULL)
- do
- {printf("%d,%5.1dn",p->num,p->zhengshu);
- p=p->next;
- }while(p!=NULL);
- }
-
- int count(struct integer *head)
- {
- int i=0;
- struct integer *p;
- p=head;
- while(p!=NULL)
- {
- p=p->next;i++;
- }
- return i;
- }
-
- void *findnode(struct integer *head,int num)
- {
- int j=1;
- struct integer *p;
-
-
-
-
-
- p=head;
- if(num>count(head)||num<=0)
- {
- printf("Input error! Please input againn");
- }
- else
- {
- while(p!=NULL && j<num)
- {
- j++;
- p=p->next;
- }
- printf("%d bianhao reprensts %dn",p->num,p->zhengshu);
- printf("n");
- }
- return(head);
- }
-
-
- struct integer *del(struct integer *head,long num)
- {
- struct integer *p1,*p2;
- if(head==NULL)
- {
- printf("nList Null!n");
- return;
- }
- p1=head;
- while(num!=p1->num && p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(num==p1->num)
- {
- if(p1==head) head=p1->next;
- else p2->next=p1->next;
- printf("Delete: %dn",num);
- n=n-1;
- }
- else printf("%d not been fonnd!n",num);
- return(head);
- }
-
- struct integer *insert(struct integer *head,struct integer *stud)
- {
- struct integer *p0,*p1,*p2;
- p1=head;
- p0=stud;
- if(head==NULL)
- {
- head=p0;
- p0->next=NULL;
- }
- else
- {
- while((p0->num>p1->num)&&(p1->next!=NULL))
- {
- p2=p1;
- p1=p1->next;
- }
- if(p0->num<=p1->num)
- {
- if(head==p1)head=p0;
- else p2->next=p0;
- p0->next=p1;
- }
- else
- {
- p1->next=p0;
- p0->next=NULL;
- }
- }
- n=n+1;
- return(head);
- }
-
- main()
- {
- int a,b;
- struct integer *head,stu;
- int del_num,fin_num;
- printf("1 to go on / 0 to exit:n");
- scanf("%d",&a);
- while(a!=0)
- {
-
-
- printf("1 creat 2 print 3 delete 4 insert 5 findonde 0 exitn");
-
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
-
- printf("Please Input bianhao and data:n");
- printf("for example 1,90 0,0 to exit:n");
- head=creat();
- }break;
- case 2:
- {
-
- print(head);
- }break;
- case 3:
- {
-
- printf("nInput the deleted biaohao:");
- scanf("%d",&del_num);
- head=del(head,del_num);
- }break;
- case 4:
- {
-
- printf("nInput the inserted biaohao and zhengshu:");
- scanf("%d,%d",&stu.num,&stu.zhengshu);
- head=insert(head,&stu);
- }break;
- case 5:
- {
-
- printf("Please Input the bianhao you want to find:");
- scanf("%d",&fin_num);
- head=findnode(head,fin_num);
- }break;
- case 0:
- {
- return;
- }break;
- default:
- printf("Input error! Please input againn");
- }
- }
- }
(cuishinankobe) |