3 链表
3.1 双向链表
#ifndef _LS_H
#define _LS_H
#include <sys/types.h>
typedef struct ListNode{
int data;
struct ListNode* next;
struct ListNode* prv;
}LIST_NODE;
typedef struct List{
LIST_NODE* head;
LIST_NODE* tail;
LIST_NODE* frwd;
LIST_NODE* bkwd;
}LIST;
void list_init(LIST* list);
void list_deinit(LIST* list);
int list_empty(LIST* list);
void list_append(LIST* list,int data);
int list_insert(LIST* list,size_t pos,int data);
int list_erase(LIST* list,size_t pos);
void list_remove(LIST* list,int data);
void list_clear(LIST* list);
int *list_at(LIST* list,size_t pos);
size_t list_size(LIST* list);
void list_begin(LIST* list);
int *list_next(LIST* list);
int *list_prv(LIST* list);
int *list_current(LIST* list);
int list_end(LIST* list);
#endif
#include <stdio.h>
#include "ls.h"
static LIST_NODE* create_node(int data,LIST_NODE* next,LIST_NODE* prev){
LIST_NODE* node = malloc(sizeof(LIST_NODE));
node->data = data;
node->next = next;
node->prev = prev;
return node;
}
static LIST_NODE* destroy_node(LIST_NODE* node,LIST_NODE* prev){
LIST_NODE* next = node->next;
if (prev)
*prev = node->prev;
free(node);
return next;
}
void list_init(LIST* list){
list->head = NULL;
list->tail = NULL;
}
void list_deinit(LIST* list){
while (list->head)
list->head = destroy_node(list->head,NULL);
list->tail = NULL;
}
int list_empty(LIST* list){
return !list->head && !list->tail;
}
void list_append(LIST* list,int data){
list->tail = create_node(data,NULL,list->tail);
if (list->tail->prev)
list->tail->prev->next = list->tail;
else
list->head = list->tail;
}
int list_insert(LIST* list,size_t pos,int data){
LIST_NODE* find = list->head;
for (;find;find = find->next){
if (!pos--){
LIST_NODE* node = create_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_erase(LIST* list,size_t pos){
LIST_NODE* find = list->head;
for (;find;find = find->next){
if (!pos--){
LIST_NODE* prev = NULL;
LIST_NODE* next = destroy_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_remove(LIST* list,int data){
LIST_NODE* find = list->head;
LIST_NODE* next = NULL;
for (;find;find = next){
next = find-> next;
if (find->data == data){
LIST_NODE* prev = NULL;
LIST_NODE* next = destroy_node(find,&prev);
if (prev)
prev->next = next;
else
list->head = next;
if (next)
next->prev = prev;
else
list->tail = prev;
}
}
}
void list_clear(LIST* list){
list_deinit(list);
}
int* list_at(LIST* list,size_t pos){
LIST_NODE* find = list->head;
for (;find;find = find->next){
if (!pos--)
return &find->data;
}
return NULL;
}
size_t list_size(LIST* list){
size_t size = 0;
LIST_NODE* find = list->head;
for (;find;find = find->next)
size++;
return size;
}
void list_begin(LIST* list){
list->frwd = list->head;
}
int* list_next(LIST* list){
int* data = &list->frwd->data;
list->frwd = list->frwd->next;
return data;
}
int* list_prev(LIST* list){
int* data = &list->frwd->data;
list->frwd = list->frwd->prev;
return data;
}
int* list_current(LIST* list){
return &list->frwd->data;
}
int list_end(LIST* list){
return !list->frwd;
}
#include <stdio.h>
#include "ls.h"
int main(){
LIST list;
list_init(&list);
list_append(&list,10);
list_append(&list,30);
list_append(&list,50);
list_insert(&list,1,20);
list_insert(&list,3,40);
list_begin(&list);
while (!list_end(&list))
printf("%d\n",*list_next(&list));
list_erase(&list,1);
list_append(&list,40);
list_remove(&list,40);
list_begin(&list);
while (!list_end(&list))
printf("%d\n",*list_next(&list));
list_deinit(&list);
}
3.2 单向链表
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode{
int data;
struct ListNode* next;
}LIST_NODE;
typedef struct List{
LIST_NODE* head;
LIST_NODE* tail;
}LIST;
static LIST_NODE* create_node(int data){
LIST_NODE* node = malloc(sizeof(LIST_NODE));
node->data = data;
node->next = NULL;
return node;
}
static LIST_NODE* destroy_node(LIST_NODE* node){
LIST_NODE* next = node->next;
free(node);
return next;
}
void list_init(LIST* list){
list->head = NULL;
list->tail = NULL;
}
void list_deinit(LIST* list){
while (list->head)
list->head = destroy_node(list->head);
list->tail = NULL;
}
void list_append(LIST* list,int data){
LIST_NODE* node = create_node(data);
if (list->tail)
list->tail->next = node;
else
list->head = node;
list->tail = node;
}
size_t list_size(LIST* list){
size_t size = 0;
LIST_NODE* find = list->head;
for (;find;find = find->next)
size++;
return size;
}
void list_print(LIST* list){
LIST_NODE* find = list->head;
for (;find;find = find->next)
printf("%-4d",find->data);
printf("\n");
}
void rprint(LIST_NODE* head){
if (head){
rprint(head->next);
printf("%-4d",head->data);
}
return;
}
void list_rprint(LIST* list){
rprint(list->head);
printf("\n");
}
void reverse(LIST_NODE* head){
if (head && head->next){
reverse(head->next);
head->next->next = head;
head->next = NULL;
}
}
void list_reverse(LIST* list){
reverse(list->head);
LIST_NODE* temp = list->head;
list->head = list->tail;
list->tail = temp;
}
int list_mid(LIST* list){
LIST_NODE* mid = NULL,*node = NULL;
for (mid = node = list->head;
node->next && node->next->next;
node = node->next-next,mid = mid->next);
return mid->data;
}
int main(){
LIST list;
list_init(&list);
list_append(&list,1);
list_append(&list,2);
list_append(&list,3);
list_print(&list);
list_rprint(&list);
list_reverse(&list);
list_print(&list);
printf("mid = %d\n",list_mid(&list));
list_deinit(&list);
}
4 二叉树
#ifndef __BT_H
#define __BT_H
#include <sys/types.h>
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_exit(BSTREE* bstree,int data);
void bstree_travel(BSTREE* bstree);
size_t bstree_size(BSTREE* bstree);
size_t bstree_height(BSTREE* bstree);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "bt.h"
static BSTREE_NODE* create_node(int data){
BSTREE_NODE* node = malloc(sizeof(BSTREE_NODE));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
static void destroy_node(BSTREE_NODE* node){
free(node);
}
static void clear(BSTREE_NODE** root){
if (*root){
clear(&(*root)->left);
clear(&(*root)->right);
destroy_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);
}
}
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_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_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_exit(BSTREE* bstree,int data){
return *find(data,&bstree->root) != NULL;
}
static void travel(BSTREE_NODE* root){
if (root){
travel(root->left);
printf("%d ",root->data);
travel(root->right);
}
}
void bstree_travel(BSTREE* bstree){
travel(bstree->root);
printf("\n");
}
size_t bstree_size(BSTREE* bstree){
return bstree->size;
}
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;
}
size_t bstree_height(BSTREE* bstree){
return height(bstree->root);
}
#include <stdio.h>
#include "bt.h"
int main(){
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("size = %d,height = %d\n",
bstree_size(&bstree),bstree_height(&bstree));
bstree_erase(&bstree,60);
bstree_travel(&bstree);
bstree_deinit(&bstree);
printf("size = %d,height = %d\n",
bstree_size(&bstree),bstree_height(&bstree));
}