Skip to content

Instantly share code, notes, and snippets.

@limitedmage
Created September 30, 2010 02:25
Show Gist options
  • Save limitedmage/603920 to your computer and use it in GitHub Desktop.
Save limitedmage/603920 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int value;
struct _node *next;
} node;
void CheckNotNull(void *pointer);
node *BuildOneTwoThree();
void PrintList(node *head);
int main() {
node *list = BuildOneTwoThree();
PrintList(list);
return 0;
}
node *BuildOneTwoThree() {
node *head;
head = (node *) malloc(sizeof(node));
CheckNotNull(head);
head->value = 1;
head->next = (node *) malloc(sizeof(node));
CheckNotNull(head->next);
head->next->value = 2;
head->next->next = (node *) malloc(sizeof(node));
CheckNotNull(head->next->next);
head->next->next->value = 3;
head->next->next->next = NULL;
return head;
}
void PrintList(node *head) {
node *curr = head;
while (curr != NULL) {
printf("%d ", curr->value);
curr = curr->next;
}
printf("\n");
}
void CheckNotNull(void *pointer) {
if (pointer == NULL) {
printf("Error: pointer is null.\n");
exit(-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment