该程序适用于酒店洗衣服务。用户输入他们的房间号、名字和姓氏,以及他们要洗的元素数量。该信息被放入链表中的节点中。在主菜单中,用户可以添加更多房间请求、更新请求、打印请求或退出程序。
结构可以定义为
struct request{
int room_number;
char first[NAME_LEN+1];
char last[NAME_LEN+1];
int num_items;
struct request *next;
};
我的函数遇到了一些问题:
- 追加函数:
/*
APPEND FUNCTION:
Gets the room number, first name, last name, and the number of items the user wants to wash.
Creates a new node and appends it to the end of the linked list.
*/
struct request *append_to_list(struct request *list)
{
struct request *new_node, *last_node, *search;
new_node = malloc(sizeof(struct request));
//new_node->next = NULL;
if(new_node == NULL)
{
printf("Error allocating memory.\n");
return list;
}
//get room number
printf("Enter room number: ");
scanf("%d", &new_node->room_number);
//search to see if the room number already exists in the LL.
for(search = list; search != NULL; search = search->next)
{
if(search->room_number == new_node->room_number)
{
printf("Room request already exists. Update request using main menu.");
return list;
}
}
//get first and last name
printf("Enter first name: ");
read_line(new_node->first, NAME_LEN+1);
printf("Enter last name: ");
read_line(new_node->last, NAME_LEN+1);
//get the number of items.
printf("Enter the number of items you wish to wash: ");
scanf("%d", &new_node->num_items);
new_node->next = list;
//if list is empty, return pointer to newly created linked list.
if(list == NULL)
{
list = new_node;
return new_node;
}
//else add request to the end of the LL and return pointer to the LL.
else
{
last_node = list;
while(last_node->next!=NULL)
last_node = last_node->next;
}
last_node->next = new_node;
return list;
}
我遇到的一些问题是由于某种原因我不能发出超过两个请求。我收到一个错误,程序崩溃了。
- 更新函数:
/*
UPDATE FUNCTION:
User enters their room number and the node containing the room number is updated with the number of items the user wants to add on.
*/
void update(struct request *list)
{
struct request *search;
int add_items;
//ask to enter room num
printf("Enter room number: ");
int room;
scanf("%d\n", &room);
//find matching room num
for(search = list; search != NULL; search = search->next)
{
if(search->room_number == room)
{
//ask to enter num of items to be added and update num of items
printf("How many items would you like to add: ");
scanf("%d\n", &add_items);
search->num_items = search->num_items + add_items;
search = search->next;
return;
}
}
//if room num is not found, print a message.
printf("Could not find request.");
return;
}
我在使用此功能时遇到的一个问题是,当我输入房间号时程序会停止...它不会崩溃,它似乎卡住了...不太清楚为什么。
- 最后是打印函数:
/*
PRINTLIST FUNCTION:
Prints all the nodes in list.
*/
void printList(struct request *list)
{
//print room num, first and last name, and num of items for all requests on the list.
while(list != NULL)
{
printf("%d ", list->room_number);
printf("%s ", list->first);
printf("%s ", list->last);
printf("%d\n ", list->num_items);
list = list->next;
}
}
我对这个函数的唯一问题是它会不停地无限打印所有节点。
感谢任何帮助。谢谢!
最佳答案
- 您需要决定是要将
new_node
放在列表的末尾还是开头。list = new_node
将它放在开头,后续循环将它放在末尾,因此您创建了一个没有结尾的循环列表,并且您的下一个插入操作陷入了无限循环。如果你想让new_node在开头,你不需要搜索结尾。如果你想在最后,那么new_node->next
必须设置为 NULL,而不是list
。 - 您的
for
循环体只执行一次,因为您在if
语句的两个分支中都有return
。 - 这可能是因为上面的要点 1。
关于c - 我第一次使用链表。想要一些关于如何解决某些问题的批评和建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565570/