C中的基本链接列表操作

时间:2015-07-14 09:20:55

标签: c data-structures malloc singly-linked-list

我正在创建一个程序来执行基本的链表操作。现在我只编写了代码,用于在前面插入节点。我运行我的程序以查看其是否正常工作,但程序在接受节点的输入后终止,然后在切换后打印消息。它甚至没有停止接受我的输入来继续操作(就在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;
}

3 个答案:

答案 0 :(得分:1)

在您的情况下,您必须更改

<?php
    //change this to your email. 
    $to = "xxxxxxxxx@gmail.com"; 
    $from = "xxxxxxx@gmail.com"; 
    $subject = "Hello! This is HTML email"; 

    //begin of HTML message 
    $message = '
<html> 
  <body bgcolor="#DCEEFC"> 
    <center> 
        <b>Looool!!! I am reciving HTML email......</b> <br> 
        <font color="red">Thanks Mohammed!</font> <br> 
        <a href="http://www.maaking.com/">* maaking.com</a> 
    </center> 
      <br><br>*** Now you Can send HTML Email <br> Regards<br>MOhammed Ahmed - Palestine 
  </body> 
</html>'; 


    $headers  = "From: $from\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 

    //options to send to cc+bcc 
    //$headers .= "Cc: [email]maa@p-i-s.cXom[/email]"; 
    //$headers .= "Bcc: [email]email@maaking.cXom[/email]"; 

    // now lets send the email. 
    mail($to, $subject, $message, $headers); 

    echo "Message has been sent....!"; 
?>

 scanf ("%c",&wish);

因为,如果在格式说明符之前没有包含前导空格,则会通过按 ENTER <来考虑生成并存储到输入缓冲区中的剩余scanf (" %c",&wish); (换行符) / kbd>第一次输入后的键。因此,第二个\n将不会等待用户输入。

答案 1 :(得分:0)

调用scanf()

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

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

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

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

要好得多。

答案 2 :(得分:0)

在输入&#39;选择&#39;之后有一个新行。按变量&#39;愿望&#39;进行扫描。因此,我们需要删除该换行符(&#39; \ n&#39;)。

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

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