liste
https://telegra.ph/liste-04-09#include <stdio.h>
#include <stdlib.h>
// provate ad aggiungere un terzo elemento
typedef struct node {
int val;
struct node * next;
} node_t;
void print_list(node_t * head) {
node_t * current = head;
while (current != NULL) {
printf("%d\n", current->val);
current = current->next;
}
}
int main(int argc, char *argv[])
{
node_t * head = NULL;
head = malloc(sizeof(node_t));
if (head == NULL)
return 1;
head->val = 1;
head->next = NULL;
head->next = malloc(sizeof(node_t));
head->next->val = 2;
head->next->next = NULL;
print_list(head);
system("PAUSE");
return 0;
}
//------------------------------------- Versione 2
#include <stdio.h>
#include <stdlib.h>
// codice a https://telegra.ph/liste-04-09
typedef struct node {
int val;
struct node * next;
} node_t;
void print_list(node_t * head) {
node_t * current = head;
while (current != NULL) {
printf("%d\n", current->val);
current = current->next;
}
}
int main(int argc, char *argv[])
{
node_t * head = NULL;
node_t * last = NULL;
node_t * temp = NULL;
temp = malloc(sizeof(node_t));
if (temp == NULL)
return 1;
head=temp; //primo inserimento
temp->val = 1;
temp->next = NULL;
last=head; //il primo è anche l'ultimo
temp = malloc(sizeof(node_t)); //heap
temp->val = 2;
temp->next = NULL;
last->next=temp;
last=temp;
temp = malloc(sizeof(node_t)); //heap
temp->val = 200;
temp->next = NULL;
last->next=temp;
last=temp;
temp = malloc(sizeof(node_t)); //heap
temp->val = 1000;
temp->next = NULL;
last->next=temp;
last=temp;
print_list(head);
system("PAUSE");
return 0;
}