Skip to content

Instantly share code, notes, and snippets.

@davestevens
Created January 27, 2012 17:05
Show Gist options
  • Save davestevens/1689809 to your computer and use it in GitHub Desktop.
Save davestevens/1689809 to your computer and use it in GitHub Desktop.
Simple linked list implementation in C.
#include <stdio.h>
#include <stdlib.h>
typedef struct elementT {
unsigned value;
struct elementT *next;
} elementT;
elementT *removeElement(elementT *, unsigned);
elementT *addElement(elementT *, unsigned);
void printList(elementT *);
int main(void) {
elementT *list = NULL;
#if TEST
printf("Setup some elements\n");
list = addElement(list, 0xdeadbeef);
list = addElement(list, 0xcafef00d);
list = addElement(list, 0x11111111);
list = addElement(list, 0x00000000);
list = addElement(list, 0xfeedface);
printf("List:\n");
printList(list);
printf("\n");
printf("Remove some elements\n");
printf("0xfeedface\n");
list = removeElement(list, 0xfeedface);
printf("List:\n");
printList(list);
printf("\n");
printf("0x11111111\n");
list = removeElement(list, 0x11111111);
printf("List:\n");
printList(list);
printf("\n");
printf("0xdeadbeef\n");
list = removeElement(list, 0xdeadbeef);
printf("List:\n");
printList(list);
printf("\n");
#endif
return 0;
}
elementT *removeElement(elementT *l, unsigned v) {
if(l == NULL) {
/* you're trying to remove something from an empty list */
return l;
}
else {
elementT *t = l;
elementT *p = NULL;
do {
if(t->value == v) {
if(t != l) {
p->next = t->next;
free(t);
return l;
}
else {
elementT *u = t->next;
free(t);
return u;
}
}
p = t;
t = t->next;
} while(t != NULL);
}
/* couldn't find it */
return l;
}
elementT *addElement(elementT *l, unsigned v) {
elementT *t = (elementT *)calloc(sizeof(elementT), 1);
t->value = v;
t->next = NULL;
if(l == NULL) {
return t;
}
else {
elementT *u = l;
while(u->next != NULL)
u = u->next;
u->next = t;
return l;
}
}
void printList(elementT *l) {
for(;l != NULL;l = l->next) {
printf("%p: 0x%08x\n", (void *)l, l->value);
}
}
@davestevens
Copy link
Author

i've used calloc to make sure that the pointer it NULL on creation. i'm sure theres some weird different between mac and linux with malloc.
also, if i don't cast i get compiler warnings (-Wall -Wextra -pedantic) about types, technically next is (struct elementT *) ;)

@iamscottmoyers
Copy link

You assign the next pointer to NULL 2 lines down. If you have c++-compat on it will complain if you do:
something = malloc(); so you should use something = (elementT *) malloc();

But no gcc switches should complain about the casting ..... ooooh you've got an un-named struct:
typedef struct { } elementT;
should be:
typedef struct elementT { } elementT;

@iamscottmoyers
Copy link

void printList(elementT *l) {
for(;l != NULL;l = l->next) {
printf("%p: 0x%08x\n", (void *)l, l->value);
}
}
}

@iamscottmoyers
Copy link

The last return in removeElement should probably be return NULL; otherwise you don't know the difference between a match and a failure.

@davestevens
Copy link
Author

cheers moyers.
the last point shouldn't matter for what i need it for, however, if i were to do that i'd lose the whole list?

@iamscottmoyers
Copy link

Yeaaah you would. Usually with this kind of list you pass in an "elementT **list" to all your functions so that the list pointer can be modified.

Actually if you don't find the element you're searching for then you can guarantee that your input list was not modified so you can safely use the list pointer you passed in.

@davestevens
Copy link
Author

davestevens commented Jan 27, 2012 via email

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