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;

これは、リストの 2 番目の要素から開始され、最初にそこに配置した要素は無視されます。


ただし、おそらくより良い方法は、無関係な要素をまったく持たないことです。

#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を渡して変更できるようにする、メインの入力ループをわずかに変更するなど、他にもクリーンアップしたことがいくつかあります。実行例は次のとおりです。

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あなたは*current整数ではない値を出力しています(それはです)struct listNode。あなたのコンパイラはおそらくこれについてあなたに警告しました。

current->dataだけでなく、 を印刷してみてください*current。うまくいくはずです。

それが本当に必要な場合は、if(current > 0)テストをより似たものに更新する必要があるかもしれません。current->data > 0

于 2012-11-06T01:54:51.643 に答える
0

印刷する printList() 関数の最初の項目は、データとしてゼロを含むリストの Head 要素です。構造体の最初の要素は int データであるため、幸運です。現在のポインターを逆参照すると、構造体の先頭で 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 したときの行でHead0. これは行です:

struct listNode Head = {0, NULL};

上記の値を 0 から 999 に変更すると、コードは最初の数値として 999 を出力するとします。

挿入中にヘッド ノードのケースを処理する必要があります。

于 2012-11-06T02:00:01.240 に答える