Created
August 19, 2012 00:05
-
-
Save mingtsay/3390470 to your computer and use it in GitHub Desktop.
This file contains 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> | |
struct Data | |
{ | |
int value; | |
struct Data *next; | |
}; | |
struct LinkList | |
{ | |
struct Data *head; | |
}; | |
void list_init(struct LinkList *list) | |
{ | |
list->head = NULL; | |
} | |
struct Data *list_data_alloc(int value) | |
{ | |
struct Data *data = (struct Data *)malloc(sizeof(struct Data)); | |
data->value = value; | |
data->next = NULL; | |
return data; | |
} | |
void list_insert_inner(struct Data **head, struct Data *previous, struct Data *data, int value) | |
{ | |
struct Data *newdata; | |
if(data == NULL) | |
{ | |
previous->next = list_data_alloc(value); | |
} | |
else if(data->value > value) | |
{ | |
newdata = list_data_alloc(value); | |
newdata->next = data; | |
if(previous == NULL) | |
{ | |
*head = newdata; | |
} | |
else | |
{ | |
previous->next = newdata; | |
} | |
} | |
else | |
{ | |
list_insert_inner(head, data, data->next, value); | |
} | |
} | |
void list_insert(struct LinkList *list, int value) | |
{ | |
struct Data *data; | |
if(list->head == NULL) | |
{ | |
list->head = list_data_alloc(value); | |
} | |
else | |
{ | |
list_insert_inner(&(list->head), NULL, list->head, value); | |
} | |
} | |
void list_search_inner(struct Data *data, int value, int *sum) | |
{ | |
if(data == NULL || data->value > value) return; | |
if(data->value == value) | |
{ | |
++(*sum); | |
} | |
list_search_inner(data->next, value, sum); | |
} | |
int list_search(struct LinkList *list, int value) | |
{ | |
int sum = 0; | |
list_search_inner(list->head, value, &sum); | |
return sum; | |
} | |
void list_remove_inner(struct Data **head, struct Data *previous, struct Data *data, int value, int single) | |
{ | |
struct Data *next, *deletion; | |
if(data == NULL) return; | |
if(data->value == value) | |
{ | |
if(single) | |
{ | |
next = data->next; | |
} | |
else | |
{ | |
next = data; | |
while(next != NULL && next->value == value) | |
{ | |
next = next->next; | |
} | |
} | |
if(previous == NULL) | |
{ | |
*head = next; | |
} | |
else | |
{ | |
previous->next = next; | |
} | |
while(data != next) | |
{ | |
deletion = data; | |
data = data->next; | |
free(deletion); | |
} | |
} | |
else if(data->value < value) | |
{ | |
list_remove_inner(head, data, data->next, value, single); | |
} | |
} | |
void list_remove(struct LinkList *list, int value, int single) | |
{ | |
struct Data *data; | |
if(list->head != NULL) | |
{ | |
list_remove_inner(&(list->head), NULL, list->head, value, single); | |
} | |
} | |
void list_remove_all(struct LinkList *list, int value) // Alias for list_remove(single = 0); | |
{ | |
list_remove(list, value, 0); | |
} | |
void list_remove_single(struct LinkList *list, int value) // Alias for list_remove(single = 1); | |
{ | |
list_remove(list, value, 1); | |
} | |
void list_travel(struct LinkList *list) | |
{ | |
struct Data *data = list->head; | |
int i = 0; | |
while(data) | |
{ | |
printf("%02d: %d, ", ++i, data->value); | |
if(i % 5 == 0) printf("\n"); | |
data = data->next; | |
} | |
if(i % 5) printf("\n"); | |
} | |
void list_travel2(struct LinkList *list) | |
{ | |
struct Data *data; | |
int i, value, first, sum; | |
data = list->head; | |
first = 1; | |
sum = 0; | |
while(data) | |
{ | |
++sum; | |
if(first) | |
{ | |
value = data->value; | |
i = 1; | |
first = 0; | |
} | |
else if(value == data->value) | |
{ | |
++i; | |
} | |
else | |
{ | |
printf("資料 %d 有 %d 個\n", value, i); | |
value = data->value; | |
i = 1; | |
} | |
data = data->next; | |
} | |
if(!first) | |
{ | |
printf("資料 %d 有 %d 個\n", value, i); | |
} | |
printf("總共 %d 筆資料\n", sum); | |
} | |
int main() | |
{ | |
struct LinkList list; | |
int array1[16] = {7, 8, 2, 8, 3, 4, 8, 1, 5, 8, 6, 1, 8, 5, 7, 8}; | |
int array2[8] = {7, 2, 3, 4, 1, 5, 6, 8}; | |
int array3[16] = {7, 8, 2, 8, 3, 4, 8, 1, 5, 8, 6, 1, 8, 5, 7, 8}; | |
int i; | |
list_init(&list); | |
for(i = 0; i < 16; ++i) | |
{ | |
list_insert(&list, array1[i]); | |
} | |
list_travel2(&list); | |
printf("\n"); | |
i = list_search(&list, 5); | |
printf("Search 5: %d\n", i); | |
printf("\n"); | |
for(i = 0; i < 8; ++i) | |
{ | |
printf("RemoveAll(%d)\n", array2[i]); | |
list_remove_all(&list, array2[i]); | |
list_travel2(&list); | |
printf("\n"); | |
} | |
printf("\n"); | |
printf("================================================================================\n"); | |
printf("\n"); | |
for(i = 0; i < 16; ++i) | |
{ | |
list_insert(&list, array1[i]); | |
} | |
list_travel2(&list); | |
printf("\n"); | |
for(i = 0; i < 16; ++i) | |
{ | |
printf("RemoveSingle(%d)\n", array3[i]); | |
list_remove_single(&list, array3[i]); | |
list_travel2(&list); | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment