2017-02-24 1 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; 
} 

은 내가 q (q는 이전 노드를 가리 키도록 작성)을 만들 때 문제는 여기에 생각합니다.

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)) 

typedef를 사용하여 포인터를 숨기는 것은 나쁜 생각입니다. 여전히 개선의 여지가있다

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 기능이 다소 어색 :

그래서 여기에 프로그램의 수정 된 버전 (간결 ommitted #include들)이다.

또한 약간의 수정