优化了命名
This commit is contained in:
31
C11-数据结构/ds/line/ball_clock/Makefile
Normal file
31
C11-数据结构/ds/line/ball_clock/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
113
C11-数据结构/ds/line/ball_clock/main.c
Normal file
113
C11-数据结构/ds/line/ball_clock/main.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* @file main.c
|
||||
* @brief 球钟算法
|
||||
*
|
||||
* @author lzy (lllzzzyyy@buaa.edu.cn)
|
||||
* @url https://lzyyyyyy.fun
|
||||
*
|
||||
* @date 2024-04-18
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "queue.h"
|
||||
#include "sqstack.h"
|
||||
|
||||
#define NR_BALL 27
|
||||
|
||||
int check(queue *qu)
|
||||
{
|
||||
int i = (qu->head + 1) % MAXSIZE;
|
||||
|
||||
do
|
||||
{
|
||||
if (qu->data[i] > qu->data[(i + 1) / MAXSIZE])
|
||||
return 0;
|
||||
i = (i + 1) % MAXSIZE;
|
||||
} while (i != qu->tail);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
queue *qu;
|
||||
int t, value;
|
||||
int time;
|
||||
sqstack *st_min, *st_fivemin, *st_hour;
|
||||
|
||||
qu = qu_create();
|
||||
if (NULL == qu)
|
||||
exit(1);
|
||||
|
||||
st_min = st_create();
|
||||
if (NULL == st_min)
|
||||
exit(1);
|
||||
|
||||
st_fivemin = st_create();
|
||||
if (NULL == st_fivemin)
|
||||
exit(1);
|
||||
|
||||
st_hour = st_create();
|
||||
if (NULL == st_hour)
|
||||
exit(1);
|
||||
|
||||
|
||||
for (i = 1; i <= NR_BALL; i++)
|
||||
qu_enqueue(qu, &i);
|
||||
|
||||
qu_travel(qu);
|
||||
|
||||
while (1)
|
||||
{
|
||||
qu_dequeue(qu, &t);
|
||||
time++;
|
||||
|
||||
if (st_min->top != 3)
|
||||
{
|
||||
st_push(st_min, &t);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!st_isempty(st_min))
|
||||
{
|
||||
st_pop(st_min, &value);
|
||||
qu_enqueue(qu, &value);
|
||||
}
|
||||
if (st_fivemin->top != 10)
|
||||
st_push(st_fivemin, &t);
|
||||
else
|
||||
{
|
||||
while (!st_isempty(st_fivemin))
|
||||
{
|
||||
st_pop(st_fivemin, &value);
|
||||
qu_enqueue(qu, &value);
|
||||
}
|
||||
if (st_hour->top != 10)
|
||||
st_push(st_hour, &t);
|
||||
else
|
||||
{
|
||||
while (!st_isempty(st_hour))
|
||||
{
|
||||
st_pop(st_hour, &value);
|
||||
qu_enqueue(qu, &value);
|
||||
}
|
||||
qu_enqueue(qu, &t);
|
||||
if (check(qu))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("time = %d\n", time);
|
||||
|
||||
qu_destroy(qu);
|
||||
st_destroy(st_min);
|
||||
st_destroy(st_fivemin);
|
||||
st_destroy(st_hour);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
72
C11-数据结构/ds/line/ball_clock/queue.c
Normal file
72
C11-数据结构/ds/line/ball_clock/queue.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
|
||||
queue *qu_create()
|
||||
{
|
||||
queue *sq;
|
||||
|
||||
sq = malloc(sizeof(*sq));
|
||||
if (NULL == sq)
|
||||
return NULL;
|
||||
|
||||
sq->head = 0;
|
||||
sq->tail = 0;
|
||||
|
||||
return sq;
|
||||
}
|
||||
|
||||
int qu_isempty(queue *sq)
|
||||
{
|
||||
return (sq->head == sq->tail);
|
||||
}
|
||||
|
||||
int qu_enqueue(queue *sq, datatype *x)
|
||||
{
|
||||
if ((sq->tail + 1) % MAXSIZE == sq->head)
|
||||
return -1;
|
||||
|
||||
sq->tail = (sq->tail + 1) % MAXSIZE;
|
||||
sq->data[sq->tail] = *x;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int qu_dequeue(queue *sq, datatype *x)
|
||||
{
|
||||
if (qu_isempty(sq))
|
||||
return -1;
|
||||
|
||||
sq->head = (sq->head + 1) % MAXSIZE;
|
||||
*x = sq->data[sq->head];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void qu_travel(queue *sq)
|
||||
{
|
||||
if (sq->head == sq->tail)
|
||||
return;
|
||||
|
||||
int i;
|
||||
|
||||
i = (sq->head + 1) % MAXSIZE;
|
||||
while (i != sq->tail)
|
||||
{
|
||||
printf("%d ", sq->data[i]);
|
||||
i = (i + 1) % MAXSIZE;
|
||||
}
|
||||
printf("%d \n", sq->data[i]);
|
||||
}
|
||||
|
||||
void qu_clear(queue *sq)
|
||||
{
|
||||
sq->head = sq->tail;
|
||||
}
|
||||
|
||||
void qu_destroy(queue *sq)
|
||||
{
|
||||
free(sq);
|
||||
}
|
||||
29
C11-数据结构/ds/line/ball_clock/queue.h
Normal file
29
C11-数据结构/ds/line/ball_clock/queue.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef QUEUE_H__
|
||||
#define QUEUE_H__
|
||||
|
||||
#define MAXSIZE 32
|
||||
|
||||
typedef int datatype;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
datatype data[MAXSIZE];
|
||||
int head, tail;
|
||||
} queue;
|
||||
|
||||
|
||||
queue *qu_create();
|
||||
|
||||
int qu_isempty();
|
||||
|
||||
int qu_enqueue(queue *, datatype *);
|
||||
|
||||
int qu_dequeue(queue *, datatype *);
|
||||
|
||||
void qu_travel(queue *);
|
||||
|
||||
void qu_clear(queue *);
|
||||
|
||||
void qu_destroy(queue *);
|
||||
|
||||
#endif
|
||||
72
C11-数据结构/ds/line/ball_clock/sqstack.c
Normal file
72
C11-数据结构/ds/line/ball_clock/sqstack.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sqstack.h"
|
||||
|
||||
sqstack *st_create(void)
|
||||
{
|
||||
sqstack *st;
|
||||
|
||||
st = malloc(sizeof(*st));
|
||||
if (NULL == st)
|
||||
return NULL;
|
||||
|
||||
st->top = -1;
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
int st_isempty(sqstack *st)
|
||||
{
|
||||
return (-1 == st->top);
|
||||
}
|
||||
|
||||
int st_push(sqstack *st, type *data)
|
||||
{
|
||||
if (SIZE - 1 == st->top)
|
||||
return -1;
|
||||
|
||||
st->data[++st->top] = *data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 取出栈顶元素 */
|
||||
int st_pop(sqstack *st, type *data)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return -1;
|
||||
|
||||
*data = st->data[st->top--];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* 查看栈顶元素 */
|
||||
int st_top(sqstack *st, type *data)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return -1;
|
||||
|
||||
*data = st->data[st->top];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void st_travel(sqstack *st)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return;
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i <= st->top; i++)
|
||||
printf("%d ", st->data[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void st_destroy(sqstack *st)
|
||||
{
|
||||
free(st);
|
||||
}
|
||||
30
C11-数据结构/ds/line/ball_clock/sqstack.h
Normal file
30
C11-数据结构/ds/line/ball_clock/sqstack.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef SQSTACK_H__
|
||||
#define SQSTACK_H__
|
||||
|
||||
#define SIZE 32
|
||||
|
||||
typedef int type;
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
type data[SIZE];
|
||||
int top;
|
||||
} sqstack;
|
||||
|
||||
sqstack *st_create(void);
|
||||
|
||||
int st_isempty(sqstack *);
|
||||
|
||||
int st_push(sqstack *, type *);
|
||||
|
||||
/* 取出栈顶元素 */
|
||||
int st_pop(sqstack *, type *);
|
||||
|
||||
/* 查看栈顶元素 */
|
||||
int st_top(sqstack *, type *);
|
||||
|
||||
void st_travel(sqstack *);
|
||||
|
||||
void st_destroy(sqstack *);
|
||||
|
||||
#endif
|
||||
31
C11-数据结构/ds/line/compute/Makefile
Normal file
31
C11-数据结构/ds/line/compute/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
149
C11-数据结构/ds/line/compute/main.c
Normal file
149
C11-数据结构/ds/line/compute/main.c
Normal file
@@ -0,0 +1,149 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sqstack.h"
|
||||
|
||||
// !!! 课程不完整,程序未写完
|
||||
// TODO: snum和sop需要不同的datatype
|
||||
|
||||
static void compute(sqstack *snum, int *op)
|
||||
{
|
||||
datatype n1, n2, n;
|
||||
|
||||
st_pop(snum, &n2);
|
||||
st_pop(snum, &n1);
|
||||
|
||||
switch (*op)
|
||||
{
|
||||
case '+': n = n1 + n2; break;
|
||||
case '-': n = n1 - n2; break;
|
||||
case '*': n = n1 * n2; break;
|
||||
case '/': n = n1 / n2; break;
|
||||
|
||||
default: exit(1);
|
||||
}
|
||||
st_push(snum, &n);
|
||||
}
|
||||
|
||||
static deal_bracket(sqstack *snum, sqstack *sop)
|
||||
{
|
||||
datatype old_op;
|
||||
|
||||
st_top(sop, &old_op);
|
||||
|
||||
while (old_op != '(')
|
||||
{
|
||||
st_pop(sop, &old_op);
|
||||
compute(snum, &old_op);
|
||||
st_top(sop, &old_op);
|
||||
}
|
||||
|
||||
st_pop(sop, &old_op);
|
||||
}
|
||||
|
||||
static int get_pri(int op)
|
||||
{
|
||||
switch (op)
|
||||
{
|
||||
case '(': return 0;
|
||||
case '+':
|
||||
case '-': return 1;
|
||||
case '*':
|
||||
case '/': return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void deal_op(sqstack *snum, sqstack *sop, char op)
|
||||
{
|
||||
datatype old_op;
|
||||
|
||||
if (st_isempty(sop) || op == '(')
|
||||
{
|
||||
st_push(sop, &op);
|
||||
return;
|
||||
}
|
||||
|
||||
st_top(sop, &old_op);
|
||||
|
||||
if (get_pri(op) > get_pri(old_op))
|
||||
{
|
||||
st_push(sop, &op);
|
||||
return;
|
||||
}
|
||||
|
||||
while (get_pri(op) <= get_pri(old_op))
|
||||
{
|
||||
st_pop(sop, &old_op);
|
||||
compute(snum, &old_op);
|
||||
if (st_isempty(sop))
|
||||
break;
|
||||
|
||||
st_top(sop, &old_op);
|
||||
}
|
||||
st_push(sop, &op);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
char str[] = "(11+3)*2-5";
|
||||
sqstack *snum, *sop;
|
||||
int value = 0;
|
||||
int flag = 0;
|
||||
int old_op;
|
||||
|
||||
snum = st_create();
|
||||
if (NULL == snum)
|
||||
exit(1);
|
||||
|
||||
sop = st_create();
|
||||
if (NULL == sop)
|
||||
exit(1);
|
||||
|
||||
while (str[i] != '\n')
|
||||
{
|
||||
if (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
value = 10 * value + (str[i] - '0');
|
||||
flag = 1;
|
||||
}
|
||||
else // is a op
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
st_push(snum, &value);
|
||||
flag = 0;
|
||||
value = 0;
|
||||
}
|
||||
|
||||
if (str[i] == ')')
|
||||
{
|
||||
deal_bracket(snum, sop);
|
||||
}
|
||||
else // ( + - * /
|
||||
{
|
||||
deal_op(snum, sop, str[i]);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (flag)
|
||||
st_push(snum, &value);
|
||||
|
||||
while (!st_isempty(sop))
|
||||
{
|
||||
st_pop(sop, &old_op);
|
||||
compute(snum, &old_op);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
st_destroy(snum);
|
||||
st_destroy(sop);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
72
C11-数据结构/ds/line/compute/sqstack.c
Normal file
72
C11-数据结构/ds/line/compute/sqstack.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sqstack.h"
|
||||
|
||||
sqstack *st_create(void)
|
||||
{
|
||||
sqstack *st;
|
||||
|
||||
st = malloc(sizeof(*st));
|
||||
if (NULL == st)
|
||||
return NULL;
|
||||
|
||||
st->top = -1;
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
int st_isempty(sqstack *st)
|
||||
{
|
||||
return (-1 == st->top);
|
||||
}
|
||||
|
||||
int st_push(sqstack *st, datatype *data)
|
||||
{
|
||||
if (MAXSIZE - 1 == st->top)
|
||||
return -1;
|
||||
|
||||
st->data[++st->top] = *data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 取出栈顶元素 */
|
||||
int st_pop(sqstack *st, datatype *data)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return -1;
|
||||
|
||||
*data = st->data[st->top--];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* 查看栈顶元素 */
|
||||
int st_top(sqstack *st, datatype *data)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return -1;
|
||||
|
||||
*data = st->data[st->top];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void st_travel(sqstack *st)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return;
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i <= st->top; i++)
|
||||
printf("%d ", st->data[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void st_destroy(sqstack *st)
|
||||
{
|
||||
free(st);
|
||||
}
|
||||
30
C11-数据结构/ds/line/compute/sqstack.h
Normal file
30
C11-数据结构/ds/line/compute/sqstack.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef SQSTACK_H__
|
||||
#define SQSTACK_H__
|
||||
|
||||
#define MAXSIZE 32
|
||||
|
||||
typedef int datatype;
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
datatype data[MAXSIZE];
|
||||
int top;
|
||||
} sqstack;
|
||||
|
||||
sqstack *st_create(void);
|
||||
|
||||
int st_isempty(sqstack *);
|
||||
|
||||
int st_push(sqstack *, datatype *);
|
||||
|
||||
/* 取出栈顶元素 */
|
||||
int st_pop(sqstack *, datatype *);
|
||||
|
||||
/* 查看栈顶元素 */
|
||||
int st_top(sqstack *, datatype *);
|
||||
|
||||
void st_travel(sqstack *);
|
||||
|
||||
void st_destroy(sqstack *);
|
||||
|
||||
#endif
|
||||
81
C11-数据结构/ds/line/list/arr/main.c
Normal file
81
C11-数据结构/ds/line/list/arr/main.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sqlist.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
sqlist *list = NULL;
|
||||
sqlist *list1 = NULL;
|
||||
|
||||
datatype arr[] = {12, 23, 34, 45, 56};
|
||||
datatype arr1[] = {89, 90, 78, 67, 56, 43};
|
||||
|
||||
int i, err;
|
||||
|
||||
list = sqlist_create();
|
||||
// sqlist_create1(&list);
|
||||
if (NULL == list)
|
||||
{
|
||||
fprintf(stderr, "sqlist_create() failed!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
list1 = sqlist_create();
|
||||
if (NULL == list1)
|
||||
{
|
||||
fprintf(stderr, "sqlist_create() failed!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
for (i = 0; i < sizeof(arr) / sizeof(*arr); i++)
|
||||
{
|
||||
// !!! '=' 的优先级低于 '!='
|
||||
if ((err = sqlist_insert(list, 0, &arr[i])) != 0)
|
||||
{
|
||||
if (-1 == err)
|
||||
fprintf(stderr, "The arr is full.\n");
|
||||
else if (-2 == err)
|
||||
fprintf(stderr, "The pos you want to insert is wrong.\n");
|
||||
else
|
||||
fprintf(stderr, "Error!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < sizeof(arr1) / sizeof(*arr1); i++)
|
||||
{
|
||||
// !!! '=' 的优先级低于 '!='
|
||||
if ((err = sqlist_insert(list1, 0, &arr1[i])) != 0)
|
||||
{
|
||||
if (-1 == err)
|
||||
fprintf(stderr, "The arr is full.\n");
|
||||
else if (-2 == err)
|
||||
fprintf(stderr, "The pos you want to insert is wrong.\n");
|
||||
else
|
||||
fprintf(stderr, "Error!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// !!! 用打印行号排查段错误
|
||||
// printf("%d\n", __LINE__);
|
||||
sqlist_display(list);
|
||||
sqlist_display(list1);
|
||||
|
||||
sqlist_union(list, list1);
|
||||
sqlist_display(list);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
sqlist_delete(list, 1);
|
||||
sqlist_display(list);
|
||||
#endif
|
||||
|
||||
sqlist_destroy(list);
|
||||
sqlist_destroy(list1);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
7
C11-数据结构/ds/line/list/arr/makefile
Normal file
7
C11-数据结构/ds/line/list/arr/makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
all:main
|
||||
|
||||
main:main.o sqlist.o
|
||||
$(CC) $^ -o $@
|
||||
|
||||
clean:
|
||||
rm *.o main -rf
|
||||
131
C11-数据结构/ds/line/list/arr/sqlist.c
Normal file
131
C11-数据结构/ds/line/list/arr/sqlist.c
Normal file
@@ -0,0 +1,131 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sqlist.h"
|
||||
|
||||
sqlist *sqlist_create()
|
||||
{
|
||||
sqlist *me;
|
||||
|
||||
me = malloc(sizeof(*me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
|
||||
me->last = -1;
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
void sqlist_create1(sqlist **ptr)
|
||||
{
|
||||
*ptr = malloc(sizeof(**ptr));
|
||||
if (NULL == *ptr)
|
||||
return;
|
||||
|
||||
(*ptr)->last = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
int sqlist_insert(sqlist *me, int i, datatype *data)
|
||||
{
|
||||
int j;
|
||||
|
||||
if (DATASIZE - 1 == me->last)
|
||||
return -1;
|
||||
|
||||
if (i < 0 || i > me->last + 1)
|
||||
return -2;
|
||||
|
||||
for (j = me->last; i <= j; j--)
|
||||
{
|
||||
me->data[j + 1] = me->data[j];
|
||||
}
|
||||
|
||||
// TODO: 仅限常规数据结构
|
||||
me->data[i] = *data;
|
||||
me->last++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sqlist_delete(sqlist *me, int i)
|
||||
{
|
||||
int j;
|
||||
|
||||
if (i < 0 || i > me->last)
|
||||
return -1;
|
||||
|
||||
for (j = i + 1; j <= me->last; j++)
|
||||
{
|
||||
me->data[j - 1] = me->data[j];
|
||||
}
|
||||
|
||||
me->last--;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sqlist_find(sqlist *me, datatype *data)
|
||||
{
|
||||
if (0 == sqlist_isempty(me))
|
||||
return -1;
|
||||
|
||||
|
||||
for (int i = 0; i < me->last; i++)
|
||||
{
|
||||
if (me->data[i] == *data)
|
||||
return i;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
int sqlist_isempty(sqlist *me)
|
||||
{
|
||||
if (-1 == me->last)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int sqlist_setempty(sqlist *me)
|
||||
{
|
||||
me->last = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sqlist_getnum(sqlist *me)
|
||||
{
|
||||
return me->last + 1;
|
||||
}
|
||||
|
||||
void sqlist_display(sqlist *me)
|
||||
{
|
||||
if (-1 == me->last)
|
||||
return;
|
||||
for (int i = 0; i <= me->last; i++)
|
||||
{
|
||||
printf("%d ", me->data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int sqlist_destroy(sqlist *me)
|
||||
{
|
||||
free(me);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sqlist_union(sqlist *list1, sqlist *list2)
|
||||
{
|
||||
// list1 -> 12 23 34 45 56
|
||||
// list2 -> 78 89 56 23 10
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i <= list2->last; i++)
|
||||
{
|
||||
if (sqlist_find(list1, &list2->data[i]) < 0)
|
||||
{
|
||||
sqlist_insert(list1, 0, &list2->data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
C11-数据结构/ds/line/list/arr/sqlist.h
Normal file
36
C11-数据结构/ds/line/list/arr/sqlist.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef SQLIST_H__
|
||||
#define SQLIST_H__
|
||||
|
||||
#define DATASIZE 1024
|
||||
|
||||
typedef int datatype;
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
datatype data[DATASIZE];
|
||||
int last;
|
||||
} sqlist;
|
||||
|
||||
sqlist *sqlist_create();
|
||||
void sqlist_create1(sqlist **me);
|
||||
|
||||
int sqlist_insert(sqlist *, int i, datatype *data);
|
||||
|
||||
int sqlist_delete(sqlist *, int i);
|
||||
|
||||
int sqlist_find(sqlist *, datatype *data);
|
||||
|
||||
int sqlist_isempty(sqlist *);
|
||||
|
||||
int sqlist_setempty(sqlist *);
|
||||
|
||||
int sqlist_getnum(sqlist *);
|
||||
|
||||
void sqlist_display(sqlist *);
|
||||
|
||||
int sqlist_destroy(sqlist *);
|
||||
|
||||
int sqlist_union(sqlist *, sqlist *);
|
||||
|
||||
|
||||
#endif
|
||||
56
C11-数据结构/ds/line/list/linklist/double/kernel/list.h
Normal file
56
C11-数据结构/ds/line/list/linklist/double/kernel/list.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef LINUX_LIST_H__
|
||||
#define LINUX_LIST_H__
|
||||
|
||||
struct list_head
|
||||
{
|
||||
struct list_head *prev;
|
||||
struct list_head *next;
|
||||
};
|
||||
|
||||
#define LIST_HEAD_INIT(name) \
|
||||
{ \
|
||||
&(name), &(name) \
|
||||
}
|
||||
|
||||
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
|
||||
|
||||
static inline void __list_add(struct list_head *new,
|
||||
struct list_head *prev,
|
||||
struct list_head *next)
|
||||
{
|
||||
next->prev = new;
|
||||
new->next = next;
|
||||
new->prev = prev;
|
||||
prev->next = new;
|
||||
}
|
||||
|
||||
static inline void list_add(struct list_head *new, struct list_head *head)
|
||||
{
|
||||
__list_add(new, head, head->next);
|
||||
}
|
||||
|
||||
#define __list_for_each(pos, head) \
|
||||
for (pos = (head)->next; pos != (head); pos = pos->next)
|
||||
|
||||
|
||||
// 这个内核的链表仅包含两个指针,用来被数据包含
|
||||
// 希望由嵌入的指针找回到完整的结构体
|
||||
|
||||
/**
|
||||
* ptr->cur
|
||||
* type->struct score_st
|
||||
* member->node
|
||||
*
|
||||
*/
|
||||
|
||||
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)
|
||||
|
||||
#define container_of(ptr, type, member) \
|
||||
({ \
|
||||
const typeof(((type *)0)->member) *__mptr = (ptr); \
|
||||
(type *)((char *)__mptr - offsetof(type, member)); \
|
||||
})
|
||||
|
||||
#define list_entry(ptr, type, member) container_of(ptr, type, member)
|
||||
|
||||
#endif
|
||||
63
C11-数据结构/ds/line/list/linklist/double/kernel/main.c
Normal file
63
C11-数据结构/ds/line/list/linklist/double/kernel/main.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
struct list_head node;
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
static void print_s(struct score_st *d)
|
||||
{
|
||||
printf("%d %s %d %d \n", d->id, d->name, d->math, d->chinese);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
struct score_st *datap;
|
||||
struct list_head *cur;
|
||||
|
||||
LIST_HEAD(head);
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
datap = malloc(sizeof(*datap));
|
||||
if (NULL == datap)
|
||||
exit(1);
|
||||
|
||||
datap->id = i;
|
||||
snprintf(datap->name, NAMESIZE, "stu%d", i);
|
||||
datap->math = rand() % 100;
|
||||
datap->chinese = rand() % 100;
|
||||
|
||||
list_add(&datap->node, &head);
|
||||
}
|
||||
|
||||
__list_for_each(cur, &head)
|
||||
{
|
||||
datap = list_entry(cur, struct score_st, node);
|
||||
print_s(datap);
|
||||
}
|
||||
|
||||
|
||||
__list_for_each(cur, &head)
|
||||
{
|
||||
datap = list_entry(cur, struct score_st, node);
|
||||
if (5 == datap->id)
|
||||
break;
|
||||
}
|
||||
if (cur == &head)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
print_s(datap);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
31
C11-数据结构/ds/line/list/linklist/double/kernel/makefile
Normal file
31
C11-数据结构/ds/line/list/linklist/double/kernel/makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
31
C11-数据结构/ds/line/list/linklist/double/lib1/Makefile
Normal file
31
C11-数据结构/ds/line/list/linklist/double/lib1/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
134
C11-数据结构/ds/line/list/linklist/double/lib1/llist.c
Normal file
134
C11-数据结构/ds/line/list/linklist/double/lib1/llist.c
Normal file
@@ -0,0 +1,134 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->size = initsize;
|
||||
new->head.data = NULL;
|
||||
new->head.prev = &new->head;
|
||||
new->head.next = &new->head;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
return -1;
|
||||
|
||||
newnode->data = malloc(ptr->size);
|
||||
if (newnode->data == NULL)
|
||||
return -2;
|
||||
|
||||
memcpy(newnode->data, data, ptr->size);
|
||||
|
||||
if (LLIST_FORWARD == mode)
|
||||
{
|
||||
newnode->prev = &ptr->head;
|
||||
newnode->next = ptr->head.next;
|
||||
}
|
||||
else if (LLIST_BACKWARD == mode)
|
||||
{
|
||||
newnode->prev = ptr->head.prev;
|
||||
newnode->next = &ptr->head;
|
||||
}
|
||||
else // error
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
// !!! 妙啊
|
||||
newnode->next->prev = newnode;
|
||||
newnode->prev->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st *find_(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
{
|
||||
if (0 == cmp(key, cur->data))
|
||||
break;
|
||||
}
|
||||
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
return find_(ptr, key, cmp)->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
// !!!
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
free(node->data);
|
||||
free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
node->next->prev = node->prev;
|
||||
node->prev->next = node->next;
|
||||
|
||||
if (data != NULL)
|
||||
memcpy(data, node->data, ptr->size);
|
||||
|
||||
free(node->data);
|
||||
free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = next)
|
||||
{
|
||||
next = cur->next;
|
||||
free(cur->data);
|
||||
free(cur);
|
||||
}
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
37
C11-数据结构/ds/line/list/linklist/double/lib1/llist.h
Normal file
37
C11-数据结构/ds/line/list/linklist/double/lib1/llist.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef LLIST_H__
|
||||
#define LLIST_H__
|
||||
|
||||
#define LLIST_FORWARD 1
|
||||
#define LLIST_BACKWARD 2
|
||||
|
||||
struct llist_node_st
|
||||
{
|
||||
void *data;
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int size;
|
||||
struct llist_node_st head;
|
||||
} LLIST;
|
||||
|
||||
typedef void llist_op(const void *);
|
||||
typedef int llist_cmp(const void *, const void *);
|
||||
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *);
|
||||
|
||||
int llist_delete(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_fetch(LLIST *, const void *key, llist_cmp *, void *data);
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#endif
|
||||
95
C11-数据结构/ds/line/list/linklist/double/lib1/main.c
Normal file
95
C11-数据结构/ds/line/list/linklist/double/lib1/main.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
void print_s(const void *record)
|
||||
{
|
||||
const struct score_st *r = record;
|
||||
|
||||
printf("%d %s %d %d\n", r->id, r->name, r->math, r->chinese);
|
||||
}
|
||||
|
||||
int id_cmp(const void *key, const void *record)
|
||||
{
|
||||
const int *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return (*k - r->id);
|
||||
}
|
||||
|
||||
int name_cmp(const void *key, const void *record)
|
||||
{
|
||||
const char *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return strcmp(k, r->name);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
LLIST *handler;
|
||||
int i, ret;
|
||||
struct score_st tmp;
|
||||
|
||||
handler = llist_create(sizeof(struct score_st));
|
||||
if (NULL == handler)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "std%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
ret = llist_insert(handler, &tmp, LLIST_FORWARD);
|
||||
if (ret)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
llist_travel(handler, print_s);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
// int id = 3;
|
||||
char *del_name = "std6";
|
||||
|
||||
// ret = llist_delete(handler, &id, id_cmp);
|
||||
ret = llist_delete(handler, del_name, name_cmp);
|
||||
if (ret)
|
||||
printf("llist_deleter failed!\n");
|
||||
|
||||
llist_travel(handler, print_s);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
int id = 3;
|
||||
struct score_st *data;
|
||||
|
||||
data = llist_find(handler, &id, id_cmp);
|
||||
if (NULL == data)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
print_s(data);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
llist_destroy(handler);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
31
C11-数据结构/ds/line/list/linklist/double/lib2/Makefile
Normal file
31
C11-数据结构/ds/line/list/linklist/double/lib2/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
130
C11-数据结构/ds/line/list/linklist/double/lib2/llist.c
Normal file
130
C11-数据结构/ds/line/list/linklist/double/lib2/llist.c
Normal file
@@ -0,0 +1,130 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->size = initsize;
|
||||
new->head.prev = &new->head;
|
||||
new->head.next = &new->head;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
return -1;
|
||||
|
||||
memcpy(newnode->data, data, ptr->size);
|
||||
|
||||
if (LLIST_FORWARD == mode)
|
||||
{
|
||||
newnode->prev = &ptr->head;
|
||||
newnode->next = ptr->head.next;
|
||||
}
|
||||
else if (LLIST_BACKWARD == mode)
|
||||
{
|
||||
newnode->prev = ptr->head.prev;
|
||||
newnode->next = &ptr->head;
|
||||
}
|
||||
else // error
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
// !!! 妙啊
|
||||
newnode->next->prev = newnode;
|
||||
newnode->prev->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st *find_(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
{
|
||||
if (0 == cmp(key, cur->data))
|
||||
break;
|
||||
}
|
||||
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
// !!!
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
free(node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
node->next->prev = node->prev;
|
||||
node->prev->next = node->next;
|
||||
|
||||
if (data != NULL)
|
||||
memcpy(data, node->data, ptr->size);
|
||||
|
||||
free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = next)
|
||||
{
|
||||
next = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
39
C11-数据结构/ds/line/list/linklist/double/lib2/llist.h
Normal file
39
C11-数据结构/ds/line/list/linklist/double/lib2/llist.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef LLIST_H__
|
||||
#define LLIST_H__
|
||||
|
||||
#define LLIST_FORWARD 1
|
||||
#define LLIST_BACKWARD 2
|
||||
|
||||
// !!! 变长结构体
|
||||
struct llist_node_st
|
||||
{
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
// data要放在最后
|
||||
char data[1]; // 占位符,数据的起始。C99才只是[0]
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int size;
|
||||
struct llist_node_st head;
|
||||
} LLIST;
|
||||
|
||||
typedef void llist_op(const void *);
|
||||
typedef int llist_cmp(const void *, const void *);
|
||||
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *);
|
||||
|
||||
int llist_delete(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_fetch(LLIST *, const void *key, llist_cmp *, void *data);
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#endif
|
||||
95
C11-数据结构/ds/line/list/linklist/double/lib2/main.c
Normal file
95
C11-数据结构/ds/line/list/linklist/double/lib2/main.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
void print_s(const void *record)
|
||||
{
|
||||
const struct score_st *r = record;
|
||||
|
||||
printf("%d %s %d %d\n", r->id, r->name, r->math, r->chinese);
|
||||
}
|
||||
|
||||
int id_cmp(const void *key, const void *record)
|
||||
{
|
||||
const int *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return (*k - r->id);
|
||||
}
|
||||
|
||||
int name_cmp(const void *key, const void *record)
|
||||
{
|
||||
const char *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return strcmp(k, r->name);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
LLIST *handler;
|
||||
int i, ret;
|
||||
struct score_st tmp;
|
||||
|
||||
handler = llist_create(sizeof(struct score_st));
|
||||
if (NULL == handler)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "std%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
ret = llist_insert(handler, &tmp, LLIST_FORWARD);
|
||||
if (ret)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
llist_travel(handler, print_s);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
// int id = 3;
|
||||
char *del_name = "std6";
|
||||
|
||||
// ret = llist_delete(handler, &id, id_cmp);
|
||||
ret = llist_delete(handler, del_name, name_cmp);
|
||||
if (ret)
|
||||
printf("llist_deleter failed!\n");
|
||||
|
||||
llist_travel(handler, print_s);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
int id = 3;
|
||||
struct score_st *data;
|
||||
|
||||
data = llist_find(handler, &id, id_cmp);
|
||||
if (NULL == data)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
print_s(data);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
llist_destroy(handler);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
31
C11-数据结构/ds/line/list/linklist/double/lib3/Makefile
Normal file
31
C11-数据结构/ds/line/list/linklist/double/lib3/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
142
C11-数据结构/ds/line/list/linklist/double/lib3/llist.c
Normal file
142
C11-数据结构/ds/line/list/linklist/double/lib3/llist.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *);
|
||||
int llist_delete(LLIST *, const void *key, llist_cmp *);
|
||||
int llist_fetch(LLIST *, const void *key, llist_cmp *, void *data);
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->size = initsize;
|
||||
new->head.prev = &new->head;
|
||||
new->head.next = &new->head;
|
||||
|
||||
new->insert = llist_insert;
|
||||
new->delete = llist_delete;
|
||||
new->find = llist_find;
|
||||
new->fetch = llist_fetch;
|
||||
new->travel = llist_travel;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
return -1;
|
||||
|
||||
memcpy(newnode->data, data, ptr->size);
|
||||
|
||||
if (LLIST_FORWARD == mode)
|
||||
{
|
||||
newnode->prev = &ptr->head;
|
||||
newnode->next = ptr->head.next;
|
||||
}
|
||||
else if (LLIST_BACKWARD == mode)
|
||||
{
|
||||
newnode->prev = ptr->head.prev;
|
||||
newnode->next = &ptr->head;
|
||||
}
|
||||
else // error
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
// !!! 妙啊
|
||||
newnode->next->prev = newnode;
|
||||
newnode->prev->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st *find_(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
{
|
||||
if (0 == cmp(key, cur->data))
|
||||
break;
|
||||
}
|
||||
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
// !!!
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
free(node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
node->next->prev = node->prev;
|
||||
node->prev->next = node->next;
|
||||
|
||||
if (data != NULL)
|
||||
memcpy(data, node->data, ptr->size);
|
||||
|
||||
free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = next)
|
||||
{
|
||||
next = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
38
C11-数据结构/ds/line/list/linklist/double/lib3/llist.h
Normal file
38
C11-数据结构/ds/line/list/linklist/double/lib3/llist.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef LLIST_H__
|
||||
#define LLIST_H__
|
||||
|
||||
#define LLIST_FORWARD 1
|
||||
#define LLIST_BACKWARD 2
|
||||
|
||||
typedef void llist_op(const void *);
|
||||
typedef int llist_cmp(const void *, const void *);
|
||||
|
||||
// !!! 变长结构体
|
||||
struct llist_node_st
|
||||
{
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
// data要放在最后
|
||||
char data[1]; // 占位符,数据的起始。C99才只是[0]
|
||||
};
|
||||
|
||||
typedef struct llist_head
|
||||
{
|
||||
int size;
|
||||
struct llist_node_st head;
|
||||
|
||||
int (*insert)(struct llist_head *, const void *, int);
|
||||
void *(*find)(struct llist_head *, const void *, llist_cmp *);
|
||||
int (*delete)(struct llist_head *, const void *, llist_cmp *);
|
||||
int (*fetch)(struct llist_head *, const void *, llist_cmp *, void *);
|
||||
void (*travel)(struct llist_head *, llist_op *);
|
||||
|
||||
} LLIST;
|
||||
|
||||
|
||||
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#endif
|
||||
97
C11-数据结构/ds/line/list/linklist/double/lib3/main.c
Normal file
97
C11-数据结构/ds/line/list/linklist/double/lib3/main.c
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
void print_s(const void *record)
|
||||
{
|
||||
const struct score_st *r = record;
|
||||
|
||||
printf("%d %s %d %d\n", r->id, r->name, r->math, r->chinese);
|
||||
}
|
||||
|
||||
int id_cmp(const void *key, const void *record)
|
||||
{
|
||||
const int *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return (*k - r->id);
|
||||
}
|
||||
|
||||
int name_cmp(const void *key, const void *record)
|
||||
{
|
||||
const char *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return strcmp(k, r->name);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
LLIST *handler;
|
||||
int i, ret;
|
||||
struct score_st tmp;
|
||||
|
||||
handler = llist_create(sizeof(struct score_st));
|
||||
if (NULL == handler)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "std%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
ret = handler->insert(handler, &tmp, LLIST_FORWARD);
|
||||
if (ret)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
handler->travel(handler, print_s);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
printf("\n\n");
|
||||
|
||||
// int id = 3;
|
||||
char *del_name = "std6";
|
||||
|
||||
// ret = llist_delete(handler, &id, id_cmp);
|
||||
ret = llist_delete(handler, del_name, name_cmp);
|
||||
if (ret)
|
||||
printf("llist_deleter failed!\n");
|
||||
|
||||
llist_travel(handler, print_s);
|
||||
|
||||
|
||||
|
||||
int id = 3;
|
||||
struct score_st *data;
|
||||
|
||||
data = llist_find(handler, &id, id_cmp);
|
||||
if (NULL == data)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
print_s(data);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
llist_destroy(handler);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
31
C11-数据结构/ds/line/list/linklist/double/lib4/Makefile
Normal file
31
C11-数据结构/ds/line/list/linklist/double/lib4/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
153
C11-数据结构/ds/line/list/linklist/double/lib4/llist.c
Normal file
153
C11-数据结构/ds/line/list/linklist/double/lib4/llist.c
Normal file
@@ -0,0 +1,153 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
// !!! 变长结构体
|
||||
struct llist_node_st
|
||||
{
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
// data要放在最后
|
||||
char data[1]; // 占位符,数据的起始。C99才只是[0]
|
||||
};
|
||||
|
||||
struct llist_head_st
|
||||
{
|
||||
int size;
|
||||
struct llist_node_st head;
|
||||
};
|
||||
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
struct llist_head_st *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->size = initsize;
|
||||
new->head.prev = &new->head;
|
||||
new->head.next = &new->head;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST *p, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st *newnode;
|
||||
struct llist_head_st *ptr = p;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
return -1;
|
||||
|
||||
memcpy(newnode->data, data, ptr->size);
|
||||
|
||||
if (LLIST_FORWARD == mode)
|
||||
{
|
||||
newnode->prev = &ptr->head;
|
||||
newnode->next = ptr->head.next;
|
||||
}
|
||||
else if (LLIST_BACKWARD == mode)
|
||||
{
|
||||
newnode->prev = ptr->head.prev;
|
||||
newnode->next = &ptr->head;
|
||||
}
|
||||
else // error
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
// !!! 妙啊
|
||||
newnode->next->prev = newnode;
|
||||
newnode->prev->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st *find_(struct llist_head_st *ptr,
|
||||
const void *key,
|
||||
llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
{
|
||||
if (0 == cmp(key, cur->data))
|
||||
break;
|
||||
}
|
||||
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void *llist_find(LLIST *p, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST *p, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
// !!!
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
free(node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST *p, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
node->next->prev = node->prev;
|
||||
node->prev->next = node->next;
|
||||
|
||||
if (data != NULL)
|
||||
memcpy(data, node->data, ptr->size);
|
||||
|
||||
free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST *p, llist_op *op)
|
||||
{
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST *p)
|
||||
{
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = next)
|
||||
{
|
||||
next = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
27
C11-数据结构/ds/line/list/linklist/double/lib4/llist.h
Normal file
27
C11-数据结构/ds/line/list/linklist/double/lib4/llist.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef LLIST_H__
|
||||
#define LLIST_H__
|
||||
|
||||
#define LLIST_FORWARD 1
|
||||
#define LLIST_BACKWARD 2
|
||||
|
||||
// !!! 实现了隐藏 LLIST 的实现方法
|
||||
typedef void LLIST;
|
||||
|
||||
typedef void llist_op(const void *);
|
||||
typedef int llist_cmp(const void *, const void *);
|
||||
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
int llist_insert(LLIST *, const void *data, int mode);
|
||||
|
||||
void *llist_find(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_delete(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_fetch(LLIST *, const void *key, llist_cmp *, void *data);
|
||||
|
||||
void llist_travel(LLIST *, llist_op *);
|
||||
|
||||
void llist_destroy(LLIST *);
|
||||
|
||||
#endif
|
||||
95
C11-数据结构/ds/line/list/linklist/double/lib4/main.c
Normal file
95
C11-数据结构/ds/line/list/linklist/double/lib4/main.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
void print_s(const void *record)
|
||||
{
|
||||
const struct score_st *r = record;
|
||||
|
||||
printf("%d %s %d %d\n", r->id, r->name, r->math, r->chinese);
|
||||
}
|
||||
|
||||
int id_cmp(const void *key, const void *record)
|
||||
{
|
||||
const int *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return (*k - r->id);
|
||||
}
|
||||
|
||||
int name_cmp(const void *key, const void *record)
|
||||
{
|
||||
const char *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return strcmp(k, r->name);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
LLIST *handler;
|
||||
int i, ret;
|
||||
struct score_st tmp;
|
||||
|
||||
handler = llist_create(sizeof(struct score_st));
|
||||
if (NULL == handler)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "std%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
ret = llist_insert(handler, &tmp, LLIST_FORWARD);
|
||||
if (ret)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
llist_travel(handler, print_s);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
// int id = 3;
|
||||
char *del_name = "std6";
|
||||
|
||||
// ret = llist_delete(handler, &id, id_cmp);
|
||||
ret = llist_delete(handler, del_name, name_cmp);
|
||||
if (ret)
|
||||
printf("llist_deleter failed!\n");
|
||||
|
||||
llist_travel(handler, print_s);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
int id = 3;
|
||||
struct score_st *data;
|
||||
|
||||
data = llist_find(handler, &id, id_cmp);
|
||||
if (NULL == data)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
print_s(data);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
llist_destroy(handler);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
156
C11-数据结构/ds/line/list/linklist/simple/head/list.c
Normal file
156
C11-数据结构/ds/line/list/linklist/simple/head/list.c
Normal file
@@ -0,0 +1,156 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
list *list_create()
|
||||
{
|
||||
list *me;
|
||||
me = malloc(sizeof(*me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
|
||||
me->next = NULL;
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
// 从第一个有效节点作为第0个
|
||||
int list_insert_at(list *me, int i, datatype *data)
|
||||
{
|
||||
int k = 0;
|
||||
list *node = me, *newnode;
|
||||
|
||||
if (i < 0)
|
||||
return -1;
|
||||
// return -EINVAL;
|
||||
|
||||
while (k < i && node != NULL)
|
||||
{
|
||||
node = node->next;
|
||||
k++;
|
||||
}
|
||||
|
||||
if (NULL != node)
|
||||
{
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
return -2;
|
||||
|
||||
newnode->data = *data;
|
||||
newnode->next = NULL;
|
||||
|
||||
newnode->next = node->next;
|
||||
node->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return -3;
|
||||
}
|
||||
|
||||
// 有序插入
|
||||
int list_order_insert(list *me, datatype *data)
|
||||
{
|
||||
list *p = me, *q;
|
||||
|
||||
while (p->next && p->next->data < *data)
|
||||
p = p->next;
|
||||
|
||||
q = malloc(sizeof(*q));
|
||||
if (NULL == q)
|
||||
return -1;
|
||||
|
||||
q->data = *data;
|
||||
q->next = p->next;
|
||||
p->next = q;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int list_delete_at(list *me, int i, datatype *data)
|
||||
{
|
||||
int k = 0;
|
||||
list *p = me, *q;
|
||||
|
||||
*data = -1;
|
||||
|
||||
if (i < 0)
|
||||
return -1;
|
||||
|
||||
while (k < i)
|
||||
{
|
||||
p = p->next;
|
||||
k++;
|
||||
}
|
||||
|
||||
if (p)
|
||||
{
|
||||
q = p->next;
|
||||
p->next = q->next;
|
||||
*data = q->data;
|
||||
free(q);
|
||||
q = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return -2;
|
||||
}
|
||||
|
||||
int list_delete(list *me, datatype *data)
|
||||
{
|
||||
list *p = me, *q;
|
||||
while (p->next && p->next->data != *data)
|
||||
p = p->next;
|
||||
|
||||
if (NULL == p->next)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
q = p->next;
|
||||
p->next = q->next;
|
||||
free(q);
|
||||
q = NULL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int list_isempty(list *me)
|
||||
{
|
||||
if (NULL == me->next)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void list_display(list *me)
|
||||
{
|
||||
list *node = me->next;
|
||||
if (0 == list_isempty(me))
|
||||
return;
|
||||
|
||||
while (NULL != node)
|
||||
{
|
||||
printf("%d ", node->data);
|
||||
node = node->next;
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void list_destroy(list *me)
|
||||
{
|
||||
list *node, *next;
|
||||
|
||||
for (node = me->next; node != NULL; node = next)
|
||||
{
|
||||
next = node->next;
|
||||
free(node);
|
||||
}
|
||||
|
||||
free(me);
|
||||
|
||||
return;
|
||||
}
|
||||
26
C11-数据结构/ds/line/list/linklist/simple/head/list.h
Normal file
26
C11-数据结构/ds/line/list/linklist/simple/head/list.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef LIST_H__
|
||||
#define LIST_H__
|
||||
|
||||
typedef int datatype;
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
datatype data;
|
||||
struct node_st *next;
|
||||
}list;
|
||||
|
||||
list *list_create();
|
||||
|
||||
int list_insert_at(list *me, int i, datatype *data);
|
||||
int list_order_insert(list *me, datatype *data);
|
||||
|
||||
int list_delete_at(list *me, int i, datatype *data);
|
||||
int list_delete(list *me, datatype *data);
|
||||
|
||||
int list_isempty(list *me);
|
||||
|
||||
void list_display(list *me);
|
||||
void list_destroy(list *me);
|
||||
|
||||
|
||||
#endif
|
||||
44
C11-数据结构/ds/line/list/linklist/simple/head/main.c
Normal file
44
C11-数据结构/ds/line/list/linklist/simple/head/main.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
list *l;
|
||||
datatype arr[] = {12, 9, 23, 2, 34, 6, 45};
|
||||
|
||||
l = list_create();
|
||||
if (NULL == l)
|
||||
exit(1);
|
||||
|
||||
for (int i = 0; i < sizeof(arr) / sizeof(*arr); i++)
|
||||
{
|
||||
// if (list_insert_at(l, 0, &arr[i]))
|
||||
if (list_order_insert(l, &arr[i]))
|
||||
exit(1);
|
||||
}
|
||||
|
||||
list_display(l);
|
||||
|
||||
datatype value;
|
||||
int err;
|
||||
|
||||
err = list_delete_at(l, 2, &value);
|
||||
if (err)
|
||||
exit(1);
|
||||
list_display(l);
|
||||
printf("delete:%d\n", value);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
int value = 12;
|
||||
list_delete(l, &value);
|
||||
list_display(l);
|
||||
#endif
|
||||
|
||||
list_destroy(l);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
7
C11-数据结构/ds/line/list/linklist/simple/head/makefile
Normal file
7
C11-数据结构/ds/line/list/linklist/simple/head/makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
all:main
|
||||
|
||||
main:main.o list.o
|
||||
$(CC) $^ -o $@
|
||||
|
||||
clean:
|
||||
rm *.o main -rf
|
||||
117
C11-数据结构/ds/line/list/linklist/simple/josephu/jose.c
Normal file
117
C11-数据结构/ds/line/list/linklist/simple/josephu/jose.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define JOSE_NUM 8
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
int data;
|
||||
struct node_st *next;
|
||||
} list;
|
||||
|
||||
// try something
|
||||
#if 0
|
||||
list *jose_create(int a[], int n)
|
||||
{
|
||||
list *me, *p;
|
||||
me = malloc(sizeof(*me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
me->data = me;
|
||||
|
||||
p = malloc(sizeof(*p));
|
||||
p = me;
|
||||
p->data = a[i];
|
||||
me->next = p;
|
||||
}
|
||||
|
||||
return me;
|
||||
}
|
||||
#endif
|
||||
|
||||
list *jose_create(int n)
|
||||
{
|
||||
list *me, *newnode, *cur;
|
||||
int i = 1;
|
||||
|
||||
me = malloc(sizeof(*me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
me->data = i;
|
||||
me->next = me;
|
||||
i++;
|
||||
|
||||
cur = me;
|
||||
|
||||
for (; i <= n; i++)
|
||||
{
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
return NULL;
|
||||
newnode->data = i;
|
||||
newnode->next = me;
|
||||
|
||||
cur->next = newnode;
|
||||
cur = newnode;
|
||||
}
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
void jose_show(list *me)
|
||||
{
|
||||
list *list;
|
||||
|
||||
for (list = me; list->next != me; list = list->next)
|
||||
{
|
||||
// sleep(1);
|
||||
printf("%d ", list->data);
|
||||
// fflush(NULL);
|
||||
}
|
||||
printf("%d\n", list->data);
|
||||
}
|
||||
|
||||
void jose_kill(list **me, int n)
|
||||
{
|
||||
list *cur = *me, *node;
|
||||
int i = 1;
|
||||
|
||||
while (cur != cur->next)
|
||||
{
|
||||
while (i < n)
|
||||
{
|
||||
node = cur;
|
||||
cur = cur->next;
|
||||
i++;
|
||||
}
|
||||
|
||||
printf("%d ", cur->data);
|
||||
node->next = cur->next;
|
||||
free(cur);
|
||||
|
||||
cur = node->next;
|
||||
i = 1;
|
||||
}
|
||||
|
||||
*me = cur;
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
list *list;
|
||||
|
||||
list = jose_create(JOSE_NUM);
|
||||
|
||||
jose_show(list);
|
||||
|
||||
jose_kill(&list, 3);
|
||||
|
||||
jose_show(list);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
48
C11-数据结构/ds/line/list/linklist/simple/nohead/main.c
Normal file
48
C11-数据结构/ds/line/list/linklist/simple/nohead/main.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nohead.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, ret;
|
||||
struct node_st *list = NULL;
|
||||
struct score_st tmp;
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "stu%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
// list = list_insert(list, &tmp);
|
||||
ret = list_insert(&list, &tmp);
|
||||
if (0 != ret)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
list_show(list);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
int id = 13;
|
||||
struct score_st *ptr;
|
||||
ptr = list_find(&list, id);
|
||||
if (NULL == ptr)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
printf("%d %s %d %d", ptr->id, ptr->name, ptr->math, ptr->chinese);
|
||||
|
||||
|
||||
#if 0
|
||||
list_delete(&list);
|
||||
|
||||
list_show(list);
|
||||
#endif
|
||||
|
||||
list_destroy(&list);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
7
C11-数据结构/ds/line/list/linklist/simple/nohead/makefile
Normal file
7
C11-数据结构/ds/line/list/linklist/simple/nohead/makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
all:main
|
||||
|
||||
main:main.o nohead.o
|
||||
$(CC) $^ -o $@
|
||||
|
||||
clean:
|
||||
rm *.o main -rf
|
||||
106
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.c
Normal file
106
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nohead.h"
|
||||
|
||||
|
||||
#if 0
|
||||
struct node_st *list_insert(struct node_st *list, struct score_st *data)
|
||||
{
|
||||
struct node_st *new;
|
||||
|
||||
new = malloc(sizeof(*new));
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->data = *data;
|
||||
new->next = list;
|
||||
list = new;
|
||||
|
||||
return list;
|
||||
}
|
||||
#endif
|
||||
|
||||
int list_insert(struct node_st **list, struct score_st *data)
|
||||
{
|
||||
struct node_st *new;
|
||||
|
||||
new = malloc(sizeof(*new));
|
||||
if (NULL == new)
|
||||
return -1;
|
||||
|
||||
new->data = *data;
|
||||
new->next = *list;
|
||||
*list = new;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void list_show(struct node_st *list)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
for (cur = list; NULL != cur; cur = cur->next)
|
||||
{
|
||||
printf("%d %s %d %d\n",
|
||||
cur->data.id,
|
||||
cur->data.name,
|
||||
cur->data.math,
|
||||
cur->data.chinese);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除首位
|
||||
int list_delete(struct node_st **list)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
if (NULL == *list)
|
||||
return -1;
|
||||
|
||||
cur = *list;
|
||||
*list = (*list)->next;
|
||||
|
||||
free(cur);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct score_st *list_find(struct node_st **list, int id)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
if (NULL == *list)
|
||||
return NULL;
|
||||
|
||||
// for (cur = *list; NULL != cur->next; cur = cur->next)
|
||||
// !!! 是cur不为空
|
||||
for (cur = *list; NULL != cur; cur = cur->next)
|
||||
{
|
||||
if (id == cur->data.id)
|
||||
{
|
||||
// printf("%d %s %d %d\n",
|
||||
// cur->data.id,
|
||||
// cur->data.name,
|
||||
// cur->data.math,
|
||||
// cur->data.chinese);
|
||||
return &(cur->data);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int list_destroy(struct node_st **list)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
if (NULL == list)
|
||||
return -1;
|
||||
|
||||
for (cur = *list; NULL != cur; cur = *list)
|
||||
{
|
||||
*list = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
}
|
||||
26
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.h
Normal file
26
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
struct node_st
|
||||
{
|
||||
struct score_st data;
|
||||
struct node_st *next;
|
||||
};
|
||||
|
||||
// struct node_st *list_insert(struct node_st *list, struct score_st *data);
|
||||
int list_insert(struct node_st **list, struct score_st *data);
|
||||
|
||||
void list_show(struct node_st *list);
|
||||
|
||||
int list_delete(struct node_st **list);
|
||||
|
||||
struct score_st *list_find(struct node_st **list, int id);
|
||||
|
||||
int list_destroy(struct node_st **list);
|
||||
114
C11-数据结构/ds/line/list/linklist/simple/polynomial/poly.c
Normal file
114
C11-数据结构/ds/line/list/linklist/simple/polynomial/poly.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
int coef;
|
||||
int exp;
|
||||
struct node_st *next;
|
||||
} list;
|
||||
|
||||
list *poly_create(int a[][2], int n)
|
||||
{
|
||||
list *me, *newnode, *cur;
|
||||
|
||||
me = malloc(sizeof(me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
me->next = NULL;
|
||||
|
||||
cur = me;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
return NULL;
|
||||
|
||||
newnode->coef = a[i][0];
|
||||
newnode->exp = a[i][1];
|
||||
newnode->next = NULL;
|
||||
|
||||
cur->next = newnode;
|
||||
cur = newnode;
|
||||
}
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
void poly_show(list *me)
|
||||
{
|
||||
list *cur;
|
||||
|
||||
for (cur = me->next; NULL != cur; cur = cur->next)
|
||||
{
|
||||
printf("(%d %d) ", cur->coef, cur->exp);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void poly_union(list *p1, list *p2)
|
||||
{
|
||||
list *p, *q, *r;
|
||||
|
||||
p = p1->next;
|
||||
q = p2->next;
|
||||
r = p1;
|
||||
|
||||
while (p && q)
|
||||
{
|
||||
if (p->exp < q->exp)
|
||||
{
|
||||
r->next = p;
|
||||
r = p;
|
||||
p = p->next;
|
||||
}
|
||||
else if (p->exp > q->exp)
|
||||
{
|
||||
r->next = q;
|
||||
r = q;
|
||||
q = q->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->coef += q->coef;
|
||||
if (p->coef)
|
||||
{
|
||||
r->next = p;
|
||||
r = p;
|
||||
}
|
||||
|
||||
p = p->next;
|
||||
q = q->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (NULL == p)
|
||||
r->next = q;
|
||||
else
|
||||
r->next = p;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[][2] = {{5, 0}, {2, 1}, {8, 8}, {3, 16}};
|
||||
int b[][2] = {{6, 1}, {16, 6}, {-8, 8}};
|
||||
|
||||
list *p1, *p2;
|
||||
|
||||
p1 = poly_create(a, 4);
|
||||
if (NULL == p1)
|
||||
exit(1);
|
||||
|
||||
p2 = poly_create(b, 3);
|
||||
if (NULL == p2)
|
||||
exit(1);
|
||||
|
||||
poly_show(p1);
|
||||
poly_show(p2);
|
||||
|
||||
poly_union(p1, p2);
|
||||
poly_show(p1);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
31
C11-数据结构/ds/line/squeue/arr/Makefile
Normal file
31
C11-数据结构/ds/line/squeue/arr/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
39
C11-数据结构/ds/line/squeue/arr/main.c
Normal file
39
C11-数据结构/ds/line/squeue/arr/main.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
queue *sq;
|
||||
datatype arr[] = {2, 34, 89, 12};
|
||||
int i;
|
||||
|
||||
sq = qu_create();
|
||||
if (NULL == sq)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < sizeof(arr) / sizeof(*arr); i++)
|
||||
qu_enqueue(sq, &arr[i]);
|
||||
|
||||
qu_travel(sq);
|
||||
|
||||
datatype tmp;
|
||||
qu_dequeue(sq, &tmp);
|
||||
printf("DEQUEUE:%d\n", tmp);
|
||||
|
||||
#if 0
|
||||
datatype tmp = 100;
|
||||
int ret;
|
||||
|
||||
ret = qu_enqueue(sq, &tmp);
|
||||
if (-1 == ret)
|
||||
printf("Queue is full!\n");
|
||||
else
|
||||
qu_travel(sq);
|
||||
#endif
|
||||
|
||||
qu_destroy(sq);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
72
C11-数据结构/ds/line/squeue/arr/queue.c
Normal file
72
C11-数据结构/ds/line/squeue/arr/queue.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
|
||||
queue *qu_create()
|
||||
{
|
||||
queue *sq;
|
||||
|
||||
sq = malloc(sizeof(*sq));
|
||||
if (NULL == sq)
|
||||
return NULL;
|
||||
|
||||
sq->head = 0;
|
||||
sq->tail = 0;
|
||||
|
||||
return sq;
|
||||
}
|
||||
|
||||
int qu_isempty(queue *sq)
|
||||
{
|
||||
return (sq->head == sq->tail);
|
||||
}
|
||||
|
||||
int qu_enqueue(queue *sq, datatype *x)
|
||||
{
|
||||
if ((sq->tail + 1) % MAXSIZE == sq->head)
|
||||
return -1;
|
||||
|
||||
sq->tail = (sq->tail + 1) % MAXSIZE;
|
||||
sq->data[sq->tail] = *x;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int qu_dequeue(queue *sq, datatype *x)
|
||||
{
|
||||
if (qu_isempty(sq))
|
||||
return -1;
|
||||
|
||||
sq->head = (sq->head + 1) % MAXSIZE;
|
||||
*x = sq->data[sq->head];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void qu_travel(queue *sq)
|
||||
{
|
||||
if (sq->head == sq->tail)
|
||||
return;
|
||||
|
||||
int i;
|
||||
|
||||
i = (sq->head + 1) % MAXSIZE;
|
||||
while (i != sq->tail)
|
||||
{
|
||||
printf("%d ", sq->data[i]);
|
||||
i = (i + 1) % MAXSIZE;
|
||||
}
|
||||
printf("%d \n", sq->data[i]);
|
||||
}
|
||||
|
||||
void qu_clear(queue *sq)
|
||||
{
|
||||
sq->head = sq->tail;
|
||||
}
|
||||
|
||||
void qu_destroy(queue *sq)
|
||||
{
|
||||
free(sq);
|
||||
}
|
||||
29
C11-数据结构/ds/line/squeue/arr/queue.h
Normal file
29
C11-数据结构/ds/line/squeue/arr/queue.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef QUEUE_H__
|
||||
#define QUEUE_H__
|
||||
|
||||
#define MAXSIZE 5
|
||||
|
||||
typedef int datatype;
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
datatype data[MAXSIZE];
|
||||
int head, tail;
|
||||
} queue;
|
||||
|
||||
|
||||
queue *qu_create();
|
||||
|
||||
int qu_isempty();
|
||||
|
||||
int qu_enqueue(queue *, datatype *);
|
||||
|
||||
int qu_dequeue(queue *, datatype *);
|
||||
|
||||
void qu_travel(queue *);
|
||||
|
||||
void qu_clear(queue *);
|
||||
|
||||
void qu_destroy(queue *);
|
||||
|
||||
#endif
|
||||
31
C11-数据结构/ds/line/squeue/list/Makefile
Normal file
31
C11-数据结构/ds/line/squeue/list/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
130
C11-数据结构/ds/line/squeue/list/llist.c
Normal file
130
C11-数据结构/ds/line/squeue/list/llist.c
Normal file
@@ -0,0 +1,130 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->size = initsize;
|
||||
new->head.prev = &new->head;
|
||||
new->head.next = &new->head;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
return -1;
|
||||
|
||||
memcpy(newnode->data, data, ptr->size);
|
||||
|
||||
if (LLIST_FORWARD == mode)
|
||||
{
|
||||
newnode->prev = &ptr->head;
|
||||
newnode->next = ptr->head.next;
|
||||
}
|
||||
else if (LLIST_BACKWARD == mode)
|
||||
{
|
||||
newnode->prev = ptr->head.prev;
|
||||
newnode->next = &ptr->head;
|
||||
}
|
||||
else // error
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
// !!! 妙啊
|
||||
newnode->next->prev = newnode;
|
||||
newnode->prev->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st *find_(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
{
|
||||
if (0 == cmp(key, cur->data))
|
||||
break;
|
||||
}
|
||||
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
// !!!
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
free(node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
node->next->prev = node->prev;
|
||||
node->prev->next = node->next;
|
||||
|
||||
if (data != NULL)
|
||||
memcpy(data, node->data, ptr->size);
|
||||
|
||||
free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = next)
|
||||
{
|
||||
next = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
39
C11-数据结构/ds/line/squeue/list/llist.h
Normal file
39
C11-数据结构/ds/line/squeue/list/llist.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef LLIST_H__
|
||||
#define LLIST_H__
|
||||
|
||||
#define LLIST_FORWARD 1
|
||||
#define LLIST_BACKWARD 2
|
||||
|
||||
// !!! 变长结构体
|
||||
struct llist_node_st
|
||||
{
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
// data要放在最后
|
||||
char data[1]; // 占位符,数据的起始。C99才只是[0]
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int size;
|
||||
struct llist_node_st head;
|
||||
} LLIST;
|
||||
|
||||
typedef void llist_op(const void *);
|
||||
typedef int llist_cmp(const void *, const void *);
|
||||
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *);
|
||||
|
||||
int llist_delete(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_fetch(LLIST *, const void *key, llist_cmp *, void *data);
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#endif
|
||||
56
C11-数据结构/ds/line/squeue/list/main.c
Normal file
56
C11-数据结构/ds/line/squeue/list/main.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
static int print_s(void *record)
|
||||
{
|
||||
struct score_st *r = record;
|
||||
|
||||
printf("%d %s %d %d\n", r->id, r->name, r->math, r->chinese);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
QUEUE *qu;
|
||||
struct score_st tmp;
|
||||
int i, ret;
|
||||
|
||||
qu = queue_create(sizeof(struct score_st));
|
||||
if (NULL == qu)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "stu%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
if (0 != queue_en(qu, &tmp))
|
||||
break;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
ret = queue_de(qu, &tmp);
|
||||
if (-1 == ret)
|
||||
break;
|
||||
|
||||
print_s(&tmp);
|
||||
}
|
||||
|
||||
queue_destroy(qu);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
26
C11-数据结构/ds/line/squeue/list/queue.c
Normal file
26
C11-数据结构/ds/line/squeue/list/queue.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "queue.h"
|
||||
|
||||
QUEUE *queue_create(int size)
|
||||
{
|
||||
return llist_create(size);
|
||||
}
|
||||
|
||||
int queue_en(QUEUE *ptr, const void *data)
|
||||
{
|
||||
llist_insert(ptr, data, LLIST_BACKWARD);
|
||||
}
|
||||
|
||||
static int always_match(const void *p1, const void *p2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int queue_de(QUEUE *ptr, void *data)
|
||||
{
|
||||
return llist_fetch(ptr, (void *)0, always_match, data);
|
||||
}
|
||||
|
||||
void queue_destroy(QUEUE *ptr)
|
||||
{
|
||||
llist_destroy(ptr);
|
||||
}
|
||||
16
C11-数据结构/ds/line/squeue/list/queue.h
Normal file
16
C11-数据结构/ds/line/squeue/list/queue.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef QUEUE_H__
|
||||
#define QUEUE_H__
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
typedef LLIST QUEUE;
|
||||
|
||||
QUEUE *queue_create(int);
|
||||
|
||||
int queue_en(QUEUE *, const void *);
|
||||
|
||||
int queue_de(QUEUE *, void *);
|
||||
|
||||
void queue_destroy(QUEUE *);
|
||||
|
||||
#endif
|
||||
31
C11-数据结构/ds/line/stack/arr/Makefile
Normal file
31
C11-数据结构/ds/line/stack/arr/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
41
C11-数据结构/ds/line/stack/arr/main.c
Normal file
41
C11-数据结构/ds/line/stack/arr/main.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sqstack.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
datatype arr[] = {19, 23, 0, 45, 67};
|
||||
sqstack *st;
|
||||
int i;
|
||||
|
||||
st = st_create();
|
||||
if (NULL == st)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < sizeof(arr) / sizeof(*arr); i++)
|
||||
st_push(st, &arr[i]);
|
||||
|
||||
st_travel(st);
|
||||
|
||||
datatype tmp;
|
||||
while (0 == st_pop(st, &tmp))
|
||||
{
|
||||
printf("POP:%d\n", tmp);
|
||||
}
|
||||
|
||||
#if 0
|
||||
datatype tmp = 1;
|
||||
int ret;
|
||||
|
||||
ret = st_push(st, &tmp);
|
||||
if (0 != ret)
|
||||
printf("st_push failed.\n");
|
||||
else
|
||||
st_travel(st);
|
||||
|
||||
st_destroy(st);
|
||||
#endif
|
||||
|
||||
exit(0);
|
||||
}
|
||||
72
C11-数据结构/ds/line/stack/arr/sqstack.c
Normal file
72
C11-数据结构/ds/line/stack/arr/sqstack.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sqstack.h"
|
||||
|
||||
sqstack *st_create(void)
|
||||
{
|
||||
sqstack *st;
|
||||
|
||||
st = malloc(sizeof(*st));
|
||||
if (NULL == st)
|
||||
return NULL;
|
||||
|
||||
st->top = -1;
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
int st_isempty(sqstack *st)
|
||||
{
|
||||
return (-1 == st->top);
|
||||
}
|
||||
|
||||
int st_push(sqstack *st, datatype *data)
|
||||
{
|
||||
if (MAXSIZE - 1 == st->top)
|
||||
return -1;
|
||||
|
||||
st->data[++st->top] = *data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 取出栈顶元素 */
|
||||
int st_pop(sqstack *st, datatype *data)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return -1;
|
||||
|
||||
*data = st->data[st->top--];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* 查看栈顶元素 */
|
||||
int st_top(sqstack *st, datatype *data)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return -1;
|
||||
|
||||
*data = st->data[st->top];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void st_travel(sqstack *st)
|
||||
{
|
||||
if (st_isempty(st))
|
||||
return;
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i <= st->top; i++)
|
||||
printf("%d ", st->data[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void st_destroy(sqstack *st)
|
||||
{
|
||||
free(st);
|
||||
}
|
||||
30
C11-数据结构/ds/line/stack/arr/sqstack.h
Normal file
30
C11-数据结构/ds/line/stack/arr/sqstack.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef SQSTACK_H__
|
||||
#define SQSTACK_H__
|
||||
|
||||
#define MAXSIZE 5
|
||||
|
||||
typedef int datatype;
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
datatype data[MAXSIZE];
|
||||
int top;
|
||||
} sqstack;
|
||||
|
||||
sqstack *st_create(void);
|
||||
|
||||
int st_isempty(sqstack *);
|
||||
|
||||
int st_push(sqstack *, datatype *);
|
||||
|
||||
/* 取出栈顶元素 */
|
||||
int st_pop(sqstack *, datatype *);
|
||||
|
||||
/* 查看栈顶元素 */
|
||||
int st_top(sqstack *, datatype *);
|
||||
|
||||
void st_travel(sqstack *);
|
||||
|
||||
void st_destroy(sqstack *);
|
||||
|
||||
#endif
|
||||
31
C11-数据结构/ds/line/stack/list/Makefile
Normal file
31
C11-数据结构/ds/line/stack/list/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) -o $@ $^
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
130
C11-数据结构/ds/line/stack/list/llist.c
Normal file
130
C11-数据结构/ds/line/stack/list/llist.c
Normal file
@@ -0,0 +1,130 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->size = initsize;
|
||||
new->head.prev = &new->head;
|
||||
new->head.next = &new->head;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
return -1;
|
||||
|
||||
memcpy(newnode->data, data, ptr->size);
|
||||
|
||||
if (LLIST_FORWARD == mode)
|
||||
{
|
||||
newnode->prev = &ptr->head;
|
||||
newnode->next = ptr->head.next;
|
||||
}
|
||||
else if (LLIST_BACKWARD == mode)
|
||||
{
|
||||
newnode->prev = ptr->head.prev;
|
||||
newnode->next = &ptr->head;
|
||||
}
|
||||
else // error
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
// !!! 妙啊
|
||||
newnode->next->prev = newnode;
|
||||
newnode->prev->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st *find_(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
{
|
||||
if (0 == cmp(key, cur->data))
|
||||
break;
|
||||
}
|
||||
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
// !!!
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
free(node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return -1;
|
||||
|
||||
node->next->prev = node->prev;
|
||||
node->prev->next = node->next;
|
||||
|
||||
if (data != NULL)
|
||||
memcpy(data, node->data, ptr->size);
|
||||
|
||||
free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = next)
|
||||
{
|
||||
next = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
39
C11-数据结构/ds/line/stack/list/llist.h
Normal file
39
C11-数据结构/ds/line/stack/list/llist.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef LLIST_H__
|
||||
#define LLIST_H__
|
||||
|
||||
#define LLIST_FORWARD 1
|
||||
#define LLIST_BACKWARD 2
|
||||
|
||||
// !!! 变长结构体
|
||||
struct llist_node_st
|
||||
{
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
// data要放在最后
|
||||
char data[1]; // 占位符,数据的起始。C99才只是[0]
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int size;
|
||||
struct llist_node_st head;
|
||||
} LLIST;
|
||||
|
||||
typedef void llist_op(const void *);
|
||||
typedef int llist_cmp(const void *, const void *);
|
||||
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *);
|
||||
|
||||
int llist_delete(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_fetch(LLIST *, const void *key, llist_cmp *, void *data);
|
||||
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#endif
|
||||
55
C11-数据结构/ds/line/stack/list/main.c
Normal file
55
C11-数据结构/ds/line/stack/list/main.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stack.h>
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
static int print_s(void *record)
|
||||
{
|
||||
struct score_st *r = record;
|
||||
|
||||
printf("%d %s %d %d\n", r->id, r->name, r->math, r->chinese);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
STACK *st;
|
||||
struct score_st tmp;
|
||||
int i, ret;
|
||||
|
||||
st = stack_create(sizeof(struct score_st));
|
||||
if (NULL == st)
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "stu%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
if (stack_push(st, &tmp))
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
ret = stack_pop(st, &tmp);
|
||||
if (-1 == ret)
|
||||
break;
|
||||
print_s(&tmp);
|
||||
}
|
||||
|
||||
stack_destroy(st);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
29
C11-数据结构/ds/line/stack/list/stack.c
Normal file
29
C11-数据结构/ds/line/stack/list/stack.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
LLIST *stack_create(int initsize)
|
||||
{
|
||||
return llist_create(initsize);
|
||||
}
|
||||
|
||||
int stack_push(STACK *ptr, const void *data)
|
||||
{
|
||||
return llist_insert(ptr, data, LLIST_FORWARD);
|
||||
}
|
||||
|
||||
static int always_match(const void *p1, const void *p2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stack_pop(STACK *ptr, void *data)
|
||||
{
|
||||
// !!! 假接口
|
||||
return llist_fetch(ptr, (void *)0, always_match, data);
|
||||
}
|
||||
|
||||
void stack_destroy(STACK *ptr)
|
||||
{
|
||||
llist_destroy(ptr);
|
||||
}
|
||||
16
C11-数据结构/ds/line/stack/list/stack.h
Normal file
16
C11-数据结构/ds/line/stack/list/stack.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef STACK_H__
|
||||
#define STACK_H__
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
typedef LLIST STACK;
|
||||
|
||||
STACK *stack_create(int);
|
||||
|
||||
int stack_push(STACK *, const void *data);
|
||||
|
||||
int stack_pop(STACK *, void *data);
|
||||
|
||||
void stack_destroy(STACK *);
|
||||
|
||||
#endif
|
||||
35
C11-数据结构/ds/tree/btree/Makefile
Normal file
35
C11-数据结构/ds/tree/btree/Makefile
Normal file
@@ -0,0 +1,35 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
LDFLAGS=-L/usr/local/lib
|
||||
|
||||
LDLIBS=-lqueue -lllist
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
362
C11-数据结构/ds/tree/btree/main.c
Normal file
362
C11-数据结构/ds/tree/btree/main.c
Normal file
@@ -0,0 +1,362 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <queue.h>
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
struct node_st
|
||||
{
|
||||
struct score_st data;
|
||||
struct node_st *l, *r;
|
||||
};
|
||||
|
||||
|
||||
struct node_st *tree = NULL;
|
||||
|
||||
/**
|
||||
* @brief 插入节点
|
||||
* @details
|
||||
* 小左大右
|
||||
*
|
||||
* !!! 为什么要传入二级指针
|
||||
* !!! 因为你可能改变的是根节点自身,不仅仅是改变根节点指向的值。
|
||||
* !!! 要改变一级指针本身,就需要传入二级指针。
|
||||
*
|
||||
* @param root
|
||||
* @param data
|
||||
* @return int
|
||||
*/
|
||||
static int insert(struct node_st **root, struct score_st *data)
|
||||
{
|
||||
struct node_st *node;
|
||||
|
||||
if (NULL == *root)
|
||||
{
|
||||
node = malloc(sizeof(*node));
|
||||
if (NULL == node)
|
||||
return -1;
|
||||
|
||||
node->data = *data;
|
||||
node->l = NULL;
|
||||
node->r = NULL;
|
||||
|
||||
*root = node;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (data->id <= (*root)->data.id)
|
||||
return insert(&(*root)->l, data);
|
||||
return insert(&(*root)->r, data);
|
||||
}
|
||||
|
||||
|
||||
struct score_st *find(struct node_st *root, int id)
|
||||
{
|
||||
if (NULL == root)
|
||||
return NULL;
|
||||
|
||||
if (id == root->data.id)
|
||||
return &root->data;
|
||||
|
||||
if (id < root->data.id)
|
||||
return find(root->l, id);
|
||||
return find(root->r, id);
|
||||
}
|
||||
|
||||
static void print_s(struct score_st *d)
|
||||
{
|
||||
printf("%d %s %d %d\n", d->id, d->name, d->math, d->chinese);
|
||||
}
|
||||
|
||||
|
||||
static void draw_(struct node_st *root, int level)
|
||||
{
|
||||
if (NULL == root)
|
||||
return;
|
||||
|
||||
draw_(root->r, level + 1);
|
||||
|
||||
for (int i = 0; i < level; i++)
|
||||
printf(" ");
|
||||
print_s(&root->data);
|
||||
|
||||
draw_(root->l, level + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 绘制树
|
||||
* @details
|
||||
* 躺下来,根在左,子节点在右。
|
||||
* 这样子,深度即制表符个数。
|
||||
*D
|
||||
* @param root
|
||||
*/
|
||||
static void draw(struct node_st *root)
|
||||
{
|
||||
draw_(root, 0);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
// getchar();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 计算某个节点的节点总个数
|
||||
* @details
|
||||
*
|
||||
* @param root
|
||||
* @return int
|
||||
*/
|
||||
static int get_num(struct node_st *root)
|
||||
{
|
||||
if (NULL == root)
|
||||
return 0;
|
||||
|
||||
return get_num(root->l) + get_num(root->r) + 1;
|
||||
}
|
||||
|
||||
|
||||
static struct node_st *find_min(struct node_st *root)
|
||||
{
|
||||
if (NULL == root->l)
|
||||
return root;
|
||||
|
||||
return find_min(root->l);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 左旋
|
||||
* @details
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void turn_left(struct node_st **root)
|
||||
{
|
||||
struct node_st *cur = *root;
|
||||
|
||||
*root = cur->r;
|
||||
cur->r = NULL;
|
||||
|
||||
find_min(*root)->l = cur;
|
||||
|
||||
// draw(tree);
|
||||
}
|
||||
|
||||
|
||||
static struct node_st *find_max(struct node_st *root)
|
||||
{
|
||||
if (NULL == root->r)
|
||||
return root;
|
||||
|
||||
return find_min(root->r);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 右旋
|
||||
* @details
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void turn_right(struct node_st **root)
|
||||
{
|
||||
struct node_st *cur = *root;
|
||||
|
||||
*root = cur->l;
|
||||
cur->l = NULL;
|
||||
|
||||
find_max(*root)->r = cur;
|
||||
|
||||
// draw(tree);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 平衡
|
||||
* @details
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void balance(struct node_st **root)
|
||||
{
|
||||
if (NULL == *root)
|
||||
return;
|
||||
|
||||
int sub;
|
||||
|
||||
while (1)
|
||||
{
|
||||
sub = get_num((*root)->l) - get_num((*root)->r);
|
||||
|
||||
if (sub >= -1 && sub <= 1)
|
||||
break;
|
||||
if (sub < -1)
|
||||
turn_left(root);
|
||||
if (sub > 1)
|
||||
turn_right(root);
|
||||
}
|
||||
|
||||
balance(&(*root)->l);
|
||||
balance(&(*root)->r);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 删除某个节点
|
||||
* @details
|
||||
* 左子树顶上来
|
||||
*
|
||||
* @param root
|
||||
* @param tmpid
|
||||
*/
|
||||
static void delete(struct node_st **root, int id)
|
||||
{
|
||||
struct node_st **node = root;
|
||||
struct node_st *cur;
|
||||
|
||||
while (*node != NULL && (*node)->data.id != id)
|
||||
{
|
||||
if (id < (*node)->data.id)
|
||||
node = &(*node)->l;
|
||||
else
|
||||
node = &(*node)->r;
|
||||
}
|
||||
|
||||
if (NULL == *node)
|
||||
return;
|
||||
|
||||
cur = *node;
|
||||
|
||||
if (NULL == cur->l)
|
||||
*node = cur->r;
|
||||
else
|
||||
{
|
||||
*node = cur->l;
|
||||
find_max(cur->l)->r = cur->r;
|
||||
}
|
||||
|
||||
free(cur);
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* @brief 前序遍历
|
||||
* @details
|
||||
* 中序和后序改一改输出顺序就行
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void travel(struct node_st *root)
|
||||
{
|
||||
if (NULL == root)
|
||||
return;
|
||||
print_s(&root->data);
|
||||
|
||||
travel(root->l);
|
||||
|
||||
travel(root->r);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @brief 按层遍历
|
||||
* @details
|
||||
* 使用一个队列存储同一层
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void travel(struct node_st *root)
|
||||
{
|
||||
if (NULL == root)
|
||||
return;
|
||||
|
||||
int ret;
|
||||
QUEUE *qu;
|
||||
struct node_st *cur;
|
||||
|
||||
qu = queue_create(sizeof(struct node_st *));
|
||||
if (NULL == qu)
|
||||
return;
|
||||
|
||||
queue_en(qu, &root);
|
||||
/* if error */
|
||||
|
||||
while (1)
|
||||
{
|
||||
ret = queue_de(qu, &cur);
|
||||
if (-1 == ret)
|
||||
break;
|
||||
|
||||
print_s(&cur->data);
|
||||
|
||||
if (NULL != cur->l)
|
||||
queue_en(qu, &cur->l);
|
||||
|
||||
if (NULL != cur->r)
|
||||
queue_en(qu, &cur->r);
|
||||
}
|
||||
|
||||
queue_destroy(qu);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[] = {1, 2, 3, 7, 6, 5, 9, 8, 4};
|
||||
int i;
|
||||
struct score_st tmp, *datap;
|
||||
|
||||
for (i = 0; i < sizeof(arr) / sizeof(*arr); i++)
|
||||
{
|
||||
tmp.id = arr[i];
|
||||
snprintf(tmp.name, NAMESIZE, "stu%d", arr[i]);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
/* 小往左,大往右 */
|
||||
insert(&tree, &tmp);
|
||||
}
|
||||
|
||||
draw(tree);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
balance(&tree);
|
||||
|
||||
draw(tree);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
travel(tree);
|
||||
|
||||
#if 0
|
||||
int tmpid = 5;
|
||||
delete (&tree, tmpid);
|
||||
|
||||
draw(tree);
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
int tmpid = 2;
|
||||
datap = find(tree, tmpid);
|
||||
if (NULL == datap)
|
||||
printf("Can not find the id %d\n", tmpid);
|
||||
else
|
||||
print_s(datap);
|
||||
#endif
|
||||
|
||||
exit(0);
|
||||
}
|
||||
35
C11-数据结构/ds/tree/retrieval/Makefile
Normal file
35
C11-数据结构/ds/tree/retrieval/Makefile
Normal file
@@ -0,0 +1,35 @@
|
||||
# 方便起见一般都会先定义编译器链接器
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
|
||||
# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
|
||||
OBJS = $(patsubst %c, %o, $(SRCS))
|
||||
|
||||
# 可执行文件的名字
|
||||
TARGET = main
|
||||
|
||||
# .PHONE伪目标,具体含义百度一下一大堆介绍
|
||||
.PHONY:all clean
|
||||
|
||||
# 要生成的目标文件
|
||||
all: $(TARGET)
|
||||
|
||||
LDFLAGS=-L/usr/local/lib
|
||||
|
||||
# LDLIBS=-lqueue -lllist
|
||||
|
||||
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
|
||||
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
|
||||
|
||||
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
|
||||
%.o:%.c
|
||||
$(CC) -c $^
|
||||
|
||||
# make clean删除所有.o和目标文件
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
121
C11-数据结构/ds/tree/retrieval/trie.c
Normal file
121
C11-数据结构/ds/tree/retrieval/trie.c
Normal file
@@ -0,0 +1,121 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DESC_SIZE 256
|
||||
#define KEY_SIZE 256
|
||||
#define BUFSIZE 512
|
||||
#define FNAME "log"
|
||||
|
||||
struct node_st
|
||||
{
|
||||
struct node_st *ch[26];
|
||||
char desc[DESC_SIZE];
|
||||
};
|
||||
|
||||
static int get_word(FILE *fp, char *key, char *desc)
|
||||
{
|
||||
char buf[BUFSIZE];
|
||||
char *retp;
|
||||
int i, j;
|
||||
|
||||
retp = fgets(buf, BUFSIZE, fp);
|
||||
if (NULL == retp)
|
||||
return -1;
|
||||
|
||||
for (i = 0; i < KEY_SIZE - 1 && buf[i] != ':'; i++)
|
||||
key[i] = buf[i];
|
||||
key[i] = '\0';
|
||||
|
||||
i++;
|
||||
|
||||
for (j = 0; j < DESC_SIZE - 1 && buf[i] != '\0'; j++, i++)
|
||||
desc[j] = buf[i];
|
||||
desc[j] = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct node_st *newnode()
|
||||
{
|
||||
struct node_st *node;
|
||||
int i;
|
||||
|
||||
node = malloc(sizeof(*node));
|
||||
if (NULL == node)
|
||||
return NULL;
|
||||
|
||||
node->desc[0] = '\0';
|
||||
|
||||
for (i = 0; i < 26; i++)
|
||||
node->ch[i] = NULL;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
static int insert(struct node_st **root, char *key, char *desc)
|
||||
{
|
||||
if (NULL == *root)
|
||||
{
|
||||
*root = newnode();
|
||||
if (NULL == *root)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ('\0' == *key)
|
||||
{
|
||||
strcpy((*root)->desc, desc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return insert((*root)->ch + *key - 'a', key + 1, desc);
|
||||
}
|
||||
|
||||
static char *find(struct node_st *root, char *key)
|
||||
{
|
||||
if (NULL == root)
|
||||
return NULL;
|
||||
|
||||
if ('\0' == *key)
|
||||
return root->desc;
|
||||
|
||||
return find(root->ch[*key - 'a'], key + 1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
struct node_st *tree;
|
||||
FILE *fp;
|
||||
char desc[DESC_SIZE] = {'\0'}, key[KEY_SIZE] = {'\0'};
|
||||
int ret;
|
||||
char *datap;
|
||||
|
||||
fp = fopen(FNAME, "r");
|
||||
if (NULL == fp)
|
||||
{
|
||||
fprintf(stderr, "fopen():error!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
ret = get_word(fp, key, desc);
|
||||
if (-1 == ret)
|
||||
break;
|
||||
|
||||
// puts(key);
|
||||
// puts(desc);
|
||||
|
||||
insert(&tree, key, desc);
|
||||
}
|
||||
|
||||
datap = find(tree, "donkey");
|
||||
if (NULL == datap)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
puts(datap);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
106
C11-数据结构/ds/tree/save_load/load.c
Normal file
106
C11-数据结构/ds/tree/save_load/load.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define FNAME "/tmp/out"
|
||||
|
||||
|
||||
struct node_st
|
||||
{
|
||||
char data;
|
||||
struct node_st *l, *r;
|
||||
};
|
||||
|
||||
|
||||
struct node_st *tree = NULL;
|
||||
|
||||
|
||||
static void draw_(struct node_st *root, int level)
|
||||
{
|
||||
if (NULL == root)
|
||||
return;
|
||||
|
||||
draw_(root->r, level + 1);
|
||||
|
||||
for (int i = 0; i < level; i++)
|
||||
printf(" ");
|
||||
printf("%c\n", root->data);
|
||||
|
||||
draw_(root->l, level + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 绘制树
|
||||
* @details
|
||||
* 躺下来,根在左,子节点在右。
|
||||
* 这样子,深度即制表符个数。
|
||||
*D
|
||||
* @param root
|
||||
*/
|
||||
static void draw(struct node_st *root)
|
||||
{
|
||||
draw_(root, 0);
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
static struct node_st *load_(FILE *fp)
|
||||
{
|
||||
int c;
|
||||
struct node_st *root;
|
||||
|
||||
c = fgetc(fp);
|
||||
/* if error */
|
||||
|
||||
if ('(' != c)
|
||||
{
|
||||
fprintf(stderr, "fgetc():error.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
c = fgetc(fp);
|
||||
if (')' == c)
|
||||
return 0;
|
||||
|
||||
root = malloc(sizeof(*root));
|
||||
if (NULL == root)
|
||||
exit(1);
|
||||
|
||||
root->data = c;
|
||||
root->l = load_(fp);
|
||||
root->r = load_(fp);
|
||||
|
||||
fgetc(fp);
|
||||
/* if error */
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct node_st *load(const char *path)
|
||||
{
|
||||
FILE *fp;
|
||||
struct node_st *root;
|
||||
|
||||
fp = fopen(path, "r");
|
||||
if (NULL == fp)
|
||||
return NULL;
|
||||
|
||||
root = load_(fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
struct node_st *root;
|
||||
|
||||
root = load(FNAME);
|
||||
|
||||
draw(root);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
138
C11-数据结构/ds/tree/save_load/save.c
Normal file
138
C11-数据结构/ds/tree/save_load/save.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define FNAME "/tmp/out"
|
||||
|
||||
|
||||
struct node_st
|
||||
{
|
||||
char data;
|
||||
struct node_st *l, *r;
|
||||
};
|
||||
|
||||
|
||||
struct node_st *tree = NULL;
|
||||
|
||||
/**
|
||||
* @brief 插入节点
|
||||
* @details
|
||||
* 小左大右
|
||||
*
|
||||
* !!! 为什么要传入二级指针
|
||||
* !!! 因为你可能改变的是根节点自身,不仅仅是改变根节点指向的值。
|
||||
* !!! 要改变一级指针本身,就需要传入二级指针。
|
||||
*
|
||||
* @param root
|
||||
* @param data
|
||||
* @return int
|
||||
*/
|
||||
static int insert(struct node_st **root, int data)
|
||||
{
|
||||
struct node_st *node;
|
||||
|
||||
if (NULL == *root)
|
||||
{
|
||||
node = malloc(sizeof(*node));
|
||||
if (NULL == node)
|
||||
return -1;
|
||||
|
||||
node->data = data;
|
||||
node->l = NULL;
|
||||
node->r = NULL;
|
||||
|
||||
*root = node;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (data <= (*root)->data)
|
||||
return insert(&(*root)->l, data);
|
||||
return insert(&(*root)->r, data);
|
||||
}
|
||||
|
||||
|
||||
static void draw_(struct node_st *root, int level)
|
||||
{
|
||||
if (NULL == root)
|
||||
return;
|
||||
|
||||
draw_(root->r, level + 1);
|
||||
|
||||
for (int i = 0; i < level; i++)
|
||||
printf(" ");
|
||||
printf("%c\n", root->data);
|
||||
|
||||
draw_(root->l, level + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 绘制树
|
||||
* @details
|
||||
* 躺下来,根在左,子节点在右。
|
||||
* 这样子,深度即制表符个数。
|
||||
*D
|
||||
* @param root
|
||||
*/
|
||||
static void draw(struct node_st *root)
|
||||
{
|
||||
draw_(root, 0);
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
|
||||
static int save_(struct node_st *root, FILE *fp)
|
||||
{
|
||||
fputc('(', fp);
|
||||
/* if error */
|
||||
|
||||
if (NULL == root)
|
||||
{
|
||||
fputc(')', fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
fputc(root->data, fp);
|
||||
/* if error */
|
||||
|
||||
save_(root->l, fp);
|
||||
save_(root->r, fp);
|
||||
|
||||
fputc(')', fp);
|
||||
/* if error */
|
||||
}
|
||||
|
||||
static int save(struct node_st *root, const char *path)
|
||||
{
|
||||
FILE *fp;
|
||||
fp = fopen(path, "w");
|
||||
if (NULL == fp)
|
||||
return -1;
|
||||
|
||||
save_(tree, fp);
|
||||
|
||||
fputc('\n', fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char arr[] = "cefadjbh";
|
||||
int i;
|
||||
struct score_st *datap;
|
||||
|
||||
for (i = 0; i < sizeof(arr) / sizeof(*arr) - 1; i++)
|
||||
{
|
||||
/* 小往左,大往右 */
|
||||
insert(&tree, arr[i]);
|
||||
}
|
||||
|
||||
draw(tree);
|
||||
|
||||
save(tree, FNAME);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user