|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 surongre 于 2011-12-8 09:03 编辑
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #define LEN sizeof(struct student) //student结构体大小
- struct student *creat(); //创建链表函数
- struct student *del(struct student *head,int num); //删除链表节点函数
- void print(struct student *head); //打印链表函数
- struct student //定义结构体
- {
- int num;
- float score;
- struct student *next;
- };
- int n; //全局变量,用来记录存放了多少个节点
- void main()
- {
- int num;
- struct student *stu,*p; //定义一个指向student结构体的指针
-
- stu = creat(); //调用creat函数,创建链表,并把creat函数的返回值赋值给stu
- p = stu;
- print(stu); //调用print函数输出,参数为stu
- printf("Please input you want to delete node:");
- scanf("%d",&num); //获取输入节点信息
- print(del(p,num)); //调用del函数进行链表节点删除
-
- printf("\n\n"); //打印2个回车
- system("pause"); //暂停
- }
- struct student *creat() //crate函数
- {
- struct student *head; //定义head指针
- struct student *p1,*p2; //定义p1,p2指针
- p1 = p2 = (struct student *)malloc(LEN); //调用malloc函数向系统申请sizeof(struct student)个空间
- //并通过强制转换赋值给p1和p2两个指针
- printf("Please input num:"); //打印提示
- scanf("%d",&p1->num); //获取整型输入,并赋值给p1->num
- printf("Pleaes input score:"); //打印提示
- scanf("%f",&p1->score); //获取浮点型输入,并赋值给p2->score
- printf("\n");
-
- head = NULL; //把NULL赋值给head,代表空
-
- n = 0; //初始化节点计数器
-
- while(p1->num) //如果p1->num不等于0,即为真,这执行循环体,否则退出
- {
- n++; //节点计数器加1
- if(1 == n) //如果节点计数器等于1,说明是第一个节点,赋值给head
- {
- head = p1; //赋值语句,把p1赋值给头结点
-
- }
- else //如果节点计数器不等于1
- {
- p2->next = p1; //则把p2->next指向p1
- }
-
- p2 = p1; //把p1赋值给p2
- p1 = (struct student *)malloc(LEN); //向系统申请sizeof(struct student)个空间,赋值给p1
-
- printf("Please input num:"); //打印提示
- scanf("%d",&p1->num); //获取整型输入,并赋值给p1->num
- printf("Please input score:"); //打印提示
- scanf("%f",&p1->score); //获取浮点型输入,并赋值给p1->score
- printf("\n");
- }
- p2->next = NULL; //结束循环,把p2->next置为NULL,代表链表建立结束
- free(p1); //释放p1指向的内存
- return head; //返回head
- }
- void print(struct student *head) //定义打印函数
- {
- struct student *p; //定义一个指向student结构体的指针p
- printf("\nThere are %d records\n",n); //打印节点计数器的值
- p = head; //把head赋值给p
- if(NULL != head) //如果head不等于NULL,执行循环体,否则退出
- {
- do
- {
- printf("学号为%d的成绩是%f\n",p->num,p->score); //打印p->num和p->score的值
- printf("\n");
- p = p->next; //把p->next赋值给p,即p指向下一个节点
- }while(p); //如果p不等于NULL,继续循环,否则退出
- }
-
- }
- struct student(struct student *head,int num) //定义删除链表节点函数
- {
- struct student *p1,*p2; //定义指向student结构的p1,p2,head指针
- p1 = head;
- p2 = p1->next;
- if(NULL == head)
- {
- printf("Error:This linked list is empty!!!\n");
- goto END;
- }
- else
- {
- while(p1->num != num && p1->next != NULL)
- {
- p2 = p1;
- p1 = p1->next;
- }
- if(p1->num == num)
- {
- if(p1 == head)
- {
- head = p1->next;
- }
- else
- {
- p2->next = p1->next;
- }
- printf("Delete node success\n");
- n = n-1;
- }
- else
- {
- printf("Error: Not Found %d node",num);
- goto END;
- }
- }
- END:
- return head; //这里出问题了
- }
复制代码 编译时提示 error C2440: 'return' : cannot convert from 'struct student *' to 'int'和error C2617: 'student' : inconsistent return statement
为什么会这样?我前面定义del函数是struct student *,和创建链表的函数定义是一样的,但是创建链表函数返回head则正常。。。。去掉删除链表节点的函数,正常编译正常运行 |
|