Linked List - append and push

2

I'm trying to learn Linked List and made the following code:

#include <iostream>

using namespace std;

typedef struct snode {
    int data;
    snode *next;
} node;

void start(node ** head) {

    (*head)->next = NULL;
}

void push(node **head, int val) {
    node *no = new node();
    no->data = val;
    no->next = *head;
    *head = no;
}
void append(node **head, int val) {
    node *newnode = new node();
    node *last = *head;

    newnode->data = val;
    newnode->next = NULL;

    if ((*head)->next == NULL ) {
        (*head) = newnode;
        return;
    }
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = newnode;
    return;
}
void print(node *head) {
    node *temp = new node();
    temp = head;
    if (temp->next == NULL) {
        cout << "Node Vazio\n"; return;
    }
    while (temp != NULL) {
        cout << "Valor: " << temp->data << endl;
        temp = temp->next;
    }
}
int main() {
    node * lista = new node();
    //start(&lista);
    //append(&lista, 2);
    push(&lista, 8);
    append(&lista, 5);
    push(&lista, 10);
    append(&lista, 200);
    append(&lista, 2213);
    print(lista);

    getchar();

    return 1;
}

The problem is what it is returning:

Valor: 10
Valor: 8
Valor: 0
Valor: 5
Valor: 200
Valor: 2213

Valor: 0 was not supposed to be appearing ... Someone gives a help

    
asked by anonymous 12.07.2017 / 02:43

1 answer

2

The problem is that the list is starting with a node, which has not been initialized to data or next :

node * lista = new node();

That you happened to have data a 0 and next a 0 ( NULL ) too, otherwise it would not work. The correct boot will then be:

node * lista = NULL;

In addition, the append function has an incorrect block:

if ((*head)->next == NULL ) {
    (*head) = newnode;
    return;
}

That says that if the list only has 1 element then it becomes the new node. That makes you lose the knot you already had! And removing this if becomes functional. We can still test if the list is empty in this function that I think was the goal of if previous, doing:

void append(node **head, int val) {
    if (head == NULL) {
        push(head, val); //se está vazia usa a função de adição à cabeça, que lida bem com NULL
        return;
    }

    node *newnode = new node();
    node *last = *head;

    newnode->data = val;
    newnode->next = NULL;

    while (last->next != NULL) {
        last = last->next;
    }

    last->next = newnode;
}

It is important to note that the empty list test in append is done before creating the node, which was not previously and therefore had a memory leak.

print also has a memory leak:

void print(node *head) {
    node *temp = new node();
    temp = head;
    ...

A node was created here and its memory address was stored in the temp pointer, which was then changed to point to the head node. Soon the node initially created was lost in memory. What you want to do is to create the pointer only and to point it directly to head , to be able to navigate later:

void print(node *head) {
    node *temp = head;
    ...
    
12.07.2017 / 03:26