unexpected output

Hi,

I'm new here and I have one doubt related to the language C. I'm learning it and trying to write a code that represents a linked list. I have a struct called No that represents each item of the list. Actually I create one pointer to point to each No. So I tried to create also **primeiroNoDaLista and **ultimoNoDaLista which are the pointer that points to the pointer that points to the first item(No) of the list and the other the pointer that points to the pointer that points to the last item(No) of the list respectively. The idea is that if I know the first and the last item I can add one new item at the end of the list without pass through the list. The problem in my code bellow is that I inserted just one item in the list which is the number one, but in the output shows number 3 as the element in the list. Why does it happen?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>  
#include<stdlib.h>

struct No{
       int dado;
       struct No *proximo;
};

typedef struct No No;
No **primeiroNoDaLista = NULL;
No **ultimoNoDaLista = NULL;

void mostrarDadosDosNos(){
     No *noTemporario = *primeiroNoDaLista;
     
     while(noTemporario != NULL){
          printf("%d", noTemporario->dado);                        
          noTemporario = noTemporario->proximo;
          printf("\n");
     }
     getch();
}

int listaIsEmpty(){
     if(primeiroNoDaLista){
          return 0; //that's null
     }else{
          return 1; //is null
     }       
}

void inserirNovoNo(int dado){
     No novoNo;
     novoNo.dado = dado;
     novoNo.proximo = NULL;

     if(listaIsEmpty() == 1){
          No *ponteiroNovoNo = &novoNo;
          primeiroNoDaLista = &ponteiroNovoNo;   
          ultimoNoDaLista = &ponteiroNovoNo;
     }else{
          //I'll implement in order to add items in the end of the list if the list is not empty
     }    
}

void criarEPopularListaLigada(){
     inserirNovoNo(1);
     inserirNovoNo(2);
     inserirNovoNo(3);
}

main(){
     criarEPopularListaLigada();
     mostrarDadosDosNos();
}


Sorry if I didn't explain it in a clear way. If you don't understand I'll try to explain it better.
Last edited on
1. Use plain pointers to No's instead of **.

2. novoNo is local variable. It expires when you leave the scope of that function. You must use dynamically allocated memory. See malloc and free.
Hi keskiverto, Thank you for your response. I tried to rewrite the code in another way following what you said:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>

struct no{
       int dado;
       struct no *proximo;       
};

typedef struct no No;
typedef No *PonteiroParaNo;

void inserirNovoNo(PonteiroParaNo *ponteiroNoInicial, int dado);
int isEmpty(PonteiroParaNo ponteiroNoInicial);
void mostrarDadosDosNos(PonteiroParaNo ponteiroNoInicial);

int main(){
    PonteiroParaNo ponteiroNoInicial = NULL;
    inserirNovoNo(&ponteiroNoInicial, 1);
    mostrarDadosDosNos(ponteiroNoInicial);
    
    return 0;   
}

void inserirNovoNo(PonteiroParaNo *ponteiroNoInicial, int dado){
     PonteiroParaNo novoNo = malloc(sizeof(No));
     
     if(novoNo != NULL){
//          if(isEmpty(ponteiroNoInicial)){
               novoNo->dado = dado;
               novoNo->proximo = NULL;
               *ponteiroNoInicial = novoNo;
  //        }
     }else{
          printf("Nó não foi inserido. Não há memória disponível.");      
     }
}

int isEmpty(PonteiroParaNo ponteiroNoInicial){
    return ponteiroNoInicial == NULL;
}

void mostrarDadosDosNos(PonteiroParaNo ponteiroNoInicial){
     while(ponteiroNoInicial != NULL){
          printf("%c --> ", ponteiroNoInicial->dado);
          ponteiroNoInicial = ponteiroNoInicial->proximo;
     }
     printf("NULL");
     getch();
}


I have 2 problems now.

1-the output doesn't show the 1 --> NULL, it shows an smile instead of the number 1:
☺ --> NULL

2-If I uncomment the lines that are commented I see NULL instead of
1 --> NULL

Can you help me explaning what's wrong with my code and maybe giving tips for me to finish the insert method(I called it inserirNovoNo). I want to insert number always at the end of the list.
Line 44. printf format codes. "%c" is not signed int, but unsigned char. Replace it with "%d".

Line 30: You seem to insert to the front of the list. However, you discard the existing list. Try
novoNo->proximo = *ponteiroNoInicial;
* If list is empty, then *ponteiroNoInicial is NULL and you get same result as in your code.
* If list is not empty, then the proximo of the new head node will point to the previous head node.

Consider adding a deleteList() method. You now allocate from heap with malloc, and you should release that memory in the end of the program with free.
Hey keskiverto, thank you very much.

I wrote the code again in another way. Now it's working. Here is the code to help others:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>

struct no{
       int dado;
       struct no *proximo;       
};

typedef struct no No;

int isEmpty(No *ponteiroPrimeiroNo){
     return (ponteiroPrimeiroNo == NULL);
}

void inserirNovoNo(No **ptrParaPtrPrimeiroNo, No **ptrParaPtrUltimoNo, int dado){
     No *novoNo = (No*) malloc(sizeof(No));
     novoNo->dado = dado;
     
     if(isEmpty(*ptrParaPtrPrimeiroNo)){
          novoNo->proximo = *ptrParaPtrPrimeiroNo;
          *ptrParaPtrPrimeiroNo = novoNo;
     }else{
          novoNo->proximo = NULL;
          (*ptrParaPtrUltimoNo)->proximo = novoNo;
     }
     
     *ptrParaPtrUltimoNo = novoNo;
}

void mostrarDadosDosNos(No *ponteiroPrimeiroNo){
     No *temp;
     
     printf("Nos: ");
     for(temp = ponteiroPrimeiroNo; temp != NULL; temp = temp->proximo){
          printf("%d -> ", temp->dado);         
     }     
     printf("NULL");
}

int main(void){
    No *ponteiroPrimeiroNo = NULL;
    No *ponteiroUltimoNo = NULL;
    
    inserirNovoNo(&ponteiroPrimeiroNo, &ponteiroUltimoNo, 1);
    inserirNovoNo(&ponteiroPrimeiroNo, &ponteiroUltimoNo, 2);
    inserirNovoNo(&ponteiroPrimeiroNo, &ponteiroUltimoNo, 3);

    mostrarDadosDosNos(ponteiroPrimeiroNo);

    getch();

    return 0;    
}
Topic archived. No new replies allowed.