#ifndef DATA_STRUCTURE_H
#define DATA_STRUCTURE_H
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
typedef struct Stack_sx{
int* arr;
size_t cap;
size_t top;
}STACK_SX;
void stack_sx_init(STACK_SX* stack, size_t cap);
void stack_sx_deinit(STACK_SX* stack);
int stack_sx_empty(STACK_SX* stack);
int stack_sx_full(STACK_SX* stack);
void stack_sx_push(STACK_SX* stack, int data);
int stack_sx_pop(STACK_SX* stack);
int stack_sx_top(STACK_SX* stack);
size_t stack_sx_size(STACK_SX* stack);
typedef struct StackNode{
int data;
struct StackNode* next;
}STACK_NODE;
typedef struct Stack_ls{
STACK_NODE* top;
}STACK_LS;
void stack_ls_init(STACK_LS* stack);
void stack_ls_deinit(STACK_LS* stack);
int stack_ls_empty(STACK_LS* stack);
void stack_ls_push(STACK_LS* stack, int data);
int stack_ls_pop(STACK_LS* stack);
int stack_ls_top(STACK_LS* stack);
size_t stack_ls_size(STACK_LS* stack);
typedef struct Queue_sx{
int* arr;
size_t cap;
size_t front;
size_t rear;
size_t size;
}QUEUE_SX;
void queue_sx_init(QUEUE_SX* queue, size_t cap);
void queue_sx_deinit(QUEUE_SX* queue);
int queue_sx_empty(QUEUE_SX* queue);
int queue_sx_full(QUEUE_SX* queue);
void queue_sx_push(QUEUE_SX* queue, int data);
int queue_sx_pop(QUEUE_SX* queue);
int queue_sx_front(QUEUE_SX* queue);
size_t queue_sx_size(QUEUE_SX* queue);
typedef struct QueueNode{
int data;
struct QueueNode* next;
}QUEUE_NODE;
typedef struct Queue_ls{
QUEUE_NODE* front;
QUEUE_NODE* rear;
}QUEUE_LS;
void queue_ls_init(QUEUE_LS* queue);
void queue_ls_deinit(QUEUE_LS* queue);
int queue_ls_empty(QUEUE_LS* queue);
void queue_ls_push(QUEUE_LS* queue, int data);
int queue_ls_pop(QUEUE_LS* queue);
int queue_ls_front(QUEUE_LS* queue);
size_t queue_ls_size(QUEUE_LS* queue);
typedef struct ListNode_d{
int data;
struct ListNode_d* next;
}LIST_NODE_D;
typedef struct List_d{
LIST_NODE_D* head;
LIST_NODE_D* tail;
LIST_NODE_D* frwd;
}LIST_D;
void list_d_init(LIST_D* list);
void list_d_deinit(LIST_D* list);
int list_d_empty(LIST_D* list);
void list_d_append(LIST_D* list, int data);
void list_d_iterative(LIST_D* list);
size_t list_d_size(LIST_D* list);
void list_d_print(LIST_D* list);
void list_d_rprint(LIST_D* list);
void list_d_reverse(LIST_D* list);
int list_d_mid(LIST_D* list);
typedef struct ListNode_s{
int data;
struct ListNode_s* next;
struct ListNode_s* prev;
}LIST_NODE_S;
typedef struct List_s{
LIST_NODE_S* head;
LIST_NODE_S* tail;
LIST_NODE_S* frwd;
}LIST_S;
void list_s_init(LIST_S* list);
void list_s_deinit(LIST_S* list);
int list_s_empty(LIST_S* list);
void list_s_append(LIST_S* list, int data);
int list_s_insert(LIST_S* list, size_t pos, int data);
int list_s_erase(LIST_S* list, size_t pos);
void list_s_remove(LIST_S* list, int data);
void list_s_clear(LIST_S* list);
int* list_s_at(LIST_S* list, size_t pos);
size_t list_s_size(LIST_S* list);
void list_s_begin(LIST_S* list);
int* list_s_next(LIST_S* list);
void list_s_reverse(LIST_S* list);
int* list_s_prev(LIST_S* list);
int* list_s_current(LIST_S* list);
int list_s_end(LIST_S* list);
typedef struct BstreeNode{
int data;
struct BstreeNode* left;
struct BstreeNode* right;
}BSTREE_NODE;
typedef struct Bstree{
BSTREE_NODE* root;
size_t size;
}BSTREE;
void bstree_init(BSTREE* bstree);
void bstree_deinit(BSTREE* bstree);
void bstree_insert(BSTREE* bstree, int data);
int bstree_erase(BSTREE* bstree, int data);
void bstree_clear(BSTREE* bstree);
void bstree_update(BSTREE* bstree, int old, int New);
int bstree_exist(BSTREE* bstree, int data);
void bstree_travel(BSTREE* bstree);
size_t bstree_size(BSTREE* bstree);
size_t bstree_height(BSTREE* bstree);
#endif
#include "data_structure.h"
void stack_sx_init(STACK_SX* stack, size_t cap)
{
stack->arr = malloc(cap * sizeof(int));
stack->cap = cap;
stack->top = 0;
}
void stack_sx_deinit(STACK_SX* stack)
{
free(stack->arr);
stack->arr = NULL;
stack->cap = 0;
stack->top = 0;
}
int stack_sx_empty(STACK_SX* stack)
{
return !stack->top;
}
int stack_sx_full(STACK_SX* stack)
{
return stack->top >= stack->cap;
}
void stack_sx_push(STACK_SX* stack, int data)
{
stack->arr[stack->top++] = data;
}
int stack_sx_pop(STACK_SX* stack)
{
stack->top--;
return stack->arr[stack->top];
}
int stack_sx_top(STACK_SX* stack)
{
return stack->arr[stack->top-1];
}
size_t stack_sx_size(STACK_SX* stack)
{
return stack->top;
}
static STACK_NODE* create_stack_node(STACK_NODE* next, int data)
{
STACK_NODE *node = malloc(sizeof(STACK_NODE));
node->data = data;
node->next = next;
return node;
}
static STACK_NODE* destroy_stack_node(STACK_NODE* node)
{
STACK_NODE* next = node->next;
free(node);
return next;
}
void stack_ls_init(STACK_LS* stack)
{
stack->top = NULL;
}
void stack_ls_deinit(STACK_LS* stack)
{
while(stack->top){
stack->top = destroy_stack_node(stack->top);
}
}
int stack_ls_empty(STACK_LS* stack)
{
return !stack->top;
}
void stack_ls_push(STACK_LS* stack, int data)
{
stack->top = create_stack_node(stack->top, data);
}
int stack_ls_pop(STACK_LS* stack)
{
int data = stack->top->data;
stack->top = destroy_stack_node(stack->top);
return data;
}
int stack_ls_top(STACK_LS* stack)
{
return stack->top->data;
}
size_t stack_ls_size(STACK_LS* stack)
{
size_t size = 0;
STACK_NODE* node = stack->top;
for(; node; node = node->next)size++;
return size;
}
void queue_sx_init(QUEUE_SX* queue, size_t cap)
{
queue->arr = malloc(cap * sizeof(int));
queue->cap = cap;
queue->front = 0;
queue->rear = 0;
queue->size = 0;
}
void queue_sx_deinit(QUEUE_SX* queue)
{
free(queue->arr);
queue->arr = NULL;
queue->cap = 0;
queue->front = 0;
queue->rear = 0;
queue->size = 0;
}
int queue_sx_empty(QUEUE_SX* queue)
{
return !queue->size;
}
int queue_sx_full(QUEUE_SX* queue)
{
return queue->size >= queue->cap;
}
void queue_sx_push(QUEUE_SX* queue, int data)
{
if(queue->rear >= queue->cap){
queue->rear = 0;
}queue->size += 1;
queue->arr[queue->rear++] = data;
}
int queue_sx_pop(QUEUE_SX* queue)
{
if(queue->front >= queue->cap){
queue->front = 0;
}queue->size -= 1;
return queue->arr[queue->front++];
}
int queue_sx_front(QUEUE_SX* queue)
{
if(queue->front >= queue->cap){
queue->front = 0;
}return queue->arr[queue->front];
}
size_t queue_sx_size(QUEUE_SX* queue)
{
return queue->size;
}
static QUEUE_NODE* create_queue_node(int data)
{
QUEUE_NODE* node = malloc(sizeof(QUEUE_NODE));
node->data = data;
node->next = NULL;
return node;
}
static QUEUE_NODE* destroy_queue_node(QUEUE_NODE* node)
{
QUEUE_NODE* next = node->next;
free(node);
return next;
}
void queue_ls_init(QUEUE_LS* queue)
{
queue->front = NULL;
queue->rear = NULL;
}
void queue_ls_deinit(QUEUE_LS* queue)
{
while(queue->front){
queue->front = destroy_queue_node(queue->front);
}queue->rear = NULL;
}
int queue_ls_empty(QUEUE_LS* queue)
{
return !queue->front && !queue->rear;
}
void queue_ls_push(QUEUE_LS* queue, int data)
{
QUEUE_NODE* rear = create_queue_node(data);
if(queue->rear){
queue->rear->next = rear;
}else queue->front = rear;
queue->rear = rear;
}
int queue_ls_pop(QUEUE_LS* queue)
{
int data = queue->front->data;
if(!(queue->front = destroy_queue_node(queue->front))){
queue->rear = NULL;
}return data;
}
int queue_ls_front(QUEUE_LS* queue)
{
return queue->front->data;
}
size_t queue_ls_size(QUEUE_LS* queue)
{
size_t size = 0;
QUEUE_NODE* find = queue->front;
for(; find; find = find->next)size++;
return size;
}
static LIST_NODE_D* create_list_node(int data)
{
LIST_NODE_D* node = malloc(sizeof(LIST_NODE_D));
node->data = data;
node->next = NULL;
return node;
}
static LIST_NODE_D* destroy_list_node(LIST_NODE_D* node)
{
LIST_NODE_D* next = node->next;
free(node);
return next;
}
void list_d_init(LIST_D* list)
{
list->head = NULL;
list->tail = NULL;
list->frwd = NULL;
}
void list_d_deinit(LIST_D* list)
{
while(list->head){
list->head = destroy_list_node(list->head);
}list->tail = NULL;
list->frwd = NULL;
}
int list_d_empty(LIST_D* list)
{
return !list->head && !list->tail;
}
void list_d_append(LIST_D* list, int data)
{
LIST_NODE_D* node = create_list_node(data);
if(list->tail){
list->tail->next = node;
}else list->head = node;
list->tail = node;
}
void list_d_iterative(LIST_D* list)
{
list->frwd = list->head;
}
size_t list_d_size(LIST_D* list)
{
size_t size = 0;
LIST_NODE_D* find = list->head;
for(; find; find = find->next)size++;
return size;
}
void list_d_print(LIST_D* list)
{
LIST_NODE_D* node = list->frwd;
for(; node; node = node->next){
printf("%d\t", node->data);
}printf("\n");
}
void rprint(LIST_NODE_D* frwd)
{
if(frwd){
rprint(frwd->next);
printf("%d\t", frwd->data);
}
}
void list_d_rprint(LIST_D* list)
{
rprint(list->frwd);
printf("\n");
}
void reverse(LIST_NODE_D* head)
{
if(head && head->next){
reverse(head->next);
head->next->next = head;
head->next = NULL;
}
}
void list_d_reverse(LIST_D* list)
{
reverse(list->head);
LIST_NODE_D* temp = list->head;
list->head = list->tail;
list->tail = temp;
list->frwd = list->head;
}
int list_d_mid(LIST_D* list)
{
LIST_NODE_D *node = NULL, *mid = NULL;
for(node = mid = list->frwd; node->next && node->next->next;
mid = mid->next, node = node->next->next);
return mid->data;
}
static LIST_NODE_S* create_lists_node(int data, LIST_NODE_S* next, LIST_NODE_S* prev){
LIST_NODE_S* node = malloc(sizeof(LIST_NODE_S));
node->data = data;
node->next = next;
node->prev = prev;
return node;
}
static LIST_NODE_S* destroy_lists_node(LIST_NODE_S* node, LIST_NODE_S** prev){
LIST_NODE_S* next = node->next;
if(prev) *prev = node->prev;
free(node);
return next;
}
void list_s_init(LIST_S* list){
list->head = NULL;
list->tail = NULL;
list->frwd = NULL;
}
void list_s_deinit(LIST_S* list){
while(list->head){
list->head = destroy_lists_node(list->head, NULL);
}
list->tail = NULL;
list->frwd = NULL;
}
int list_s_empty(LIST_S* list){
return !list->head && !list->tail;
}
void list_s_append(LIST_S* list, int data){
list->tail = create_lists_node(data, NULL, list->tail);
if(list->tail->prev){
list->tail->prev->next = list->tail;
}else{
list->head = list->tail;
}
}
int list_s_insert(LIST_S* list, size_t pos, int data){
LIST_NODE_S* find = list->head;
for(; find; find = find->next){
if(!pos--){
LIST_NODE_S* node = create_lists_node(data, find, find->prev);
if(node->prev){
node->prev->next = node;
}else list->head = node;
node->next->prev = node;
return 1;
}
}
return 0;
}
int list_s_erase(LIST_S* list, size_t pos){
LIST_NODE_S* find = list->head;
for(; find; find = find->next){
if(!pos--){
LIST_NODE_S* prev = NULL;
LIST_NODE_S* next = destroy_lists_node(find, &prev);
if(prev){
prev->next = next;
}else list->head = next;
if(next){
next->prev = prev;
}else list->tail = prev;
return 1;
}
}
return 0;
}
void list_s_remove(LIST_S* list, int data){
LIST_NODE_S* find = list->head;
LIST_NODE_S* next = NULL;
for(; find; find = next){
next = find->next;
if(find->data == data){
LIST_NODE_S* prev = NULL;
LIST_NODE_S* next = destroy_lists_node(find, &prev);
if(prev){
prev->next = next;
}else list->head = next;
if(next){
next->prev = prev;
}else list->tail = prev;
}
}
}
void list_s_clear(LIST_S* list){
list_s_deinit(list);
}
int* list_s_at(LIST_S* list, size_t pos){
LIST_NODE_S* find = list->head;
for(; find; find = find->next){
if(!pos--) return &find->data;
}
return NULL;
}
size_t list_s_size(LIST_S* list){
size_t size = 0;
LIST_NODE_S* find = list->head;
for(; find; find = find->next)size++;
return size;
}
void list_s_begin(LIST_S* list){
list->frwd = list->head;
}
int* list_s_next(LIST_S* list){
int* data = &list->frwd->data;
list->frwd = list->frwd->next;
return data;
}
void list_s_reverse(LIST_S* list){
list->frwd = list->tail;
}
int* list_s_prev(LIST_S* list){
int* data = &list->frwd->data;
list->frwd = list->frwd->prev;
return data;
}
int* list_s_current(LIST_S* list){
return &list->frwd->data;
}
int list_s_end(LIST_S* list){
return !list->frwd;
}
static BSTREE_NODE* create_bstree_node(int data){
BSTREE_NODE* node = malloc(sizeof(BSTREE_NODE));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
static void destroy_bstree_node(BSTREE_NODE* node){
free(node);
}
static void clear(BSTREE_NODE** root){
if(*root){
clear(&(*root)->left);
clear(&(*root)->right);
destroy_bstree_node(*root);
*root = NULL;
}
}
static void insert(BSTREE_NODE* node, BSTREE_NODE** root){
if(! *root) *root = node;
else if(node){
if(node->data < (*root)->data)
insert(node, &(*root)->left);
else insert(node, &(*root)->right);
}
}
static BSTREE_NODE** find(int data, BSTREE_NODE** root){
if(! *root) return root;
else{
if(data == (*root)->data) return root;
else if(data < (*root)->data)
return find(data, &(*root)->left);
else return find(data, &(*root)->right);
}
}
static void travel(BSTREE_NODE* root){
if(root){
travel(root->left);
printf("%d ", root->data);
travel(root->right);
}
}
static size_t height(BSTREE_NODE* root){
if(root){
size_t left = height(root->left);
size_t right = height(root->right);
return (left > right ? left : right) + 1;
}
return 0;
}
void bstree_init(BSTREE* bstree){
bstree->root = NULL;
bstree->size = 0;
}
void bstree_deinit(BSTREE* bstree){
clear(&bstree->root);
bstree->size = 0;
}
void bstree_insert(BSTREE* bstree, int data){
insert(create_bstree_node(data), &bstree->root);
bstree->size++;
}
int bstree_erase(BSTREE* bstree, int data){
BSTREE_NODE** node = find(data, &bstree->root);
if(*node){
insert((*node)->left, &(*node)->right);
BSTREE_NODE* temp = *node;
*node = (*node)->right;
destroy_bstree_node(temp);
bstree->size--;
return 1;
}
return 0;
}
void bstree_clear(BSTREE* bstree){
bstree_deinit(bstree);
}
void bstree_update(BSTREE* bstree, int old, int New){
while(bstree_erase(bstree, old))
bstree_insert(bstree, New);
}
int bstree_exist(BSTREE* bstree, int data){
return *find(data, &bstree->root) != NULL;
}
void bstree_travel(BSTREE* bstree){
travel(bstree->root);
printf("\n");
}
size_t bstree_size(BSTREE* bstree){
return bstree->size;
}
size_t bstree_height(BSTREE* bstree){
return height(bstree->root);
}
#include "data_structure.h"
void test_stack_sx(void) {
int i;
STACK_SX stack_sx;
stack_sx_init(&stack_sx, 5);
for(i = 0; !stack_sx_full(&stack_sx); i++){
stack_sx_push(&stack_sx, i+1);
}
printf("栈顶=%d\n", stack_sx_top(&stack_sx));
printf("数量=%d\n", stack_sx_size(&stack_sx));
while(!stack_sx_empty(&stack_sx)){
printf("%d\n", stack_sx_pop(&stack_sx));
}
stack_sx_deinit(&stack_sx);
}
void test_stack_ls(void) {
int i;
STACK_LS stack_ls;
stack_ls_init(&stack_ls);
for(i = 0; i < 5; i++){
stack_ls_push(&stack_ls, i+1);
}
printf("栈顶=%d\n", stack_ls_top(&stack_ls));
printf("数量=%d\n", stack_ls_size(&stack_ls));
while(!stack_ls_empty(&stack_ls)){
printf("%d\n", stack_ls_pop(&stack_ls));
}
stack_ls_deinit(&stack_ls);
}
void test_queue_sx(void) {
int i;
QUEUE_SX queue_sx;
queue_sx_init(&queue_sx, 5);
for(i = 0; !queue_sx_full(&queue_sx); i++){
queue_sx_push(&queue_sx, i+1);
}
printf("队首=%d\n", queue_sx_front(&queue_sx));
printf("数量=%d\n", queue_sx_size(&queue_sx));
while(!queue_sx_empty(&queue_sx)){
printf("%d\n", queue_sx_pop(&queue_sx));
}
queue_sx_deinit(&queue_sx);
}
void test_queue_ls(void) {
int i;
QUEUE_LS queue_ls;
queue_ls_init(&queue_ls);
for(i = 0; i < 5; i++){
queue_ls_push(&queue_ls, i+1);
}
printf("队首=%d\n", queue_ls_front(&queue_ls));
printf("数量=%d\n", queue_ls_size(&queue_ls));
while(!queue_ls_empty(&queue_ls)){
printf("%d\n", queue_ls_pop(&queue_ls));
}
queue_ls_deinit(&queue_ls);
}
void test_list_d(void) {
int i;
LIST_D list_d;
list_d_init(&list_d);
for(i = 0; i < 5; i++){
list_d_append(&list_d, i+1);
}
list_d_iterative(&list_d);
printf("正向打印:\n");
list_d_print(&list_d);
printf("反向打印:\n");
list_d_rprint(&list_d);
printf("逆转链表,正向打印:\n");
list_d_reverse(&list_d); list_d_print(&list_d);
printf("中间值mid = %d\n", list_d_mid(&list_d));
list_d_deinit(&list_d);
}
void test_list_s(void) {
int i;
LIST_S lists;
list_s_init(&lists);
for(i = 0; i < 10; i++){
list_s_append(&lists, (i+1)*10);
}
list_s_insert(&lists, 2, 25);
list_s_erase(&lists, 3);
list_s_append(&lists, 25);
list_s_remove(&lists, 25);
printf("取2位置=%d\n", *list_s_at(&lists, 2));
list_s_begin(&lists);
printf("frwd头数据=%d\n", *list_s_current(&lists));
while(!list_s_end(&lists)){
printf("%d\n", *list_s_next(&lists));
}
printf("数量=%d\n", list_s_size(&lists));
list_s_clear(&lists);
printf("数据为空%d\n", !list_s_empty(&lists));
printf("数量=%d\n", list_s_size(&lists));
list_s_deinit(&lists);
}
void test_bstree(void) {
BSTREE bstree; bstree_init(&bstree);
bstree_insert(&bstree, 50);
bstree_insert(&bstree, 70);
bstree_insert(&bstree, 20);
bstree_insert(&bstree, 60);
bstree_insert(&bstree, 40);
bstree_insert(&bstree, 30);
bstree_insert(&bstree, 10);
bstree_insert(&bstree, 90);
bstree_insert(&bstree, 80);
bstree_travel(&bstree);
printf("元素=%d, 层数=%d\n", bstree_size(&bstree), bstree_height(&bstree));
bstree_erase(&bstree, 60);
bstree_travel(&bstree);
printf("元素=%d, 层数=%d\n", bstree_size(&bstree), bstree_height(&bstree));
bstree_update(&bstree, 70, 77);
bstree_travel(&bstree);
printf("元素=%d, 层数=%d, 查找=%d\n", bstree_size(&bstree), bstree_height(&bstree), bstree_exist(&bstree, 70));
bstree_deinit(&bstree);
bstree_travel(&bstree);
printf("元素=%d, 层数=%d\n", bstree_size(&bstree), bstree_height(&bstree));
}
int main()
{
test_stack_sx();
test_stack_ls();
test_queue_sx();
test_queue_ls();
test_list_d();
test_list_s();
test_bstree();
return 0;
}
测试结果