订购链表打印

时间:2012-11-05 19:31:45

标签: c

我正在做我的家庭作业,这就是我已经得到的。我现在需要知道如何打印出已列入列表的信息。我需要重新配置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;
        }
}

4 个答案:

答案 0 :(得分:3)

启动时

Header->next为NULL,当您添加元素时,将当前值更改为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;

答案 1 :(得分:0)

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

您尝试访问current-&gt;而不是每次都分配它。并且您实际上并没有在链接列表中搜索正确的位置(从您的问题来看,它听起来应该被排序)。

尝试类似:

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

答案 2 :(得分:0)

你的impl有三个问题:

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

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

    电流=报头 - &gt;接着, //节点Header-&gt; next当前指向

    报头 - &gt;接着= newNode; //让header-&gt;接下来指向新的ele

    newNode-&gt;接着=电流; //让新ele的下一个指向旧的top ele

  3. header-node在某种程度上是特殊和无用的,你应该将一个空列表作为特殊情况处理,并且header应该最初为0,insertnode的工作方式如下:

    if(header == 0)

     header=newNode
    

    否则

    //做像2中所写的东西。

  4. 所有这些肯定可以用不同的方式完成,但这是该列表问题的一个常见方法...... (嗯,不知何故,代码的4个主要空白不起作用?)

答案 3 :(得分:0)

让它工作,只需要弄清楚如何制作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;
        }
}