【问题标题】:Ordered linked list printing有序链表打印
【发布时间】:2012-11-05 19:31:45
【问题描述】:

我正在做我的家庭作业,这就是我所得到的。我现在需要知道如何打印出已经输入到列表中的信息。并且我需要重新配置 insertNode 函数以将列表从小到大排序。

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

struct listNode{
  int data;    //ordered field
  struct listNode *next;
};

//prototypes
void insertNode(struct listNode *Head, int x);
int printList(struct listNode *Head, int x);
int freeList(struct listNode *Header, int x);

//main
int main(){
     struct listNode Head = {0, NULL};
     int x = 1;
     printf("This program will create an odered linked list of numbers greater"
     " than 0 until the user inputs 0 or a negative number.\n");
     while (x > 0){
     printf("Please input a value to store into the list.\n");
     scanf("%d", &x);
          insertNode(&Head, x);
     }
     printf("Program terminated.\n");
     system("PAUSE");
     }
void insertNode(struct listNode * Head, int x){
     struct listNode *newNode, *current;
     newNode = malloc(sizeof(struct listNode));
     newNode->data = x;
     newNode->next = NULL;
     current = Head;
     while (current->next != NULL && current->data < x) 
     {
        current = current->next;
        }

        if(current->next == NULL){
             current->next = newNode;
        }
        else{
             newNode->next = current->next;
             current->next = newNode;
        }
}

【问题讨论】:

标签: c


【解决方案1】:

Header-&gt;next 启动时为 NULL,添加元素时将 current 更改为 Header

 current = Header;
 write(current->next !=NULL);
 // set current to be Header->next (which is NULL)
 current = current->next;
 // dereference null
 current->next = newNode;

相反,将新元素添加到末尾:

current = Header;
while (current->next != NULL)
    current = current->next;
current->next = newNode;

【讨论】:

    【解决方案2】:
     current = Header;
     write(current->next !=NULL);
     current = current->next;
     current->next = newNode;
    

    您正在尝试访问 current->next 而没有分配它。而且您实际上并没有寻找将其插入链接列表的正确位置(从您的问题来看,这听起来应该是排序的)。

    尝试类似:

    current = Header;
    while (current->next != NULL && current->data < x) 
    {
        current = current->next;
    }
    
    if(current->next == NULL)
    {
        current->next = newNode;
    }
    else
    {
        newNode->next = current->next;
        current->next = newNode;
    }
    

    【讨论】:

    • 我试过了,它似乎奏效了!我用我的新问题编辑了原始帖子。
    • 我添加了对您其他问题的答案,以向您展示您做错了什么:stackoverflow.com/questions/13240332/…
    【解决方案3】:

    您的 impl 中存在三个问题:

    1. 你应该在末尾追加新的数字,所以你应该有两个指针到列表中:一个指向头部,另一个指向尾部(你也可以在列表顶部添加,然后你需要只有一个指针,但是列表是反向排序的)

    2. 指针链接代码是错误的,我想你想尝试在你的头节点之后插入新元素,所以你的代码应该这样写:

      当前=标题->下一个; // Header->next当前指向的节点

      标题->下一个=新节点; // 让 header->next 指向新元素

      新节点->下一个=当前; // 让新元素的下一个指向旧顶部元素

    3. header-node 有点特殊且无用,您应该将空列表作为特殊情况处理,header 最初应为 0,insertnode 的工作方式如下:

      如果(标题==0)

       header=newNode
      

      其他

      // 做 2 中写的事情。

    所有这些都可以通过不同的方式完成,但这是解决列表问题的一种常见方法...... (嗯,不知怎的,代码的 4 个前导空格不起作用?)

    【讨论】:

      【解决方案4】:

      搞定了,只需要弄清楚如何制作 printList 和 freeList 函数

      #include <stdio.h>
      #include <stdlib.h>
      
      struct listNode{
        int data;    //ordered field
        struct listNode *next;
      };
      
      //prototypes
      void insertNode(struct listNode *Head, int x);
      int printList(struct listNode *Head, int x);
      int freeList(struct listNode *Head, int x);
      
      //main
      int main(){
           struct listNode Head = {0, NULL};
           int x = 1;
           printf("This program will create an odered linked list of numbers greater"
           " than 0 until the user inputs 0 or a negative number.\n");
           while (x > 0){
           printf("Please input a value to store into the list.\n");
           scanf("%d", &x);
                insertNode(&Head, x);
           }
           printf("Program terminated.\n");
           system("PAUSE");
           }
      void insertNode(struct listNode * Head, int x){
           struct listNode *newNode, *current;
           newNode = malloc(sizeof(struct listNode));
           newNode->data = x;
           newNode->next = NULL;
           current = Head;
           while (current->next != NULL && current->data < x) 
           {
              current = current->next;
              }
      
              if(current->next == NULL){
                   current->next = newNode;
              }
              else{
                   newNode->next = current->next;
                   current->next = newNode;
              }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-10-25
        • 2015-01-18
        • 2012-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        相关资源
        最近更新 更多