|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
看鱼哥(看很多人这么叫,就跟着叫了 )的教程,这个链表,是看着他教的的时候跟着他的思路写的,跟抄袭差不多,不知道怎么回事一个函数出错了,这个是看了看流程表自己dele()函数写的,请各位帮忙看看怎么样,先谢了!!- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- #define LEN sizeof(struct stu) //stu 结构的大小
- int n;
- struct stu *creat(); //创建链表
- struct stu *dele();
- void print(struct stu *head);
- struct stu
- {
- int num;
- float score;
- struct stu *next;
- }
- struct stu *creat() //这里出错了 !!
- {
- struct stu *head;
- struct stu *p1 ,*p2;
- head=NULL;
- n=0;
- p1=p2=(struct stu *) malloc(LEN);
- printf("input student id:");
- scanf("%d",&p1->num);
- printf("input student score:");
- scanf("%f",&p1->score);
- while(p1->num)
- {
- n++;
- if(1==n)
- {
- head=p1;
- }
- else
- {
- p2->next=p1;
- }
- p2=p1;
- p1=(struct stu *) malloc(LEN);
- printf("input student id:");
- scanf("%d",&p1->num);
- printf("input student score:");
- scanf("%f",&p1->num);
- }
- p2->next=NULL;
- return head;
- }
- int main ()
- {
- int i;
- struct stu *stu;
- stu=creat();
- print(stu);
- printf("\n");
- printf("input delete student id:\n");
- scanf("%d",&i);
- struct stu *dele(int i);
- system("pause");
- return 0;
- }
- void print(struct stu *head)
- {
- struct stu *p;
- p=head;
- if(head)
- {
- do
- {
- printf("学号为%d的成绩是:%f\n",p->num,p->score);
- p=p->next;
- }while(p);
- }
- }
- struct stu *dele(int i)
- {
- struct stu *head;
- struct stu *p,*p1;
- p=head;
- if(p->next)
- {
- while(p->next)
- {
- p1=p;
- p=p->next;
- printf("学号:%d 成绩:%f\n",p->num,p->score);
- while(p1->num!=i&&p->next!=NULL)
- {
- p=head;
- p1=p;
- if(1==n)
- {
- head=p->next;
- }
- else if(p->next)
- {
- p1->next=NULL;
- }
- else
- {
- p1->next=p->next;
- }
- }
- }
-
复制代码
|
|