我第一次使用链表。想要就如何解决某些问题提出一些批评和建议

时间:2019-04-08 02:42:16

标签: c linked-list

该程序适用于酒店洗衣服务。用户输入他们的房间号,名字和姓氏以及他们要清洗的物品数量。该信息被放入链表中的节点中。在主菜单中,用户可以添加更多房间请求,更新请求,打印请求或退出程序。

结构可以定义为

struct request{
    int room_number;
    char first[NAME_LEN+1];
    char last[NAME_LEN+1];
    int num_items;
    struct request *next;
};

我的功能遇到了一些问题:

  1. 附加函数:
/*
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;

}

我遇到的一些问题是由于某种原因我不能提出两个以上的请求。我收到一个错误,程序崩溃了。

  1. 更新功能:
/*
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;
}

此功能的问题在于,当我输入房间号时,程序只会停止...不会崩溃,似乎卡住了...不确定为什么。

  1. 最后是打印功能:
/*
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;
    }
}

此功能的唯一问题是,它可以无限打印所有节点而不会停止。

感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:3)

  1. 您需要确定要在列表的末尾还是new_nodelist = new_node将其放在开头,随后的循环将其放在结尾,因此您创建了一个没有结尾的循环列表,而您的下一个插入操作陷入无限循环。如果希望new_node在开头,则无需搜索结尾。如果最后需要,则必须将new_node->next设置为NULL,而不是list
  2. 您的for循环主体仅执行一次,因为您在return语句的两臂中都有if
  3. 这可能是因为上面的要点1。