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