#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value; // 4 bytes
struct node *next; // 8 bytes
} node_t;
node_t *new_node(int v);
int main(int argc, char *argv[])
{
node_t *head = NULL;
int x;
// Keep reading integers, insert them into linked list
while(scanf("%d", &x) == 1) {
node_t *tmp = new_node(x);
if(head == NULL) {
// case 1
head = tmp;
} else {
// case 2
tmp->next = head;
head = tmp;
}
}
// Print the list
for(node_t *curr = head; curr != NULL; curr = curr->next) {
printf("Node address: %p\n", curr);
printf(" Node value: %d\n", curr->value);
printf("Next address: %p\n\n", curr->next);
}
return 0;
}
// Creates and returns a new node with value v
node_t *new_node(int v)
{
node_t *n = NULL;
n = (node_t *)malloc(sizeof(node_t));
if(n == NULL) {
fprintf(stderr, "Error: malloc failed.\n");
return NULL;
}
// Set the value and next variables of n
n->value = v;
n->next = NULL;
return n;
}