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