我正在做一个关于链表的练习,我刚刚开始学习它。它要求我输入房间号和酒店的事件,但当我试图打印房间号和事件的输出时,它会在我从菜单中选择列出所有房间的选项后停止程序。如何修复这些代码?
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
void insertRoom(int number, char name);
void listRoom(int number, char name);
struct Number {
int number;
char name;
struct Number *ptrnext;
};
struct Number *headptr, *newptr, *currentptr;
int main()
{
char ch, name;
int choice=TRUE, number;
headptr=NULL;
while(choice==TRUE) { //menu
printf("nne - Enter room number and event");
printf("nl - List all rooms");
printf("nx - Exitn");
printf("nEnter choice: ");
scanf(" %c",&ch);
switch(ch) {
case 'e':insertRoom(number,name);break;
case 'l':listRoom(number,name);break;
case 'x': choice=FALSE; break;
default: printf("nEnter only one from the above");
}
}
return 0;
}
void insertRoom(int number,char name) {
newptr=(struct Number *)malloc(sizeof(struct Number));
printf("nEnter a number: ");
scanf("%d",&newptr->number);
printf("nEnter name: ");
scanf(" %[^n]s",&newptr->name);
if (headptr==NULL) {
headptr=newptr;
newptr->ptrnext=NULL;
}
else {
newptr->ptrnext=headptr;
headptr=newptr;
}
}
void listRoom(int number,char name) {
if (headptr==NULL) {
printf("nEmpty list");
return;
}
currentptr=headptr;
do {
printf("nnRoom number tEvent");
printf("n%d t%s",currentptr->number,currentptr->name);
printf("n");
currentptr=currentptr->ptrnext;
} while(currentptr !=NULL);
}
以下是问题的输出:输出
在声明变量名以解决问题时,只需给出事件名称的大小
char name[30];