Created
          January 27, 2012 17:05 
        
      - 
      
- 
        Save davestevens/1689809 to your computer and use it in GitHub Desktop. 
    Simple linked list implementation in C.
  
        
  
    
      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 <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); | |
| } | |
| } | 
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?
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.
ahh, pointer pointer is how it would be do, the reason i'm returning is because i'm an idiot and forgot about that. sending a null pointer to the newElement function doesn't do anything when you try to create a new element and set the pointer. because its the functions scope which gets set rather than whats passed in.
meh, this works for what i need it to (i think). i just need to modify the simulator code. still have a weird unknown segfault under certain circumstances on 32bit :S
…On 27 Jan 2012, at 19:30, iamscottmoyers wrote:
 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.
 ---
 Reply to this email directly or view it on GitHub:
 https://gist.github.com/1689809
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
The last return in removeElement should probably be return NULL; otherwise you don't know the difference between a match and a failure.