Created
August 10, 2015 16:34
-
-
Save hosaka/9bb720e1cdc1d4d789b1 to your computer and use it in GitHub Desktop.
Most simple linked list implementation in C
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
// list node | |
typedef struct node_s | |
{ | |
int data; | |
struct node_s *next; | |
struct node_s *prev; | |
} node_t; | |
void list_print(node_t *head) | |
{ | |
node_t *curr = head; | |
while (curr != NULL) | |
{ | |
printf("%d\n", curr->data); | |
curr = curr->next; | |
} | |
} | |
// add an element to the end of the list | |
void list_append (int data, node_t *head) | |
{ | |
// rewind to head | |
node_t *curr = head; | |
while (curr->next != NULL) | |
{ | |
curr = curr->next; | |
} | |
curr->next = malloc( sizeof(node_t) ); | |
curr->next->data = data; | |
curr->next->next = NULL; | |
} | |
int main (int argc, char *argv) | |
{ | |
node_t *head = NULL; | |
// create new node | |
head = malloc( sizeof(node_t) ); | |
head->data = 0; | |
head->next = NULL; | |
list_append(30, head); | |
list_append(354, head); | |
list_print(head); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh this is great!!