【问题标题】:linked list error on inserting new data in node or element in C在 C 中的节点或元素中插入新数据时出现链表错误
【发布时间】:2017-08-01 18:35:54
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int info;
    struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
    while(n!=NULL)
    {
        printf("%d ",n->info);
        n=n->next;
    }
}
int main()
{
    printf("Hello world!\n");
    list head,temp;
    char ch;
    head=NULL;
    printf("Want to add data:\n");
    scanf("%c",&ch);
    while(ch=='y'||ch=='Y')
    {
        temp=(list)malloc(sizeof(Node));
        scanf("%d",&temp->info);
        temp->next=head;
        head=temp->next;
        printf("Want to add more data:\n");
        scanf("%c",&ch);
    }
    printlist(head);
    return 0;
}

这是我的代码。 我的问题在这里,我不能和我的列表中的数据,但添加了节点...... 我认为我的“scanf”功能有问题.... 请帮我解决这个问题并将更正的代码发给我

                                  thank u...hope I can get a reply soon

【问题讨论】:

  • head=temp-&gt;next; --> head=temp;
  • 你需要改变scanf("%c",&ch);到 scanf("%c",&ch);在这两个地方。

标签: c singly-linked-list insert-update


【解决方案1】:

尝试将 head=temp->next 更改为 head=temp。您再次将 head 分配给自身。

【讨论】:

    【解决方案2】:

    除了上述答案之外,如果您希望保持将元素添加到链表的顺序(头部始终保持固定,只有在最初为 NULL 时才会更改为指向第一个元素),以下调整会解决这个问题。任何新元素总是添加到链表的末尾,头部固定在第一个元素。

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node
    {
      int info;
      struct node* next;
    }Node;
    
    typedef Node* list;
    
    void printlist(list n)
    {
      while(n!=NULL)
      {
         printf("%d ",n->info);
         n=n->next;
      }
    }
    
    int main(){
    
       printf("Hello world!\n");
       list head,temp;
       char ch;
       head=NULL;
       printf("Want to add data:\n");
       scanf("%c",&ch);
    
       while(ch=='y'||ch=='Y'){
    
          temp=(list)malloc(sizeof(Node));
          scanf("%d",&temp->info);
          temp->next=NULL;
          if(head == NULL){
              head = temp;
           }
          else{
              list temp2 = head;
              while(temp2->next != NULL){
                 temp2 = temp2->next;
              }
              temp2->next = temp;
          }
        printf("Want to add more data:\n");
        scanf(" %c",&ch);
        }
       printlist(head);
       return 0;
      }
    

    【讨论】:

    • 还是有同样的问题
    • 想要任何其他建议
    【解决方案3】:

    如下更改您的代码。 scanf("%c",&ch);到 scanf("%c",&ch);和头=临时->下一个;头=温度;对于 Scanf,请参见下面的链接 见链接scanf() function doesn't work?

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct node
    {
        int info;
        struct node* next;
    }Node;
    typedef Node* list;
    void printlist(list n)
    {
        while(n!=NULL)
        {
            printf("%d ",n->info);
            n=n->next;
        }
    }
    int main()
    {
        printf("Hello world!\n");
        list head,temp;
        char ch;
        head=NULL;
        printf("Want to add data:\n");
        scanf(" %c",&ch);
        while(ch=='y'||ch=='Y')
        {
            temp=(list)malloc(sizeof(Node));
            scanf("%d",&temp->info);
            temp->next=head;
            head = temp;
            printf("Want to add more data:\n");
            scanf(" %c",&ch);
        }
        printlist(head);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 2016-09-30
      • 1970-01-01
      • 2017-09-26
      相关资源
      最近更新 更多