C中的基本链接列表操作

时间:2022-12-19 07:21:42

I am creating a program to perform basic linked list operations. Right now i have wrote the code only for inserting the node at the front. I ran my program to see its working or not but the program is terminating after accepting the input for the node, then it prints the message after switch. It doesn't even pause for accepting my input for continuing the operations (just before end of main())

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

here is the code :

这是代码:

#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;
}

3 个解决方案

#1


1  

In your case, you have to change

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

 scanf ("%c",&wish);

to

scanf (" %c",&wish);

because, if you don't include the leading white-space before the format specifier, it will consider the remaining \n (newline) which got generated and stored into the input buffer by pressing ENTER key after the first input. So, the second scanf() won't wait for the user input.

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

#2


0  

when calling scanf()

当调用scanf()时

1) with a '%d' format specifier, the trailing newline, from where the user entered the number, will not be consume.

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

2) with a '%c' format specifier, leading white space, like a newline, will cause the scanf() to fail, leaving the parameter (wish) unchanged.

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

3) in the posted code, when the 'wish' does not contain a valid 'Y' or 'y' then the program exits.

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

I agree with the other poster, that adding a choice '0' for exiting would be a much better way than the separate call to scanf()

我同意另一张海报,为退出添加选项'0'比单独调用scanf()更好

#3


0  

there is a new line after giving the input 'choice' which scan by the variable 'wish'. So we need to remove that newline ('\n').

在给出输入“选择”之后有一个新行,它通过变量'wish'扫描。所以我们需要删除该换行符('\ n')。

So if you want the user to continue just use a getchar() before take the input wish. Its easy and simple.

因此,如果您希望用户在获取输入愿望之前继续使用getchar()。它简单易行。

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

#1


1  

In your case, you have to change

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

 scanf ("%c",&wish);

to

scanf (" %c",&wish);

because, if you don't include the leading white-space before the format specifier, it will consider the remaining \n (newline) which got generated and stored into the input buffer by pressing ENTER key after the first input. So, the second scanf() won't wait for the user input.

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

#2


0  

when calling scanf()

当调用scanf()时

1) with a '%d' format specifier, the trailing newline, from where the user entered the number, will not be consume.

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

2) with a '%c' format specifier, leading white space, like a newline, will cause the scanf() to fail, leaving the parameter (wish) unchanged.

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

3) in the posted code, when the 'wish' does not contain a valid 'Y' or 'y' then the program exits.

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

I agree with the other poster, that adding a choice '0' for exiting would be a much better way than the separate call to scanf()

我同意另一张海报,为退出添加选项'0'比单独调用scanf()更好

#3


0  

there is a new line after giving the input 'choice' which scan by the variable 'wish'. So we need to remove that newline ('\n').

在给出输入“选择”之后有一个新行,它通过变量'wish'扫描。所以我们需要删除该换行符('\ n')。

So if you want the user to continue just use a getchar() before take the input wish. Its easy and simple.

因此,如果您希望用户在获取输入愿望之前继续使用getchar()。它简单易行。

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