Skip to content

Instantly share code, notes, and snippets.

@hosaka
Created August 10, 2015 16:34
Show Gist options
  • Save hosaka/9bb720e1cdc1d4d789b1 to your computer and use it in GitHub Desktop.
Save hosaka/9bb720e1cdc1d4d789b1 to your computer and use it in GitHub Desktop.
Most simple linked list implementation in C
#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;
}
@rpappalax
Copy link

Oh this is great!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment