Skip to content

Instantly share code, notes, and snippets.

@rickyzhang-cn
rickyzhang-cn / build_binarytree.c
Last active August 29, 2015 14:07
重建二叉树
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1024
#define true 1
#define false 0
struct bt_node {
int value;
struct bt_node * left;
@rickyzhang-cn
rickyzhang-cn / eight_queue.c
Created October 20, 2014 13:25
八皇后问题的实现
/*
* 回溯法解N皇后问题
* 使用一个一维数组表示皇后的位置
* 其中数组的下标表示皇后所在的行
* 数组元素的值表示皇后所在的列
* 这样设计的棋盘,所有皇后必定不在同一行
*
* 假设前n-1行的皇后已经按照规则排列好
* 那么可以使用回溯法逐个试出第n行皇后的合法位置
* 所有皇后的初始位置都是第0列
@rickyzhang-cn
rickyzhang-cn / heap_sort.c
Created October 20, 2014 13:27
堆排序的C实现
#include <stdio.h>
#include <stdlib.h>
void down(int * p, int k, int i)
{
int value=p[i];
int t;
for(t=i*2;t<=k;t=t*2)
{
@rickyzhang-cn
rickyzhang-cn / hanoi.cpp
Created October 20, 2014 13:30
汉诺塔问题的递归和非递归实现
#include <iostream>
using namespace std;
void hanoi(int n, char src, char bri, char dst)
{
if(n == 1)
{
cout<<"Move"<<n<<"from"<<src<<"to"<<dst<<endl;
}else
{
@rickyzhang-cn
rickyzhang-cn / heap.c
Created October 20, 2014 13:35
优先队列ADT的C实现
#include <stdlib.h>
#include <stdio.h>
#include "heap.h"
PriorityQueue pq_init(int capacity)
{
PriorityQueue H;
H=(PriorityQueue )malloc(sizeof(struct HeapStruct));
@rickyzhang-cn
rickyzhang-cn / hash_table.h
Created October 20, 2014 13:38
Hash Table的C实现
#define INITIAL_TABLE_SIZE 163
typedef struct hash_item {
void* p_elem;
struct hash_item* next;
} hash_item, *p_hash_item;
typedef struct hash_table {
int item_num;
int table_size;
@rickyzhang-cn
rickyzhang-cn / avl_tree.c
Created October 20, 2014 13:40
AVL Tree的C实现
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "avl_tree.h"
static Position SingleRotateWithRight(Position K2);
static int Height(Position P)
{
if(P == NULL)
@rickyzhang-cn
rickyzhang-cn / merge_sort.c
Created October 20, 2014 13:42
排序算法的C实现
#include <stdio.h>
#include <stdlib.h>
void merge(int a[], int temp_array[], int l_pos, int r_pos, int end)
{
int i=0;
int j;
int l=l_pos,r=r_pos;
while(l<r_pos && r<=end)
{
@rickyzhang-cn
rickyzhang-cn / main.c
Last active August 29, 2015 14:08
仿照linux kfifo写的ring buffer
/**@brief ring buffer测试程序,创建两个线程,一个生产者,一个消费者。
* 生产者每隔1秒向buffer中投入数据,消费者每隔2秒去取数据。
*@atuher Anker date:2013-12-18
* */
#include "ring_buffer.h"
#include <pthread.h>
#include <time.h>
#define BUFFER_SIZE 1024 * 1024
@rickyzhang-cn
rickyzhang-cn / rngLib.c
Created November 7, 2014 12:03
VxWorks下rngLib的实现代码
/* rngLib.c - ring buffer subroutine library */
/* Copyright 1984-1995 Wind River Systems, Inc. */
/*
modification history
--------------------
01x,13feb95,jdi doc tweaks.
01w,20jan93,jdi documentation cleanup for 5.1.