1

我正在尝试创建一个链接列表,该列表将从用户那里获取输入,对其进行排序,并在用户输入 0 或负数后将其打印出来。我的代码在某个地方在打印循环的开头添加了一个“0”。
示例:我输入 1-2-3-4-5。然后程序返回 0-1-2-3-4-5。
示例 2:我输入 1-2-3-4-5。然后程序返回 0-5-1-2-3-4。这对我来说也是一个问题,因为我最终需要让程序将输入的值从最小到最大排序。但是现在我专注于让它接受输入 1-2-3-4-5 并打印 1-2-3-4-5。

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

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

//prototypes
void insertNode(struct listNode *Head, int x);
void printList(struct listNode *Head);
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);
           if (x > 0){
           insertNode(&Head, x);
           }
     }
     printList(&Head);
     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;
        }
}
void printList(struct listNode * Head){
    struct listNode *current = Head;
    while (current != NULL){
          if(current > 0){
               printf("%d \n", *current);
          }
          current = current->next;
    }
}
4

4 回答 4

1

它在列表中有一个零,因为你把它放在那里:

struct listNode Head = {0, NULL};

如果您想要快速修复,请更改输入行printList()以及处理列表的其他任何内容:

struct listNode *current = Head;

到:

struct listNode *current = Head->next;

这将从列表的第二个元素开始,忽略您放在那里的那个开始。


但是,更好的方法可能是根本没有那个无关的元素:

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

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

// Prototypes (freeList removed since not defined).

void insertNode(struct listNode **pHead, int val);
void printList(struct listNode *Head);

// Main program for testing.

int main(void) {
    // List initially empty.

    struct listNode *Head = NULL;

    int x = 1;
    puts("This program will create an ordered linked list");
    puts("    of numbers greater than 0 until the user");
    puts("    enters 0, a negative number, or a non-integer.");
    for(;;) {
          puts("Please input a value to store into the list.");
          if ((scanf("%d", &x) != 1) || (x <= 0)) break;
          insertNode(&Head, x);
     }
     printList(Head);
}

void insertNode(struct listNode **pHead, int val){
    struct listNode *newNode, *current, *previous;

    // Allocate new node, should really check for failure here.

    newNode = malloc (sizeof (struct listNode));
    newNode->data = val;
    newNode->next = NULL;

    // Handle inserting into empty list.

    if (*pHead == NULL) {
        *pHead = newNode;
        return;
    }

    // Find node to insert before.

    current = *pHead;
    while (current != NULL && current->data < val)  {
        previous = current;
        current = current->next;
    }


    // Handle inserting at start of list.

    if (current == *pHead) {
        newNode->next = *pHead;
        *pHead = newNode;
        return;
    }

    // Handle inserting at end of list.

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

    // Handle inserting somewhere inside the list.

    newNode->next = current;
    previous->next = newNode;
}

void printList (struct listNode *Head) {
    struct listNode *current = Head;

    if (current == NULL) {
        puts ("There are no numbers.");
        return;
    }

    puts ("Numbers are:");
    while (current != NULL) {
        printf ("   %d\n", current->data);
        current = current->next;
    }
}

我还清理了其他一些东西,例如更改*current为更显式的current->data,将指针传递给 head 以便您可以更改它,并对主输入循环进行轻微修改。这是一个示例运行:

This program will create an ordered linked list
    of numbers greater than 0 until the user 
    inputs 0 or a negative number.
Please input a value to store into the list.
4
Please input a value to store into the list.
1
Please input a value to store into the list.
8
Please input a value to store into the list.
5
Please input a value to store into the list.
6
Please input a value to store into the list.
3
Please input a value to store into the list.
2
Please input a value to store into the list.
9
Please input a value to store into the list.
7
Please input a value to store into the list.
0
Numbers are:
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
于 2012-11-06T01:57:59.880 回答
0

printList您打印 value*current时,它​​不是整数(它是 a struct listNode)。您的编译器可能会警告您这一点。

尝试打印current->data,而不仅仅是打印,*current事情应该会奏效。

如果这确实是您想要的,您可能还需要更新您的if(current > 0)测试以使其更像。current->data > 0

于 2012-11-06T01:54:51.643 回答
0

您正在打印的 printList() 函数中的第一项是列表的 Head 元素,其中包含零作为数据。你很幸运,因为你的结构的第一个元素是 int 数据,所以当你取消引用指向 current 的指针时,你碰巧在结构的开头得到了 int。

实际上你可能应该像下面这样重写 print 函数:

void printList(struct listNode * Head){
    struct listNode *current = Head->next;
    while (current != NULL){
          printf("%d \n", current->data);
          current = current->next;
    }
}
于 2012-11-06T01:56:52.803 回答
0

... Somewhere my code is adding a "0" to the beginning of the print loop.

是的,在您的代码中,当您第一次 init 时Head,您引入了0. 这是行:

struct listNode Head = {0, NULL};

假设您将上述值从 0 更改为 999,您的代码将打印出 999 作为第一个数字。

您需要在插入期间处理 Head 节点情况。

于 2012-11-06T02:00:01.240 回答