#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
// TODO: fill-in real data
} data_t;
typedef struct list_node {
data_t data;
struct list_node *next;
} list_node_t;
list_node_t *glob_list = NULL;
bool data_compare(data_t d1, data_t d2) {
// TODO: compare d1 with d2, return true if d1 < d2, false otherwise
// TODO: if not care about ordering, simply return true
return true;
}
void list_init() {
}
void list_insert() {
data_t data;
// TODO: insert codes that produce data here!
list_node_t *new_node = (list_node_t*)malloc(sizeof(list_node_t));
new_node->data = data;
if (glob_list == NULL) {
glob_list = new_node;
glob_list->next = NULL;
}
else if (data_compare(data, glob_list->data)) {
new_node->next = glob_list;
glob_list = new_node;
}
else {
list_node_t *cur, *prev;
cur = glob_list->next;
prev = glob_list;
while (cur != NULL) {
if (data_compare(prev->data, data) && data_compare(data, cur->data)) {
break;
}
cur = cur->next;
prev = prev->next;
}
prev->next = new_node;
new_node->next = cur;
}
}
void list_find() {
// TODO input some data for finding
list_node_t *cur = glob_list;
while (cur != NULL) {
// TODO replace `true` with a real condition
if (true) {
// TODO dump value inside cur->data
return;
}
cur = cur->next;
}
// TODO replace this if necessary
printf("Not found\n");
}
void list_delete() {
if (glob_list == NULL) {
// TODO replace this if necessary
printf("The list is empty\n");
}
else {
// TODO input some data for finding
if (glob_list->next == NULL) {
// TODO replace `true` with a real condition
if (true) {
// TODO destroy glob_list->data if necessary
free(glob_list);
glob_list = NULL;
}
}
else {
list_node_t *cur, *prev;
prev = glob_list;
cur = glob_list->next;
while (cur != NULL) {
// TODO replace `true` with a real condition
if (true) {
prev->next = cur->next;
// TODO destroy cur->data if necessary
free(cur);
return;
}
cur = cur->next;
prev = prev->next;
}
// TODO replace this if necessary
printf("Not found.\n");
}
}
}
void list_destroy() {
list_node_t *cur = glob_list;
while (cur != NULL) {
list_node_t *t = cur->next;
// TODO destroy cur->data if necessary
free(cur);
cur = t;
}
glob_list = NULL;
}
int main() {
// TODO add your transactions here
return 0;
}
学习版链表通用模板代码
最新推荐文章于 2022-10-19 10:17:13 发布