|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 df3379 于 2019-7-24 16:17 编辑
- /*
- 要求:设计一个程序,
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct NODE/*定义结构体类型,
- 这只是一个类型,不是结构体变量
- */
- {
- char name[20];
- int age;
- char sex;
- char num[20];
- struct NODE *next;//用于保存下一个结构体变量的地址,指向下一个结构体变量
- };
- struct NODE *CreateLink(void);//函数声明,创建链表
- void Init(struct NODE *);//函数声明,链表结点初始化
- void OutputLink(struct NODE *);//函数声明,输出链表
- void InsertNode(struct NODE *);//函数声明,插入结点
- int main(void)
- {
- char ch='\0';//用于判断是否执行相关程序
- struct NODE *head=NULL;//定义指向空的struct NODE型结构体变量的头指针
- //以下创建链表
- printf("是否创建当前链表?(Y/N)");
- while(1)
- {
- scanf("%c",&ch);
- getchar();//吸收回车,
- if(('Y'==ch)--('y'==ch))//提示: error C2105: '--' needs l-value
- {
- head=CreateLink();
- Init(head);
- OutputLink(head);
- break;//执行完毕退出创建链表
- }
- else if(('N'==ch)--('n'==ch))
- {
- return 0;
- }
- else
- {
- printf("请重新输入(Y/N):");
- }
- }
- //以下插入结点
- printf("是否要插入结点?(Y/N)");
- ch='\0';
- while(1)
- {
- scanf("%c",&ch);
- getchar();
- if(('Y'==ch)--('y'==ch))
- {
- InsertNode(head);
- OutputLink(head);
- break;
- }
- else if(('N'==ch)--('n'==ch))
- {
- break;
- }
- else
- {
- printf("请重新输入(Y/N):");
- }
- }
- return 0;
- }
- //以下为创建链表函数
- struct NODE *CreateLink(void)
- {
- int i=0;//循环变量
- int cnt=0;//学生的数量
- struct NODE *head=malloc(sizeof*head);//定义头指针,并初始化指向头结点
- struct NODE *move;
- if(NULL==head)
- {
- printf("内存分配失败,程序终止!");
- else(-1);
- }
- move=head;
- move->next=NULL;
- printf("请输入学生的数量:");
- scanf("%d",&cnt);
- getchar();
- for(i=0;i<cnt;++i)
- {
- struct NODE *fresh=malloc(sizeof*fresh);//
- if(NULL==fresh)
- {
- printf("内存分配失败,程序终止!");
- else(-1);
- }
- //结点连接三部曲
- move->next=fresh;//将新的结点连接到后面
- fresh->next=NULL;//新连上的结点初始化为指向NULL
- move=fresh;//指针变量move向后移动,指向当前新建的结点
- }
- return head;
- }
- //以下为链表结点初始化
- void Init(struct NODE *head)
- {
- int i=1;
- struct NODE *move=head->next;//初始化为首结点
- while(NULL!=move)
- {
- printf("请输入第%d个学生的信息",i);
- scanf("%s%d %c %s",move->name,&move->age,&move->sex,&move->num);
- getchar();
- move=move->next;
- ++i;
- }
- return;
- }
- //以下为链表输出函数
- void OutputLink(struct NODE *head)
- {
- struct NODE *move=head;
- if(NULL==move)
- {
- printf("未创建链表\n");
- return;
- }
- if(NULL==move->next)
- {
- printf("链表为空\n");
- }
- while(NULL!=move->next)
- {
- printf("[姓名:%s,年龄:%d,性别:%c,学号:%s]->",move->name,&move->age,&move->sex,&move->num);
- move=move->next;
- }
- printf("[^]\n");
- }
- //以下为插入结点函数
- void InsertNode(struct NODE *head)
- {
- char num[20]="\0";//输入一个学号,在这个学号的后面插入一个学生的信息
- struct NODE *fresh=malloc(sizeof*fresh);//存储要插入的学生的信息
- printf("请输入你想在那个学号的学生后面再插入一个学生信息:");
- while(1)//解决输入无该学号学生的BUG
- {
- struct NODE *move=head->next;//
- scanf("%s",num);
- getchar();
- while(NULL!=move)
- {
- if(0==strcmp(num,move->num))
- {
- printf("请输入插入学生的信息,分别是:姓名、年龄、性别、学号:");
- scanf("%s%d %c %s",fresh->name,&fresh->age,&fresh->sex,&fresh->num);
- getchar();
- //插入结点
- fresh->next=move->next;
- move->next=fresh;
- return;
- }
- move=move->next;
- }
- printf("无该结点,请重新输入:");
- }
- }
复制代码
报错如下:
1.第36、43、60、66行报错: error C2105: '--' needs l-value error C2064: term does not evaluate to a function
2.第88、101行报错:error C2181: illegal else without matching if
请问这是为什么?该如何改?
这是什么操作?谁教你的?
- if(('Y' == ch) || ('y' == ch))
复制代码
|
|