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 | #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 1
// Initialize head
Node* head = malloc(sizeof(*head));
head -> value = 2;
Node* curr = head; // curr is the address of the 1st node
// 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.