|
10鱼币
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #define LEN sizeof(struct student)
- struct student *create();
- void print(struct student *head);
- struct student *del(struct student *head, int num);
- int n;
- struct student
- {
- int num;
- float score;
- struct student *next;
- };
- int main()
- {
- int delnum;
- struct student *stu;
- stu = create();
- print(stu);
- printf("Please input delete num:");
- scanf("%d", &delnum);
- del(stu, delnum);
- print(stu);
- system("pause");
- }
- struct student *del(struct student *head, int num)
- {
- struct student *p, *pprev;
- pprev = p = head;
- if(head->num == num)
- head = head->next;
- p=p->next;
- while(p)
- {
- if(p->num == num)
- pprev->next = p->next;
- pprev=p=p->next;
- }
- return head;
- }
- void print(struct student *head)
- {
- struct student *p;
- p = head;
- while(p)
- {
- printf("student: %d, score: %f \r\n", p->num, p->score);
- p=p->next;
- }
- }
- struct student *create()
- {
- struct student *head;
- struct student *p1, *p2;
- p1 = p2 = (struct student *)malloc(LEN);
- printf("Please input num:");
- scanf("%d", &p1->num);
- printf("Please input score:");
- scanf("%d", &p1->score);
- head = NULL;
- n=0;
- while(p1->num)
- {
- n++;
- if(1 == n)
- {
- head = p1;
- }
- else
- {
- p2->next = p1;
- }
- p2 = p1;
- p1 = (struct student *)malloc(LEN);
- printf("Please input num:");
- scanf("%d", &p1->num);
- printf("Please input score:");
- scanf("%d", &p1->score);
- }
- p2->next = NULL;
- return head;
- }
复制代码
小弟我正在看小甲鱼老师的C语言视频第56课,写得如上程序,使用VS2010能正常编译运行(后面的部分自己写的,所以跟老师的不一样)。
可是假若把delnum的声明语句位置换一下,即把main函数改成:
int main()
{
struct student *stu;
stu = create();
print(stu);
int delnum;
printf("Please input delete num:");
scanf("%d", &delnum);
del(stu, delnum);
print(stu);
system("pause");
}
就会出现如下错误:
Error 1 error C2143: syntax error : missing ';' before 'type' e:\c\list1\list1\list1.c 25 1 list1
在网上找不到相关的说明,有知道的请不吝赐教,谢谢!
|
|