Title / Description
Code #include <stdio.h> #include <stdlib.h> typedef struct l1 { struct l1* next; int key; } l1; l1* head = NULL; l1* insert_head(int key) { l1* p; p = (l1*) malloc(sizeof(struct l1)); if(p == NULL) { printf("No memory\n"); return NULL; } p->key = key; p->next = head; head = p; return p; } l1* insert_tail(int key) { l1** tail; l1* p; tail = &head; p = (l1*) malloc(sizeof(struct l1)); if(p == NULL) { printf("No memory\n"); return NULL; } p->key = key; p->next = NULL; while(*tail != NULL) tail = &((*tail)->next); *tail = p; return p; } void remove_item(int key) { l1** parent; l1* p; p = head; parent = &head; while(p != NULL) { if(p->key == key) { *parent = p->next; free(p); p = *parent; } else { parent = &(p->next); p = p->next; } } } void print(void) { l1* p; p = head; if(p != NULL) { for(; p!=NULL; p = p->next) { printf("%d ", p->key); } } else { printf("L1 is empty!"); } printf("\n"); } int main(int argc, char** argv) { print(); insert_tail(1); insert_tail(1); insert_tail(1); insert_head(100); insert_tail(1); insert_tail(2); insert_tail(1); print(); remove_item(1); print(); }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code