-
-
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
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
#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; | |
} |
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
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