【问题标题】:error in linked list in CC中的链表错误
【发布时间】:2017-12-20 02:46:21
【问题描述】:

程序在请求之前接受输入。当我们输入第一个节点的值后,问题就开始了。

这是一个简单的程序,它接收用户的输入并将其存储在一个链表中,然后显示存储的数据。

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

struct NODE
{
    int data;
    struct NODE* next;
};

void main()
{      
    struct NODE  *first,*old,*new_node;
    int n,i;
    printf ("Enter number of elements: \n");
    scanf ("%d",&n);
    first=(struct NODE*)malloc(sizeof(struct NODE));
    first->next= NULL;

    printf ("Enter value of node 1: \n");
    scanf ("%d\n",&first->data);
    old = first;
    for(i=2;i<=n;i++)
    {
        new_node=(struct NODE*)malloc(sizeof(struct NODE));
        new_node->next= NULL;

        printf("Enter value of node %d: \n",i);
        scanf("%d\n",&new_node->data);
        old->next=new_node;
        old = new_node;
    }

    old=first;
    while(old!= NULL)
    {
        printf("%d \t",old->data);
        old=old->next;
    }
}

【问题讨论】:

  • 如果你能澄清确切的问题。
  • 向我们展示输出
  • @anuj shrivastav 看来您正在寻找单个链接列表。你用谷歌搜索过吗?你的头节点在哪里?为什么从 i=2 开始循环?为什么不能从循环内创建第一个节点?
  • @TusharSharma 我首先用作头节点
  • @anuj shrivastav 你的意见是什么?

标签: c linked-list


【解决方案1】:

问题是您的scanf format specification 中的\n,其中包含(我强调):

很少有常量(即没有格式化的字符 占位符)在格式字符串中,主要是因为程序通常是 不是为读取已知数据而设计的。 例外是一个或多个 空白字符,它丢弃所有空白字符 输入。

所以您的\n 意味着您在每个数字之后输入的换行符将被忽略,并且下一个scanf() 直到您键入 another 换行符才完成(您从第一个节点计数输入中正确省略它)。

您需要做的就是从格式字符串中删除\n,您的代码就会按预期工作:

    ...
    scanf ("%d\n",&first->data);
    ...
    scanf("%d\n",&new_node->data);
    ...

请注意,您可以通过在printf() 格式字符串中省略\n 并在调用scanf() 之前调用fflush(stdout) 来与提示在同一行输入,例如:

    printf ("Enter number of elements: "); 
    fflush(stdout);
    scanf ("%d",&n);

这会给你一个更自然的对话。

【讨论】:

    【解决方案2】:

    你可以像这样执行所有链表操作

     #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    struct Node{
        struct Node *next;
        int data;
    
    };
    
    void printList(struct Node *n)
    {
      while (n != NULL)
      {
         printf(" %d ", n->data);
         n = n->next;
      }
    }
    
    void insertFirst( struct Node *start,int data1)
     {
            struct Node *n=(struct Node*)malloc(sizeof(struct Node));
            n->data=data1;
            n->next=start->next;
            start->next=n;
     }
    
    void insertLast( struct Node *start,int data1)
     {
        struct Node *last,*previous;
        last=start->next;
        while(last!=NULL)
        {
            previous=last;
            last=last->next;    
         }
            struct Node *n=(struct Node*)malloc(sizeof(struct Node));
            n->data=data1;
            n->next=NULL;
            previous->next=n;
     }
    
    void insertPosition(struct Node *start, int pos, int data1)
    {
        int count=0;
        struct Node *node;
        node=start->next;
        while(node!=NULL)
        {
            count++;
            node=node->next;
        }
        printf("total elements before insertion is %d\n ",count);
    
        if(count+1<pos)
        printf("cannot insert at desired position ");
        else
        {
         int i=1;
         node=start;
            while(i<pos)
            {
                node=node->next;
                i++;
            }
    
          struct Node *n=(struct Node*)malloc(sizeof(struct Node));
          n->data=data1;
          n->next=node->next;
          node->next=n;
    }
    }
    
    void deleteFirst(struct Node *start)
    {
        struct Node *firstNode;
        firstNode= start->next;
        start->next=firstNode->next;
        free(firstNode);
        printf("first node is removed\n");
    }
    
    void deleteLast(struct Node *start)
    {
        struct Node *last,*previous;
        last=start;
        while(last->next!=NULL)
        {
            previous=last;
            last=last->next;    
    
         }
            previous->next=NULL;
    
        free(last);
        printf("last node is removed\n");
    }     
    
    void deletePosition(struct Node *start, int pos)
    {
        int count=0;
        struct Node *node;
        struct Node *previous;
        node=start->next;
        while(node!=NULL)
        {
            count++;
            node=node->next;
        }
        printf("total elements before deletion is %d\n ",count);
    
        if(count<pos)
        printf("cannot delete the desired position ");
        else
        {
         int i=1;
         node=start->next;
            while(i<pos)
            {
                previous=node;
                node=node->next;
                i++;
            }
    
          previous->next=node->next;
          free(node);
            printf("node is removed\n");
    
    }
    }
    
    int main()
    {
        struct Node *start=NULL;
        struct Node *node1=NULL;
        struct Node *node2=NULL;
        struct Node *node3=NULL;
        struct Node *node=NULL;
    
        start=(struct Node*)malloc(sizeof(struct Node));    
        node1=(struct Node*)malloc(sizeof(struct Node));
        node2=(struct Node*)malloc(sizeof(struct Node));
        node3=(struct Node*)malloc(sizeof(struct Node));
    
    
        start->next=node1;
        node1->data=1;
        node1->next=node2;
    
         node2->data=2;
        node2->next=node3;
    
         node3->data=3;
        node3->next=NULL;
        printf("\nsize %d\n",sizeof(struct Node));
    
    
        //insertFirst(start,10);
        //insertLast(start,200);
       // insertPosition(start,2,300);
    
        // deleteFirst(start);
      //  deleteLast(start);
      //deletePosition(start,2);
         printList(start->next);
    }
    

    可以移除 cmets 以执行该操作。

    【讨论】:

    • 我相信 OP 希望知道他自己的代码有什么问题。您的回答根本没有解决 OP 的问题。
    • 是的,但这段代码是 OP 问题的解决方案,也是帮助他的附加功能。
    • 您没有使用与 OP 相同的布局,也没有使用相同的功能或参数。没有冒犯的意思,但如果 OP 想要已经写好的代码,谷歌搜索就可以了。
    • malloc的返回不用强制转换,没必要。见:Do I cast the result of malloc?
    • @DavidC.Rankin ya 你是绝对正确的,但如果你将程序保存在 .cpp 甲酸盐(C++)中,它会导致错误。这就是我这样做的原因
    猜你喜欢
    • 2013-04-19
    • 2021-09-10
    • 1970-01-01
    • 2013-10-21
    • 2012-06-12
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多