1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h>
#include <stdlib.h>
typedef struct _Node {
int value;
struct _Node* next; // RIGHT
// Node* next; // WRONG!!! gcc doesn't know what 'Node' means yet.
} Node;
int main(int argc, char* argv[]) { // VERSION 2 - start with empty list
// EMPTY LIST -- just a Node* variable set to NULL
Node* head = NULL; // perfectly valid linked list of size 0
Node* curr = head; // perfectly valid linked list of size 0
// Initialize head
curr = malloc(sizeof(*head));
curr -> value = 2;
// Second node
curr -> next = malloc(sizeof(*curr -> next));
curr = curr -> next; // curr is the address of the 2nd node
curr -> value = 6;
// Third node (tail in this case)
curr -> next = malloc(sizeof(*curr -> next));
curr = curr -> next; // curr is the address of the 3nd node (the tail)
curr -> value = 4;
// IMPORTANT: Terminate your list: tail -> next = NULL;
curr -> next = NULL;
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2017 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.