-3
#include<stdio.h>
#include<malloc.h>

typedef struct node_t {
    int i;
    struct node_t* link;
} node;

node* head = NULL;

int main() {
    int i = 10;
    node* temp = NULL;
    head = (node *)malloc(sizeof(node));
    temp = (node *)malloc(sizeof(node));
    if(temp == NULL) {
        printf("\n malloc for temp node failed! \n");
    }
    else {
        /* linked list logic to add the elements in the beginning */
        while(i<=10) {
            temp->i = i;
            temp->link = NULL;
            if(head == NULL) {
                head = temp;
            }
            else {
                temp->link = head;
                head = temp;
            }
            i++;
        }
    }
    for(temp = head; temp->link != NULL; temp = temp->link) {
        printf("\n The data is:%d \n",temp->i);
    }
    free(temp);
    free(head);
    return 0;
}

私は単純なリンクリストプログラムを試しています。出力が得られません。

4

5 に答える 5

2

あなたは無限ループを持っているようです!(iの値は変更されません)

于 2012-11-29T07:49:01.870 に答える
2

tmp1)に値を割り当てるたびにノード ( ) を割り当てる必要がありますtmp。を一度だけ割り当てないでtmpください。その方法については、次の修正コードを参照してください。

2) 次のforループは間違っています:

for(temp = head; temp->link != NULL; temp = temp->link) {

このforループは、次のコードで修正されています

3)freeリンクされたリスト全体を参照してから、各ノードを解放する必要があります。次の固定コードを参照してください。

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

typedef struct node_t{
    int i;
     struct node_t* link;
}node;

node* head = NULL;

int main(){
     int i = 1;
     node* temp = NULL;

     /* linked list logic to add the elements in the beginning */
     while(i<=10){
         temp = (node *)malloc(sizeof(node));
         if(temp == NULL){
             printf("\n malloc for temp node failed! \n");
             exit(1);
         }
         temp->i = i;
         temp->link = NULL;
         if(head == NULL){
             head = temp;
         }
         else{
             temp->link = head;
             head = temp;
         }
         i++;
     }

     for(temp = head; temp != NULL; temp = temp->link){
         printf("\n The data is:%d \n",temp->i);
     }

     while (head!=NULL)
     {
        temp = head->link;
        free(head);
        head = temp;
     }

}
于 2012-11-29T08:03:18.013 に答える
1

whileループで変数の値を変更していないiため、whileループから抜け出すことはありません。

次のようなものが必要です。

int i=1;
while(i<=10){
  // insert i in loop
  i++;
}
于 2012-11-29T07:49:05.440 に答える
1

ループ変数の値を変更していませんi

mallocまた、個別のノードを作成するには、whileループ内で実行する必要があります。現在、コードは同じノードを何度も変更しています。

于 2012-11-29T07:49:52.150 に答える
1

無限ループに関するノードに加えて、 の値を決して変更しないため、i他に何か問題があります。

 22         /* linked list logic to add the elements in the beginning */
 23         while(i<=10){
 24             temp->i = i;
 25             temp->link = NULL;
 26             if(head == NULL){
 27                 head = temp;
 28             }
 29             else{
 30                 temp->link = head;
 31                 head = temp;

このループで何をしているのか見てください。head が NULL の場合 (割り当てが失敗する可能性はありますが、15 行目に戻って割り当てたので、その可能性はほとんどありません)、'head' を temp に設定します。

head が NULL でない場合は、temp の「リンク」を head に設定します。次に、頭を温度に設定します。次に、ループしてもう一度やり直します。

したがって、head が temp を指し、temp->link が temp を指すことになります... 正確に 1 つのノードの循環リストになります。

代わりにこれを試してください:

int main()
{   
    int i = 0;
    node *temp;

    /* linked list logic to add the elements in the beginning */
    while(i != 10) 
    {
        /* First, allocate a new node */
        temp = (node *)malloc(sizeof(node));

        if(temp == NULL) 
            return -1; /* yikes */

        /* now set its value */
        temp->i = i++;

        /* and link it into the list, at the beginning */
        temp->link = head;
        head = temp;        
     }

     /* Now traverse the list, starting from 'head' */
     temp = head;

     while(temp != NULL)
     {
        /* save the current node in a temporary variable */
        node *temp2 = temp;

        /* and move 'temp' to point to the next node in the list */
        temp = temp->link;

        /* print the current node */
        printf("\n The data is: %d\n", temp2->i);

        /* and free the memory */
        free(temp2);
     }   

     return 0;
}
于 2012-11-29T08:03:08.637 に答える