1

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

4 回答 4

3

Header->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;
于 2012-11-05T19:38:25.977 回答
0
 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;
}
于 2012-11-05T19:45:08.673 回答
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;
        }
}
于 2012-11-05T21:00:54.990 回答
0

There are three issues in your impl:

  1. you should append new numbers at the end, so you should have two pointers into the list: one points to the head, another one to the tail (you could also add at the top of list, then you need only one pointer, but then the list is reverse ordered)

  2. the pointer-chaining code is wrong, i think you want to try to insert new elements after your head-node, so your code should be written like:

    current=Header->next; // the node Header->next is currently pointing to

    Header->next=newNode; // let header->next point to new ele

    newNode->next=current; // let new ele's next point to old top ele

  3. the header-node is somehow special and useless, you should handle an empty list as a special case, and header should be initially 0, and insertnode would work like this:

    if (header==0)

     header=newNode
    

    else

    // do the stuff like written in 2.

All this can for sure done in a different way, but this is one common aproach to that list-problem... (Hmm, somehow the 4 leading blanks for code dont work?)

于 2012-11-05T19:46:41.447 回答