Created
January 1, 2017 13:09
-
-
Save chris-marsh/4d323e6e92d72460f2309cd133e80620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Linked list example */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct Node { | |
int data; | |
struct Node *next; | |
} Node; | |
void add_node(Node *node, int data) { | |
/* Traverse to last node */ | |
if (node != 0) { | |
while (node->next != 0) { | |
node = node->next; | |
} | |
} | |
/* Allocate memory space for the new node */ | |
node->next = malloc(sizeof(Node)); | |
node = node->next; | |
node->next = 0; | |
node->data = data; | |
} | |
int main(void) { | |
Node *head; | |
head = malloc(sizeof(Node)); | |
head->next = 0; | |
head->data = 12; | |
add_node(head, 24); | |
add_node(head, 36); | |
Node *node; | |
node = head; | |
while (node !=0) { | |
printf("%d\n", node->data); | |
node = node->next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment