init commit
This commit is contained in:
103
archive/algorithm/struct/adt/hash.c
Normal file
103
archive/algorithm/struct/adt/hash.c
Normal file
@@ -0,0 +1,103 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TABLE_SIZE 41
|
||||
#define MAX_FIND 4
|
||||
|
||||
//利用平方探测法创建散列
|
||||
typedef struct _hash *HASH_TABLE;
|
||||
|
||||
struct _hash
|
||||
{
|
||||
char *key;
|
||||
int value;
|
||||
};
|
||||
|
||||
HASH_TABLE hash_create()
|
||||
{
|
||||
//创建一个大小为size的表
|
||||
//也就是申请size大小的内存空间
|
||||
HASH_TABLE ret = (HASH_TABLE)malloc(TABLE_SIZE * sizeof(struct _hash));
|
||||
memset(ret, 0, sizeof(struct _hash) * TABLE_SIZE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hash(const char *key)
|
||||
{
|
||||
//散列函数,P113
|
||||
unsigned int hash_value = 0;
|
||||
while (*key != '\0')
|
||||
{
|
||||
hash_value = (hash_value << 5) + *key++;
|
||||
}
|
||||
return hash_value % TABLE_SIZE;
|
||||
}
|
||||
|
||||
int hash_insert(HASH_TABLE hash_table, const char *key, int value)
|
||||
{
|
||||
//插入一个值到hash表中
|
||||
//先计算hash值
|
||||
int sub = hash(key);
|
||||
//判断是否有碰撞
|
||||
if ((int)hash_table[sub].key != 0)
|
||||
{
|
||||
//值不是空的,利用平方的方法,探测空的地方
|
||||
int tmp_sub = 0;
|
||||
for (int i = 0; i < MAX_FIND; i++)
|
||||
{
|
||||
tmp_sub = sub + i * i;
|
||||
if (tmp_sub >= TABLE_SIZE)
|
||||
{
|
||||
tmp_sub -= TABLE_SIZE;
|
||||
}
|
||||
if ((int)hash_table[tmp_sub].key == 0 || strcmp(hash_table[tmp_sub].key, key) == 0)
|
||||
{
|
||||
//找到了空的位置,赋值,跳出循环
|
||||
break;
|
||||
}
|
||||
//如果继续碰撞,继续往后计算
|
||||
}
|
||||
if (hash_table[tmp_sub].value != value)
|
||||
{
|
||||
//没有找到空位,返回-1,提示失败
|
||||
return -1;
|
||||
}
|
||||
sub = tmp_sub;
|
||||
}
|
||||
hash_table[sub].key = (char *)malloc(strlen(key)+1);
|
||||
memset(hash_table[sub].key,0,strlen(key)+1);
|
||||
memcpy(hash_table[sub].key, key, strlen(key));
|
||||
hash_table[sub].value = value;
|
||||
return sub;
|
||||
}
|
||||
|
||||
int hash_find(HASH_TABLE hash_table, const char *key, int value)
|
||||
{
|
||||
}
|
||||
|
||||
#ifndef MAIN_FUNC
|
||||
int main()
|
||||
{
|
||||
HASH_TABLE hash_table = hash_create();
|
||||
hash_insert(hash_table, "one", 1);
|
||||
hash_insert(hash_table, "t", 2);
|
||||
hash_insert(hash_table, "e", 3);
|
||||
hash_insert(hash_table, "q", 4);
|
||||
hash_insert(hash_table, "while", 5);
|
||||
hash_insert(hash_table, "z", 6);
|
||||
hash_insert(hash_table, "f", 7);
|
||||
//遍历表
|
||||
for (int i = 0; i < TABLE_SIZE; i++)
|
||||
{
|
||||
if ((int)hash_table[i].key == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
printf("%s=>%d\n", hash_table[i].key, hash_table[i].value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
97
archive/algorithm/struct/adt/queue.c
Normal file
97
archive/algorithm/struct/adt/queue.c
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
/**
|
||||
* 数据结构,队列 感觉和栈差不多吧,一个先进后出,一个先进先出
|
||||
* 用双向链表的速度将快一些,本例采用双向链表,就不需要再检索倒数第二个节点,记录最后一个节点即可
|
||||
*/
|
||||
struct queue;
|
||||
typedef struct queue *prt_queue;
|
||||
//3->2->1->3
|
||||
struct queue
|
||||
{
|
||||
int data;
|
||||
prt_queue last; //上一个
|
||||
prt_queue next; //下一个
|
||||
};
|
||||
|
||||
//3<=>2<=>1
|
||||
//3<=>2
|
||||
//3
|
||||
/**
|
||||
* 出队列
|
||||
*/
|
||||
int pop(prt_queue *end)
|
||||
{
|
||||
if (*end == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int ret_data = (*end)->data;
|
||||
prt_queue *tmp = end;
|
||||
*end = (*end)->last;
|
||||
if ((*end) != NULL)
|
||||
{
|
||||
(*end)->next = NULL;
|
||||
}
|
||||
free(tmp);
|
||||
return ret_data;
|
||||
}
|
||||
//1
|
||||
//2<=>1
|
||||
//3<=>2<=>1
|
||||
/**
|
||||
* 进入队列
|
||||
*/
|
||||
prt_queue push(int data, prt_queue *h)
|
||||
{
|
||||
prt_queue tmp_node = malloc(sizeof(struct queue));
|
||||
(*h)->last = tmp_node;
|
||||
memset(tmp_node, 0, sizeof(struct queue));
|
||||
tmp_node->data = data;
|
||||
tmp_node->next = *h;
|
||||
*h = tmp_node;
|
||||
return tmp_node;
|
||||
}
|
||||
/**
|
||||
* 创建栈
|
||||
*/
|
||||
prt_queue create_queue(int data)
|
||||
{
|
||||
prt_queue header = malloc(sizeof(struct queue));
|
||||
memset(header, 0, sizeof(struct queue));
|
||||
header->data = data;
|
||||
return header;
|
||||
}
|
||||
|
||||
void print_queue(prt_queue *end)
|
||||
{
|
||||
int data = 0;
|
||||
while ((data = pop(end)) != 0)
|
||||
{
|
||||
printf("%d\t", data);
|
||||
}
|
||||
}
|
||||
#ifndef MAIN_FUNC
|
||||
int main()
|
||||
{
|
||||
prt_queue queue = create_queue(1);
|
||||
prt_queue end_node = queue;
|
||||
push(23, &queue);
|
||||
push(15, &queue);
|
||||
push(123, &queue);
|
||||
push(36, &queue);
|
||||
push(89, &queue);
|
||||
push(16, &queue);
|
||||
push(324, &queue);
|
||||
push(526, &queue);
|
||||
push(166, &queue);
|
||||
push(984, &queue);
|
||||
push(85, &queue);
|
||||
pop(&end_node);
|
||||
pop(&end_node);
|
||||
print_queue(&end_node);
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
86
archive/algorithm/struct/adt/stack.c
Normal file
86
archive/algorithm/struct/adt/stack.c
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
/**
|
||||
* 数据结构,栈
|
||||
*/
|
||||
struct stack;
|
||||
typedef struct stack *prt_stack;
|
||||
|
||||
struct stack
|
||||
{
|
||||
int data;
|
||||
prt_stack last;
|
||||
};
|
||||
|
||||
//1<-2 header=2 header->last=1
|
||||
//1 header=1 header->last=null
|
||||
/**
|
||||
* 出栈
|
||||
*/
|
||||
int pop(prt_stack *h)
|
||||
{
|
||||
if (*h == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int ret_data = (*h)->data;
|
||||
prt_stack *tmp = h;
|
||||
*h = (*h)->last;
|
||||
free(tmp);
|
||||
return ret_data;
|
||||
}
|
||||
//1 header=1 header->last=null
|
||||
//1<-2 header=2 header->last=1
|
||||
/**
|
||||
* 入栈
|
||||
*/
|
||||
prt_stack push(int data, prt_stack *h)
|
||||
{
|
||||
prt_stack tmp_node = malloc(sizeof(struct stack));
|
||||
memset(tmp_node, 0, sizeof(struct stack));
|
||||
tmp_node->data = data;
|
||||
tmp_node->last = *h;
|
||||
*h = tmp_node;
|
||||
return tmp_node;
|
||||
}
|
||||
/**
|
||||
* 创建栈
|
||||
*/
|
||||
prt_stack create_stack(int data)
|
||||
{
|
||||
prt_stack header = malloc(sizeof(struct stack));
|
||||
memset(header, 0, sizeof(struct stack));
|
||||
header->data = data;
|
||||
return header;
|
||||
}
|
||||
|
||||
void print_stack(prt_stack *h)
|
||||
{
|
||||
int data = 0;
|
||||
while ((data = pop(h)) != 0)
|
||||
{
|
||||
printf("%d\t", data);
|
||||
}
|
||||
}
|
||||
#ifndef MAIN_FUNC
|
||||
int main()
|
||||
{
|
||||
prt_stack stack = create_stack(1);
|
||||
push(23, &stack);
|
||||
push(15, &stack);
|
||||
push(123, &stack);
|
||||
push(36, &stack);
|
||||
push(89, &stack);
|
||||
push(16, &stack);
|
||||
push(324, &stack);
|
||||
push(526, &stack);
|
||||
push(166, &stack);
|
||||
push(984, &stack);
|
||||
push(85, &stack);
|
||||
pop(&stack);
|
||||
print_stack(&stack);
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
109
archive/algorithm/struct/adt/u_link_list.c
Normal file
109
archive/algorithm/struct/adt/u_link_list.c
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
/**
|
||||
* 单向链表 增,删,查,改
|
||||
*/
|
||||
struct node;
|
||||
typedef struct node *prt_node;
|
||||
|
||||
prt_node insert(int, int, prt_node);
|
||||
prt_node find_node(int, prt_node);
|
||||
void print_list(prt_node);
|
||||
prt_node after_node(prt_node);
|
||||
void del(int, prt_node);
|
||||
void modify(int, int, prt_node);
|
||||
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
prt_node next;
|
||||
};
|
||||
/**
|
||||
* 在n位置后面插入一个数据,n为-1时插入到最后
|
||||
*/
|
||||
prt_node insert(int n, int data, prt_node h)
|
||||
{
|
||||
prt_node tmp_node;
|
||||
prt_node last_node = n == -1 ? after_node(h) : find_node(n, h); //取得前一个节点
|
||||
tmp_node = last_node->next;
|
||||
last_node->next = malloc(sizeof(struct node));
|
||||
last_node->next->next = tmp_node;
|
||||
last_node->next->data = data;
|
||||
tmp_node = last_node->next;
|
||||
return tmp_node; //返回插入的节点
|
||||
}
|
||||
/**
|
||||
* 寻找前一个节点,n为-1时查找最后节点
|
||||
*/
|
||||
prt_node find_node(int n, prt_node h)
|
||||
{
|
||||
prt_node ret = h;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (ret->next == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
ret = ret->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* 最后一个
|
||||
*/
|
||||
prt_node after_node(prt_node h)
|
||||
{
|
||||
while (h->next != NULL)
|
||||
h = h->next;
|
||||
return h;
|
||||
}
|
||||
/**
|
||||
* 删除数据
|
||||
*/
|
||||
void del(int n, prt_node h)
|
||||
{
|
||||
h = find_node(n, h);
|
||||
prt_node tmp_node = h->next;
|
||||
h->next = tmp_node->next;
|
||||
free(tmp_node);
|
||||
}
|
||||
/**
|
||||
* 修改数据
|
||||
*/
|
||||
void modify(int n, int data, prt_node h)
|
||||
{
|
||||
prt_node tmp_node = find_node(n, h)->next;
|
||||
tmp_node->data = data;
|
||||
}
|
||||
/**
|
||||
* 输出链表
|
||||
*/
|
||||
void print_list(prt_node h)
|
||||
{
|
||||
while ((h = h->next) != NULL)
|
||||
{
|
||||
printf("%d\t", h->data);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
prt_node header = memset(malloc(sizeof(struct node)), 0, sizeof(struct node));
|
||||
//添加
|
||||
insert(-1, 153, header);
|
||||
insert(-1, 2343, header);
|
||||
insert(-1, 323, header);
|
||||
insert(-1, 564, header);
|
||||
//删除
|
||||
del(2, header);
|
||||
//插入
|
||||
insert(2, 23333, header);
|
||||
insert(4, 312, header);
|
||||
//修改
|
||||
modify(0, 888, header);
|
||||
print_list(header);
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
94
archive/algorithm/struct/adt/u_link_list.h
Normal file
94
archive/algorithm/struct/adt/u_link_list.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
|
||||
/**
|
||||
* 单向链表头文件
|
||||
*/
|
||||
|
||||
/**
|
||||
* 单向链表 增,删,查,改
|
||||
*/
|
||||
struct node;
|
||||
typedef struct node *prt_node;
|
||||
|
||||
prt_node insert(int, int, prt_node);
|
||||
prt_node find_node(int, prt_node);
|
||||
void print_list(prt_node);
|
||||
prt_node after_node(prt_node);
|
||||
void del(int, prt_node);
|
||||
void modify(int, int, prt_node);
|
||||
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
prt_node next;
|
||||
};
|
||||
/**
|
||||
* 在n位置后面插入一个数据,n为-1时插入到最后
|
||||
*/
|
||||
prt_node insert(int n, int data, prt_node h)
|
||||
{
|
||||
prt_node tmp_node;
|
||||
prt_node last_node = n == -1 ? after_node(h) : find_node(n, h); //取得前一个节点
|
||||
tmp_node = last_node->next;
|
||||
last_node->next = (prt_node)malloc(sizeof(struct node));
|
||||
last_node->next->next = tmp_node;
|
||||
last_node->next->data = data;
|
||||
tmp_node = last_node->next;
|
||||
return tmp_node; //返回插入的节点
|
||||
}
|
||||
/**
|
||||
* 寻找前一个节点,n为-1时查找最后节点
|
||||
*/
|
||||
prt_node find_node(int n, prt_node h)
|
||||
{
|
||||
prt_node ret = h;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (ret->next == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
ret = ret->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* 最后一个
|
||||
*/
|
||||
prt_node after_node(prt_node h)
|
||||
{
|
||||
while (h->next != NULL)
|
||||
h = h->next;
|
||||
return h;
|
||||
}
|
||||
/**
|
||||
* 删除数据
|
||||
*/
|
||||
void del(int n, prt_node h)
|
||||
{
|
||||
h = find_node(n, h);
|
||||
prt_node tmp_node = h->next;
|
||||
h->next = tmp_node->next;
|
||||
free(tmp_node);
|
||||
}
|
||||
/**
|
||||
* 修改数据
|
||||
*/
|
||||
void modify(int n, int data, prt_node h)
|
||||
{
|
||||
prt_node tmp_node = find_node(n, h)->next;
|
||||
tmp_node->data = data;
|
||||
}
|
||||
/**
|
||||
* 输出链表
|
||||
*/
|
||||
void print_list(prt_node h)
|
||||
{
|
||||
while ((h = h->next) != NULL)
|
||||
{
|
||||
printf("%d\t", h->data);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
Reference in New Issue
Block a user