Created
May 1, 2013 16:47
-
-
Save yinyanghu/5496494 to your computer and use it in GitHub Desktop.
Link List with hack remove function in C by Linus Torvalds. He tells me what the pointer is.
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 <string.h> | |
#include <stdlib.h> | |
struct link_list | |
{ | |
int key; | |
struct link_list *next; | |
}; | |
struct link_list *insert_ll(struct link_list *head, int key) | |
{ | |
struct link_list *ptr = (struct link_list *)malloc(sizeof(struct link_list)); | |
ptr -> key = key; | |
ptr -> next = head; | |
return ptr; | |
} | |
void remove_ll(struct link_list **head, int key) | |
{ | |
struct link_list **cur; | |
for (cur = head; *cur; ) | |
{ | |
struct link_list *entry = *cur; | |
if (entry -> key == key) | |
{ | |
*cur = entry -> next; | |
free(entry); | |
} | |
else | |
cur = &entry -> next; | |
} | |
} | |
void print_ll(struct link_list *head) | |
{ | |
struct link_list *ptr; | |
for (ptr = head; ptr; ptr = ptr -> next) | |
printf("%d ", ptr -> key); | |
printf("\n"); | |
} | |
struct link_list *HEAD; | |
int main() | |
{ | |
HEAD = NULL; | |
HEAD = insert_ll(HEAD, 1); | |
HEAD = insert_ll(HEAD, 3); | |
HEAD = insert_ll(HEAD, 2); | |
HEAD = insert_ll(HEAD, 4); | |
print_ll(HEAD); | |
remove_ll(&HEAD, 3); | |
print_ll(HEAD); | |
remove_ll(&HEAD, 4); | |
print_ll(HEAD); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment