Linked List - append and push

Asked

Viewed 91 times

2

I’m trying to learn Linked List and I 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 he’s returning:

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

The Valor: 0 I wasn’t supposed to be showing up... Somebody give a hand

1 answer

2


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

node * lista = new node();

Who happened to have the data to 0 and the next to 0 (NULL) also, otherwise it wouldn’t even work. The correct boot will then:

node * lista = NULL;

In addition the append function has an incorrect block:

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

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

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 on append is done before the knot was created, which before was not and so had a memory leak.

The print also has a memory leak:

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

Here a knot was created and its memory address was stored in the pointer temp, which was soon changed to point to the node head. Soon the initially created node was lost in memory. What is intended is to create only the pointer and point it directly to head, in order to be able to navigate:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.