电话簿采用双链表

Phonebook using double Linked List

本文关键字:链表 电话簿      更新时间:2023-10-16

问题是程序在第一个选择下运行良好但它开始重复而不要求选择并进入addNode或SearchNode函数

list *newList = new list;
int choice = 0;
while (choice != 3)
{
    printf("What would you like to do?n");
    printf("1 - Insert something in the phonebook?n");
    printf("2 - Search something from the phonebook?n");
    printf("3 - Nothing at alln");
    printf("Enter 1 through 5: ");
    scanf_s("%d", &choice);

    switch (choice) {
    case 1:
        newList->addNode();
        break;
    case 2:
        newList->searchNode();
        break;
    default:
        printf("nThank you for using the phonebookn");
    }
    choice = 0;
}

在循环结束时将choice设置为0。
然后代码在循环的顶部检查值。
循环顶部的值是0,它不等于3,所以循环继续。

如果您仍然希望在while循环中保留'choice=0;',您可以使用类似的代码:

如果(选择! = 3)选择= 0;

希望这对你有帮助!