Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Created February 11, 2011 06:32
Show Gist options
  • Save ecounysis/822004 to your computer and use it in GitHub Desktop.
Save ecounysis/822004 to your computer and use it in GitHub Desktop.
My first attempt at building a linked list in C that actually compiles and works
#include <stdlib.h>
#include <stdio.h>
#include "ll.h"
LL *createlist(int item)
{
LL *it = malloc(sizeof(LL));
it->val = item;
printf("creating new LL@%p whose value is %d\n", it, item);
return it;
}
int push(LL **thelist, int item)
{
printf("pushing %d onto front of %p\n", item, *thelist);
LL *it = createlist(item);
printf("new address should be %p\n", it);
LL *temp = *thelist;
*thelist = it;
it->next = temp;
printf("new address is %p\n", *thelist);
return 1;
}
int main()
{
LL *it = createlist(500);
printf("*it address: %p\n", it);
printf("it->val: %d\n", it->val);
push(&it, 600);
printf("*it address: %p\n", it);
printf("it->val: %d\n", it->val);
printf("it->next->val %d\n", it->next->val);
return 0;
}
typedef struct listnode
{
int val;
struct listnode *next;
} LL;
/*
int append(LL*, int);
int getval(LL*, int);
LL *createlist(int);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment