/************************************************* Description: 销毁一个列表 list Parameter: 一个指向待销毁列表的指针 list Return: 空 *************************************************/ voidDestroyList(list_tlist);
/************************************************* Description: 求列表 list 的长度 Parameter: 一个指向列表的指针 list Return: 列表长度 *************************************************/ intGetLength(constlist_tlist);
/************************************************* Description: 判断列表 list 是否已满 Parameter: 一个指向列表的指针 list Return: 如果列表已满则返回 true ,否则返回 false *************************************************/ boolIsFull(constlist_tlist);
/************************************************* Description: 判断列表 list 是否已空 Parameter: 一个指向列表的指针 list Return: 如果列表已空则返回 true ,否则返回 false *************************************************/ boolIsEmpty(constlist_tlist);
/************************************************* Description: 取列表 list 的第 i 个元素 Parameter: 一个指向列表的指针 list 下标 i (0 <= i < GetLength(list)) Return: 第 i 个元素 *************************************************/ item_tGet(constlist_tlist, int i);
/************************************************* Description: 求元素 data 在列表 list 中的下标 Parameter: 一个指向列表的指针 list 一个待寻找元素 data Return: 待寻找元素 data 的下标 i 或者 ERROR *************************************************/ intFind(constlist_tlist, item_t data);
/************************************************* Description: 在列表 list 的下标为 i 的位置上插入一个元素 data Parameter: 一个指向列表的指针 list 下标 i (0 <= i <= GetLength(list)) 待插入元素 data Return: 空 *************************************************/ voidInsert(list_tlist, int i, item_t data);
/************************************************* Description: 从列表 list 当中删除下标为 i 的元素 Parameter: 一个指向列表的指针 list 下标 i (0 <= i < GetLength(list)) Return: 空 *************************************************/ voidDelete(list_tlist, int i);
/************************************************* Description: 输出列表 list 的内容 Parameter: 一个指向列表的指针 list Return: 空 *************************************************/ voidPrint(constlist_tlist);
/************************************************* Description: 就地逆置列表 list Parameter: 一个指向列表的指针 list Return: 空 *************************************************/ voidReverse(list_tlist);
list_tCreateList(void) { list_tlist = (list_t)malloc(sizeof(structlist)); if (list == NULL) { fprintf(stderr, "ERROR: (file %s, line %d) There was not enough memory.\n", __FILE__, __LINE__); exit(-2); }
list->next = NULL;
returnlist; }
voidDestroyList(list_tlist) { if (list) { list_t current;
while (list) { current = list->next; free(list); list = current; } } }
intGetLength(constlist_tlist) { list_t current = list->next; int length = 0;
while (current) { current = current->next; length++; }
voidInsert(list_tlist, int i, item_t data) { if (IsFull(list)) { fprintf(stderr, "The list is full.\n"); return; }
if (i < 0 || i > GetLength(list)) { fprintf(stderr, "Illegal location.\n"); return; }
list_t new = (list_t)malloc(sizeof(structlist)); if (new == NULL) { fprintf(stderr, "ERROR: There was not enough memory.\n"); exit(-2); } new->data = data;
voidDelete(list_tlist, int i) { if (IsEmpty(list)) { fprintf(stderr, "The list is empty.\n"); return; }
if (i < 0 || i > (GetLength(list) - 1)) { fprintf(stderr, "Illegal location.\n"); return; }
list_t tmp = list; for (int j = 0; j < i; j++) { tmp = tmp->next; } list_t del = tmp->next; tmp->next = del->next; free(del); del = NULL; }
voidPrint(constlist_tlist) { printf("Now print the list elements:\n"); list_t tmp = list->next; int len = GetLength(list); for (int i = 0; i < len; i++) { printf("[%d]: %d\n", i, tmp->data); tmp = tmp->next; } printf("That's all.\n"); }
voidReverse(list_tlist) { if (GetLength(list) <= 1) { return; }
list_t pre = list->next; list->next = NULL; list_t tmp;
while (pre) { tmp = pre; pre = pre->next; tmp->next = list->next; list->next = tmp; } }
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { Insert(list1, i, a[i]); printf("Insert an item to list 1 [%d]: %d.\n", i, a[i]); } printf("\n");
for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { Insert(list2, i, b[i]); printf("Insert an item to list 2 [%d]: %d.\n", i, b[i]); } printf("\n");
printf("Reverse list 1.\n"); Reverse(list1); Print(list1); printf("\n");
for (int i = 0; i < GetLength(list2); ++i) { Insert(list1, i, Get(list2, i)); printf("Insert an item from list 2 to list 1 [%d]: %d.\n", i, Get(list2, i)); } printf("\n"); Print(list1); printf("\n");
item_t elem = 999; printf("Find item in list 1: item %d at [%d].\n", elem, Find(list1, elem)); printf("\n");
int length = GetLength(list1); for (int i = 0; i < length; i++) { Delete(list1, 0); printf("Delete an item from list 1.\n"); } printf("Is list 1 empty? %s.\n", IsEmpty(list1) ? "yes" : "no"); printf("\n");
DestroyList(list1); DestroyList(list2); printf("Destroy all lists.\n");
Insert an item to list 1 [0]: 1. Insert an item to list 1 [1]: 2. Insert an item to list 1 [2]: 3. Insert an item to list 1 [3]: 4.
Insert an item to list 2 [0]: 233. Insert an item to list 2 [1]: 666. Insert an item to list 2 [2]: 888. Insert an item to list 2 [3]: 999.
Reverse list 1. Now print the list elements: [0]: 4 [1]: 3 [2]: 2 [3]: 1 That's all.
Insert an item from list 2 to list 1 [0]: 233. Insert an item from list 2 to list 1 [1]: 666. Insert an item from list 2 to list 1 [2]: 888. Insert an item from list 2 to list 1 [3]: 999.
Now print the list elements: [0]: 233 [1]: 666 [2]: 888 [3]: 999 [4]: 4 [5]: 3 [6]: 2 [7]: 1 That's all.
Find item in list 1: item 999 at [3].
Delete an item from list 1. Delete an item from list 1. Delete an item from list 1. Delete an item from list 1. Delete an item from list 1. Delete an item from list 1. Delete an item from list 1. Delete an item from list 1. Is list 1 empty? yes.