C 中的基本链表操作

Basic Linked List operations in C

我正在创建一个程序来执行基本的链表操作。现在我只编写了用于在前面插入节点的代码。我 运行 我的程序想看看它是否工作,但程序在接受节点输入后终止,然后它在切换后打印消息。它甚至不会暂停接受我的输入以继续操作(就在 main() 结束之前)

这是代码:

#include <stdio.h>
#include <stdlib.h>

struct linkedlist
{
    int num;
    struct linkedlist *next;
};
struct linkedlist *head = NULL;

void display();

void insertBeginning()
{

    struct linkedlist *obj;
    int no;

    obj = (struct linkedlist *)malloc(sizeof(struct linkedlist));

    if(obj == NULL)
    {
        printf("\n Overflow ");
    }
    else
    {
        printf("\n Enter the number = ");
        scanf("%d", &no);

        obj->num = no;

        if(head == NULL)
        {
            head = obj;
            obj->next = NULL;

        }
        else
        {
            obj->next = head;
            head = obj;
        }   

    }
}

void display ()
{
    struct linkedlist *head2 = head;
    while(head2 != NULL)
    {
        printf("%d ->",head2->num);
        head2=head->next;
    }
    printf("NULL \n");
}

int main()
{

    int choice;
    char wish;


    printf("\n 1. Insert at beginning");
    printf("\n 2. Insert at end");
    printf("\n 3. Insert in between");
    printf("\n 4. Delete from front");
    printf("\n 5. Delete from end");
    printf("\n 6. Delete from in between");
    printf("\n 7. Reverse");
    printf("\n 8. Sort ascending");
    printf("\n 9. Sort descending");
    printf("\n 10.Swap alternate elements");
    printf("\n 11.Display\n\n");



   do
   {
        printf("\n Enter the option = ");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                insertBeginning();
                break;

            case 2:
//              insertEnd();
                break;

            case 3:
//              insertInbetween();
                break;

            case 4:
//              deleteFront();
                break;

            case 5:
//              deleteEnd();
                break;

            case 6:
//              deleteInbetween();
                break;

            case 7:
//              Reverse();
                break;

            case 8:
//              sortAsc();
                break;

            case 9:
//              sortDesc();
                break;

            case 10:
//              swap();
                break;

            case 11:
                display();
                break;

            default:
                printf("\n Wrong choice ");

        }

        printf("\n Do you wish to continue (y/n) = ");
        scanf ("%c",&wish);

   }while(wish == 'y' || wish =='Y');   

return 0;
}

在你的情况下,你必须改变

 scanf ("%c",&wish);

scanf (" %c",&wish);

因为,如果您不在格式说明符之前包含前导白色-space,它将考虑剩余的 \n(换行符),它生成并存储到输入缓冲区中在第一次输入后按 ENTER 键。因此,第二个 scanf() 不会等待用户输入。

调用 scanf() 时

1) 使用“%d”格式说明符时,用户输入数字的尾随换行符将不会被消耗。

2) 带有 '%c' 格式说明符,前导白色 space,如换行符,将导致 scanf() 失败,使参数 (wish) 保持不变。

3) 在发布的代码中,当 'wish' 不包含有效的 'Y' 或 'y' 时程序退出。

我同意另一位发帖者的观点,即为退出添加选项“0”比单独调用 scanf()

更好

输入 'choice' 后有一个新行,由变量 'wish' 扫描。所以我们需要删除换行符 ('\n').

因此,如果您希望用户继续,只需在接受输入愿望之前使用 getchar() 即可。简单易行。

printf("\n Do you wish to continue (y/n) = ");
getchar();
scanf ("%c",&wish);