订购链表打印

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

#include  #include  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 next; } if(current->next == NULL){ current->next = newNode; } else{ newNode->next = current->next; current->next = newNode; } } 

启动时, 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; 
  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; } 

你的impl有三个问题:

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

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

    电流=报头 – >下; //节点Header-> next当前指向

    报头 – >下一= newNode; //让header-> next指向新的ele

    newNode->下一=电流; //让新的ele下一个指向旧的顶级元素

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

    if(header == 0)

      header=newNode 

    其他

    //做2写的东西。

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

让它工作,只需要弄清楚如何制作printList和freeList函数

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