2017-02-24 151 views
-5

我不知道爲什麼這不起作用。鏈接列表指針prob

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

// struct of list 
typedef struct noeud 
{ 
    int adresse, taille, temp; 
    struct noeud* suivant; 

} * liste; 

int random(int a, int b) 
{ 
    return (a + (rand() % ((b + 1) + a))); 
} 

void initialisation(liste* LBO) 
{ 
    *LBO = NULL; 
} 

我認爲這是這裏的問題,當我創建qq創建指向前一個節點)。

void creation(liste* LBO) 
{ 
    liste q, prec = NULL; 
    int i = 0; 
    srand(time(NULL)); 
    while (i < 3) 
    { 
     printf("%d", i); 
     q = malloc(sizeof(liste)); 

     if (*LBO == NULL) 
     { 
      q->adresse = 0; 
      q->taille = random(5, 45); 
      q->temp = random(5, 15); 
      q->suivant = *LBO; 
      *LBO = q; 
      i++; 
     } 
     else 
     { 
      prec = *LBO; 
      q->taille = random(5, 45); 
      q->temp = random(5, 15); 
      q->adresse = prec->adresse + prec->taille; 
      q->suivant = *LBO; 
      *LBO = q; 
      i++; 
     } 
    } 
} 

void affichage(liste LBO) 
{ 
    printf("\nvoici ta liste \n "); 
    while (LBO != NULL) 
    { 
     printf("%d-->", LBO->taille); 
     LBO = LBO->suivant; 
    } 
    if (LBO == NULL) 
     printf("NULL"); 
} 

int main() 
{ 
    // or here 
    printf("Hello world!\n"); 
    liste LBO; 
    initialisation(&LBO); 

    creation(&LBO); 

    affichage(LBO); 
    return 0; 
} 
+6

這需要一些嚴重的編輯。我試過了,但放棄了。 – unwind

+0

嘗試使用調試器來查找導致問題的原因。爲了解決這個問題,你首先需要知道它是什麼 – Yousaf

+0

縮進(格式化)你的代碼,就像C教科書中的樣本一樣。 –

回答

1

有幾個問題:

調用

initialisation(&LBO); 

這是不是真的錯了相反的,只寫:

LBO = NULL; 

那就不要隱瞞指針與typedef這隻會增加混淆。

相反的:

typedef struct noeud 
{ 
    int adresse, taille, temp; 
    struct noeud* suivant; 

} *liste; 

寫:

struct noeud 
{ 
    int adresse, taille, temp; 
    struct noeud* suivant;  
}; 

,並使用struct noeud*,而不是liste

現在真正的問題:

這是不對的。在這裏,您分配到指針的大小,但你需要分配的大小爲整個結構:

q = malloc(sizeof(liste)); 

這實際上是一樣的:

q = malloc(sizeof(struct noeud*)) 

,但你需要:

q = malloc(sizeof(struct noeud)) 

你現在看到爲什麼使用typedefs隱藏指針是一個壞主意。

因此,這裏是你的程序的修正版本(#include小號ommitted爲了簡潔):

struct noeud 
{ 
    int adresse, taille, temp; 
    struct noeud* suivant; 
}; 

int random(int a, int b) 
{ 
    return (a + (rand() % ((b + 1) + a))); 
} 

void creation(struct noeud** LBO) 
{ 
    struct noeud* q, *prec = NULL; 
    int i = 0; 
    // srand(time(NULL)); <<<<< don't call srand here, call it once at the 
          // beginning of the program 
    while (i < 3) 
    { 
    printf("%d", i); 
    q = malloc(sizeof(struct noeud)); 

    if (*LBO == NULL) 
    { 
     q->adresse = 0; 
     q->taille = random(5, 45); 
     q->temp = random(5, 15); 
     q->suivant = *LBO; 
     *LBO = q; 
     i++; 
    } 
    else 
    { 
     prec = *LBO; 
     q->taille = random(5, 45); 
     q->temp = random(5, 15); 
     q->adresse = prec->adresse + prec->taille; 
     q->suivant = *LBO; 
     *LBO = q; 
     i++; 
    } 
    } 
} 

void affichage(struct noeud* LBO) 
{ 
    printf("\nvoici ta struct noeud* \n "); 
    while (LBO != NULL) 
    { 
    printf("%d-->", LBO->taille); 
    LBO = LBO->suivant; 
    } 
    // if (LBO == NULL) <<<<<<<<<<< drop this, LBO is always NULL here 
           // but it doesn't hurt, it's just useless 
    printf("NULL"); 
} 

int main() 
{ 
    srand(time(NULL)); // <<<<<<<<<<<<< call srand here 
    struct noeud* LBO; 
    LBO = NULL; 

    creation(&LBO); 

    affichage(LBO); 
    return 0; 
} 

仍然有改進的餘地,尤其是creation功能是有點尷尬。

也看一下意見與<<<<<<<<<<<,也有少量修正