【问题标题】:linked list, getting unwanted values链表,获取不需要的值
【发布时间】:2012-11-06 01:45:20
【问题描述】:

我正在尝试创建一个链接列表,该列表将从用户那里获取输入,对其进行排序,并在用户输入 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;
    }
}

【问题讨论】:

标签: list linked-list


【解决方案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-&gt;data,将 指针 传递给头部,以便您可以更改它,并对主输入循环稍作修改。这是一个示例运行:

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 

【讨论】:

    【解决方案2】:

    printList 中,您正在打印值*current,它不是整数(它是struct listNode)。您的编译器可能会警告您这一点。

    尝试打印current-&gt;data,而不仅仅是*current,一切都会正常。

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

    【讨论】:

    • 非常感谢。尽管您说更改打印语句。我将 if 语句从 *current 更改为 current->data 并且它起作用了。现在我这里没有多余的 0,我需要弄清楚如何修改代码以在输入值时对输入的值进行排序。
    • 是的,但printf 起作用的事实只是一个巧合。真的,你在内存中传递了一个结构,其中包含一个整数,然后是一些其他的东西,它恰好知道如何做正确的事情。如果您也更正了printf 语句,您的编译器将在您编译时停止输出警告。
    【解决方案3】:

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

    其实你可能应该像下面这样重写打印函数:

    void printList(struct listNode * Head){
        struct listNode *current = Head->next;
        while (current != NULL){
              printf("%d \n", current->data);
              current = current->next;
        }
    }
    

    【讨论】:

      【解决方案4】:

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

      是的,在你的代码中,当你第一次初始化Head 时,你引入了0。 这是行:

      struct listNode Head = {0, NULL};
      

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

      插入时需要处理Head节点情况。

      【讨论】:

        猜你喜欢
        • 2013-01-09
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 2020-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多