| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6104 人关注过本帖, 3 人收藏
标题:自学数据结构所写的一些算法~
取消只看楼主 加入收藏
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏(3)
 问题点数:0 回复次数:9 
自学数据结构所写的一些算法~

动态内存分配算法
/* 工程名称: W32_Alloc */
/* Alloc.h */
#ifndef __ALLOC_H
#define __ALLOC_H

#include "Alloc.cpp"

#endif
/* Alloc.cpp */
typedef unsigned int WORD;

typedef enum { sUsed = 1, sFree = 0 } blk_state;

typedef struct mcb {

blk_state s; /* block state */

WORD *pBegin; /* begining of allocated address */
WORD *pEnd; /* ending of allocated address */
size_t size; /* allocated size */

struct mcb *next;
struct mcb *prev;
} mcb_t; /* memory control block */

typedef struct {
mcb_t *blk_head; /* mcb linked-list head and tail */
mcb_t *blk_tail;

WORD *blk_free; /* free block ptr */

WORD *ptos; /* pointer of stack top */

size_t stack_size; /* stack size */
size_t used_size; /* used size */

} mcbc_t; /* memory control block container */

mcbc_t *c; /* global container */

inline mcbc_t *
init_container( WORD *ptos, int stack_size ) {
mcbc_t *ret = (mcbc_t *)malloc(sizeof(mcbc_t));
assert(ret);

ret->ptos = ptos;

ret->blk_head = (mcb_t *)0;
ret->blk_tail = (mcb_t *)0;
ret->blk_free = ptos;

ret->stack_size = stack_size;
ret->used_size = (int)0;

return ret;
}

inline void
bind_container( mcbc_t *con ) {
c = con;
}

inline void
throw_container( void ) {
mcb_t *p, *bak;

if(c) {
/* free linked-list */
p = c->blk_head;
while(p) {
bak = p->next;
free(p);
p = bak;
}
/* free container */
free(c);
c = (mcbc_t *)0;
}
}

inline void *
allocate( size_t size ) {
assert(c); /* ensure container has been binded */

size_t reminder = c->stack_size - c->used_size; /* calc reminder */
size_t split_size;

size_t ss;
mcb_t *p, *bak;

void *ret;

if(!size)
return (void *)0;

/* general size convert to stack offset */
if(!(size % sizeof(WORD)))
ss = size / sizeof(WORD);
else
ss = size / sizeof(WORD) + 1;

if( reminder < ss )
return (void *)0; /* not enough space for allocating */

if(!c->blk_head) {

/* no mcb in linked-list, create one */
p = (mcb_t *)malloc(sizeof(mcb_t));
assert(p);

ret = (void *)c->ptos;

c->blk_free += ss;
c->used_size += ss;

p->pBegin = c->ptos;
p->pEnd = c->blk_free;
p->s = sUsed;
p->size = size;

p->next = (mcb_t *)0;
p->prev = (mcb_t *)0;

c->blk_head = c->blk_tail = p;

return ret;

}
else {
/* find free block */
p = c->blk_head;
while(p) {
if(p->size >= ss && p->s == sFree) {
ret = (void *)p->pBegin;

/* p->size > ss, split it! */
if(p->size > ss) {
split_size = p->size - ss;

/* recalc end boundary */
p->pEnd = (WORD *)(p->pBegin + ss);
p->size = ss;
p->s = sUsed;

bak = (mcb_t *)malloc(sizeof(mcb_t));
assert(bak);

/* insert mcb */
bak->prev = p;
if(p->next) {
p->next->prev = bak;
bak->next = p->next;
}
else {
bak->next = (mcb_t *)0;
c->blk_tail = bak;
}
p->next = bak;

/* constructing a splited control block */
bak->pBegin = p->pEnd;
bak->pEnd = (WORD *)(bak->pBegin + split_size);

bak->size = split_size;
bak->s = sFree;
}
else
p->s = sUsed;

c->used_size += ss;
return ret;
}
p = p->next;
}

/* no matched block, create one at tail */
ret = (void *)c->blk_free;

c->blk_free += ss;
c->used_size += ss;

p = (mcb_t *)malloc(sizeof(mcb_t));
assert(p);

p->pBegin = (WORD *)ret;
p->pEnd = c->blk_free;

p->prev = c->blk_tail;
p->next = (mcb_t *)0;

p->size = ss;
p->s = sUsed;

c->blk_tail->next = p;
c->blk_tail = p;

return ret;
}
}

inline int
release( void *ptr ) {
assert(c);

mcb_t *p = (mcb_t *)0;
mcb_t *pLeft = (mcb_t *)0;
mcb_t *pRight = (mcb_t *)0;

p = c->blk_head;
while(p) {
/* free matched block */
if(p->pBegin == (WORD *)ptr) {
p->s = sFree;
c->used_size -= p->size;

/* merge free neighbours */
pLeft = p->prev;
pRight = p->next;
while( (pLeft && pLeft->s == sFree) || (pRight && pRight->s == sFree) ) {
/* merge left neighbour */
if(pLeft && pLeft->s == sFree) {
p->pBegin = pLeft->pBegin;
p->size += pLeft->size;
/* destory left neighbour */
p->prev = pLeft->prev;
if(pLeft->prev)
pLeft->prev->next = p;
else
c->blk_head = p;
free(pLeft);
}

/* merge right neighbour */
if(pRight && pRight->s == sFree) {
p->pEnd = pRight->pEnd;
p->size += pRight->size;
/* destory right neighbour */
p->next = pRight->next;
if(pRight->next)
pRight->next->prev = p;
else
c->blk_tail = p;
free(pRight);
}

/* reload neighbours */
pLeft = p->prev;
pRight = p->next;
}
/* on success */
return 0;
}
p = p->next;
}
/* no block matched */
return 1;
}
/* main.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <assert.h>

#include "Alloc.h"

WORD stk[8 * 1024]; /* 16Kbytes */

int main() {
char *s1,*s2,*s3;
mcbc_t *con = init_container(stk, 8*1024);
bind_container(con);

s1 = (char *)allocate(12);
s2 = (char *)allocate(4);
s3 = (char *)allocate(5);
strcpy(s1,"Hello,world");
strcpy(s2,"ABC");
strcpy(s3,"1234");

printf("%s,%s,%s\n",s1,s2,s3);

getch();

release((void *)s1);
release((void *)s2);

s1 = (char *)allocate(16);
strcpy(s1,"Hello,world,ABC");

printf("%s %s %s\n",s1,s2,s3);

getch();

release((void *)s1);
release((void *)s3);

throw_container();

return 0;
}

搜索更多相关主题的帖子: 数据结构 算法 内存 Alloc blk 
2006-10-28 19:14
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

A*算法
/* 工程名称: W32_AStar */
/* AStar.h */
/* 设计者: yuki */

#ifndef __ASTAR_H
#define __ASTAR_H

#define MAZE_WIDTH 10 /* 迷宫宽度 */
#define MAZE_HEIGHT 10 /* 迷宫高度 */

#define PATH_BLOCK 0 /* 障碍物 */
#define PATH_WALKON 1 /* 可行走 */
#define PATH_FOOTPRINT 2 /* 脚印 */

#include "AStar.cpp"

#endif
/* AStar.cpp */
/* 设计者: yuki */

typedef unsigned char byte_t;
typedef unsigned int uint_t;

/* 路径节点 */
typedef struct footprint {

/* 存放在数组中的位置 */
uint_t pos;

/* 存放方向信号量 */
byte_t direct;

struct footprint *next;
struct footprint *prev;

} path_t;


/*
方向信号量查询表
0x01(0000 0001) : 上
0x02(0000 0010) : 下
0x04(0000 0100) : 左
0x08(0000 1000) : 右
*/

static byte_t d_signal[4] = {0x01, 0x02, 0x04, 0x08};

/*
方向信号量使用表
如果指定方向已经走过,那么使用“与”运算去处该方向
0x0E(0000 1110) : 上
0x0D(0000 1101) : 下
0x0B(0000 1011) : 左
0x07(0000 0111) : 右
*/

static byte_t d_spend[4] = {0x0E, 0x0D, 0x0B, 0x07};

/* 指定方向移动偏量 */
static int move[4][2] = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} };

/* 打印迷宫用的符号 */
static byte_t symbolic[3] = {'#',0x20,'*'};

/* 求两点间的距离 */
inline uint_t
distance( uint_t pos1X, uint_t pos1Y, uint_t pos2X, uint_t pos2Y ) {

uint_t ret = 0;

/* 距离公式 */
ret = (uint_t)sqrt((pow((double)((int)pos1X - (int)pos2X),2) + pow((double)((int)pos1Y - (int)pos2Y),2)));

return ret;

}

/* 压缩坐标 */
inline uint_t
create_pos( uint_t pX, uint_t pY ) {
uint_t ret = 0;
/* 将pX赋给ret,这样pX坐标在ret的低八位 */
ret = pX;

/* 将pX坐标移到高八位去,这样低位就能存放pY */
ret <<= 8;
/* 将pY存放放到ret的低八位,并保持高八位的数据不变 */
ret |= pY;

return ret;
}

/*
== 估计函数 ===========================================
-p : 当前移动到的节点指针
-quit_x
-quit_y : quit_x 和 quit_y表示迷宫出口坐标
-maze : 迷宫矩阵
=======================================================
*/
inline path_t *
evaluate( path_t *p, uint_t quit_x, uint_t quit_y, byte_t maze[MAZE_HEIGHT][MAZE_WIDTH] ) {
uint_t pX, pY;

/* 用于接收四个方向离开出口的距离,以便选择最近的方向移动 */
int dis[4];
int minDis = 32767;
int minId = -1;

path_t *pnode = (path_t *)0;

register int i;

/* 计算当前节点的坐标 */
pX = p->pos >> 8;
pY = p->pos & 0x00FF;

memset(dis, (int)-1, sizeof(int)*4);

/* 计算每个方向离开出口的距离,一次存放到dis数组,若没有i方向,则dis[i]任保留-1 */
for( i = 0; i < 4; ++i ) {
if( (p->direct & d_signal[i]) >> i == 0x01 )
dis[i] =(int)distance(pX + move[i][0], pY + move[i][1], quit_x, quit_y);
}

/* 获得最短距离的方向 */
for(i = 0; i < 4; ++i) {
if(dis[i] != -1 && dis[i] < minDis) {
minId = i;
minDis = dis[i];
}
}

/* 若没有可用的方向,则通知寻径函数折回 */
if(minId == -1)
return (path_t *)0;

/* 用去最近距离方向的信号量 */
p->direct &= d_spend[minId];
/* 在移动到新位置之前,在旧位置处留下足迹 */
maze[pY][pX] = (byte_t)PATH_FOOTPRINT;

/* 构建新的路径节点 */
pnode = (path_t *)malloc(sizeof(path_t));
assert(pnode);

/* 计算下一个位置的坐标 */
pX += move[minId][0];
pY += move[minId][1];

pnode->pos = create_pos(pX, pY);
pnode->prev = p;
pnode->next = (path_t *)0;
pnode->direct = 0;

/* 在新位置处,计算下一个位置可用的移动方向 */
for(i = 0; i < 4; ++i) {
if(maze[pY + move[i][1]][pX + move[i][0]] != PATH_BLOCK && maze[pY + move[i][1]][pX + move[i][0]] != PATH_FOOTPRINT) {
/* 若尝试的下一个位置不是障碍物或自己走过的足迹,则视为可用方向,获得该方向的信号量 */
pnode->direct |= d_signal[i];
}
}

return pnode;

}

/*
== A*算法寻径函数 ===========================================
-eX
-eY :入口坐标
-qX
-qY :出口坐标
-maze :迷宫矩阵
=============================================================
*/

inline path_t *
AStar(uint_t eX, uint_t eY, uint_t qX, uint_t qY, byte_t maze[MAZE_HEIGHT][MAZE_WIDTH]) {
register int i;

/* 压缩坐标 */
uint_t quit_pos = create_pos(qX, qY);

/* 构建入口路径节点,视为路径链的头 */
path_t *head = (path_t *)malloc(sizeof(path_t));
path_t *p = (path_t *)0;
path_t *back = (path_t *)0;
assert(head);

p = head;
p->direct = 0;
p->pos = create_pos(eX,eY);
p->next = (path_t *)0;
p->prev = (path_t *)0;

/* 创建入口处的可用方向 */
for(i = 0; i < 4; ++i) {
if(maze[eY + move[i][1]][eX + move[i][0]] != PATH_BLOCK)
/* 若无障碍物则获得该方向的信号量 */
p->direct |= d_signal[i];
}

do {

/* 获得下个路径的节点指针 */
back = evaluate(p, qX, qY, maze);
if(back) {
p->next = back;
p = p->next;
}

/* 无路可走则折回 */
if(p->direct == 0 && p != head && p->pos != quit_pos) {
back = p->prev;
back->next = (path_t *)0;

/* 清楚脚印 */
maze[p->pos & 0x00FF][p->pos >> 8] = (byte_t)PATH_WALKON;

free(p);
p = back;
}

/* 若走不出迷宫,即折回到入口,且入口处的可用方向全部尝试过 */
if(p == head && p->direct == 0) {
free(head);
return (path_t *)0;
}

} while( p->pos != quit_pos );

/* 在出口处留下脚印,便于打印 */
maze[p->pos & 0x00FF][p->pos >> 8] = (byte_t)PATH_FOOTPRINT;

return head;

}
/* main.cpp */
/* 设计者: yuki */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <math.h>
#include <assert.h>

#include "AStar.h"

static byte_t maze[MAZE_HEIGHT][MAZE_WIDTH] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 1, 1, 1, 0, 1, 0,
0, 1, 1, 0, 1, 1, 1, 0, 1, 0,
0, 1, 1, 1, 1, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 1, 1, 0, 1, 0,
0, 1, 1, 1, 0, 1, 1, 0, 1, 0,
0, 1, 0, 1, 1, 1, 0, 1, 1, 0,
0, 1, 0, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

int main() {
register int i,j;

path_t *pHead = AStar((uint_t)1,(uint_t)1,(uint_t)2,(uint_t)8,maze);
path_t *p = pHead;
path_t *bak;
if(p) {
bak = p->next;
printf("(%u,%u)",p->pos >> 8, p->pos & 0x00FF);
free(p);
p = bak;
while(p) {
bak = p->next;
printf("->(%u,%u)",p->pos >> 8, p->pos & 0x00FF);
free(p);
p = bak;
}
printf("\n");
}
else
printf("No path to get out of the maze\n");

pHead = p = bak = (path_t *)0;

/* 打印迷宫 */
for(i = 0; i < MAZE_HEIGHT; ++i) {
for(j = 0; j < MAZE_WIDTH; ++j)
printf("%c",symbolic[maze[i][j]]);
printf("\n");
}

getch();

return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:16
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

表达式计算器
/* 工程名称: W32_Expr */
/* Expr.h */
#ifndef __EXPR_H
#define __EXPR_H

#define TRUE 0x01
#define FALSE 0x00

#define OP_PRIOR_LESS_TO 0x00
#define OP_PRIOR_GREATER_THAN 0x01
#define OP_PRIOR_EQUAL_TO 0x02

#define STACK_SIZE 64
#define BUFFER_SIZE 16

#include "Expr.cpp"

#endif
/* Expr.cpp */
typedef unsigned char op_prior_t;
typedef unsigned char bool_t;
typedef int stack_t;

/* is an operator ? */
inline bool_t
is_opr(char c) {
switch(c) {
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
return (bool_t)TRUE;
default:
return (bool_t)FALSE;
}
}

/* calc two oprands */
inline int
calc_oprand(int opr1, int opr2, char op) {
switch(op) {
case '+':
return opr1 + opr2;
case '-':
return opr1 - opr2;
case '*':
return opr1 * opr2;
case '/':
assert(opr2);
return opr1 / opr2;
default:
return -1;
}
}

/* get prior of operator */
inline op_prior_t
get_op_prior(char op) {
switch(op) {
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
default:
return 0xFF;
}
}

inline int
cmp_op(char op1, char op2) {
op_prior_t pop1 = get_op_prior(op1);
op_prior_t pop2 = get_op_prior(op2);

assert(pop1 != 0xFF);
assert(pop2 != 0xFF);

if(pop1 > pop2)
return OP_PRIOR_GREATER_THAN;
else if(pop1 < pop2)
return OP_PRIOR_LESS_TO;
else
return OP_PRIOR_EQUAL_TO;
}

/* string length */
inline int
strl(char *str) {
char *p = str;
while(*p++);
return (int)(p - str - 1);
}

/* 10^n */
inline int
pow_ten_n(int n) {
int ret = 1;
while(n > 0) {
ret *= 10;
--n;
}
return ret;
}

/* convert string to integer */
inline int
str_to_int(char *str) {
int ret = 0;
int i = 0;
int len = strl(str);

if(*str == '-' || *str == '+') {
++i;
--len;
}

while(len > 0) {
ret += (*(str + i) - '0') * pow_ten_n(len-1);
++i;
--len;
}

if(*str == '-')
return (-1) * ret;
return ret;
}

inline int
calc_expr(char *exp) {
/* test express whether it is vaild */
assert(exp);

/* stack of oprands */
stack_t stk_opr[STACK_SIZE];
/* stack of operators */
stack_t stk_op[STACK_SIZE];

/* oprand1 & oprand2 */
int opr1 = 0;
int opr2 = 0;

int cmp_ret;

/* buffer */
char buffer[BUFFER_SIZE];

/* expression length */
int len = strl(exp);

/* counters */
register int i,j;

/* stack pointer */
stack_t *pOpr = stk_opr;
stack_t *pOp = stk_op;

/* initialize buffer & stacks */
memset(buffer, (char)0, sizeof(char) * BUFFER_SIZE);
memset(stk_opr,(stack_t)0,sizeof(stack_t) * STACK_SIZE);
memset(stk_op, (stack_t)0,sizeof(stack_t) * STACK_SIZE);

/* get the first value */
if(is_opr(*exp) && (*exp == '+' || *exp == '-')) {
*buffer = *exp;
i = 1;
j = 1;
}
else {
i = 0;
j = 0;
}

while(!is_opr(exp[i]))
buffer[j++] = exp[i++];
if(i > 0) {
/* push the first oprand to oprands stack */
*pOpr = str_to_int(buffer);
/* clean up buffer */
memset(buffer, (char)0, sizeof(char) * BUFFER_SIZE);
/* reset buffer pointer 'j' */
j = 0;
}

/* push the first operator */
if(exp[i] != '\0')
*pOp = exp[i++];

while(exp[i]) {
/* if exp[i] is not an operator, than get the value */
if(!is_opr(exp[i])) {
while(!is_opr(exp[i]) && exp[i] != '\0')
buffer[j++] = exp[i++];
++pOpr;
*pOpr = str_to_int(buffer);
memset(buffer, (char)0, sizeof(char) * BUFFER_SIZE);
j = 0;
}
else {
if(exp[i] == '(') {
++pOp;
*pOp = exp[i++];
}
else if(exp[i] != ')') {
if(*pOp != '(') {
cmp_ret = cmp_op(*pOp,exp[i]);
if(cmp_ret == OP_PRIOR_GREATER_THAN || cmp_ret == OP_PRIOR_EQUAL_TO) {
/* pop two oprands to calc and than push the result to the stack */
opr2 = *pOpr--;
opr1 = *pOpr;
*pOpr = calc_oprand(opr1,opr2,*pOp);

/* push the low priority operator */
*pOp = exp[i++];
}
else if(cmp_ret == OP_PRIOR_LESS_TO) {
/* push the high priority operator */
++pOp;
*pOp = exp[i++];
}
}
else {
++pOp;
*pOp = exp[i++];
}
}
else {
/* loop until a ')' matched with */
while(*pOp != '(') {
opr2 = *pOpr--;
opr1 = *pOpr;
*pOpr = calc_oprand(opr1,opr2,*pOp);

/* pop the operator */
*pOp-- = (char)0;

if(pOp == stk_op && *pOp != '(') {
printf("No '(' matched in expression!\n");
getch();
exit(1);
}
}
/* pop the '(',and let the expression move on */
if(pOp > stk_op)
*pOp-- = (char)0;
else
*pOp = (char)0;
++i;
}
}
}
while(pOp >= stk_op) {
opr2 = *pOpr--;
opr1 = *pOpr;
*pOpr = calc_oprand(opr1,opr2,*pOp);
*pOp-- = (char)0;
}
return *stk_opr;
}
/* main.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <conio.h>
#include <string.h>

#include "Expr.h"

int main() {
char exp[80];
printf("Input an expression\n");
gets(exp);
printf("= %d\n",calc_expr(exp));

getch();
return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:19
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

广义表的演示
/* 工程名称: W32_GList */
/* GList.h */
#ifndef __GLIST_H
#define __GLIST_H

#include "Glist.cpp"

#endif
/* GList.cpp */
#define NODE_ELEMENT 0x00
#define NODE_LIST 0x01

typedef unsigned char node_type_t;
typedef unsigned int index_t;

typedef struct {
index_t id_x;
index_t id_y;
} locator_t;

typedef struct GList {
node_type_t lst_type;
char *menu;
int item_count;

union {
locator_t loc;
struct GList *sub_list;
} data_item;
struct Glist *next;
} list_t;

static char *description[3][2] = { "Black Eyes", "Blue Eyes" ,
"Tall Body" , "Short Body" ,
"Fat Body" , "Thin Body"
};

static char *meu_list[] = {"== Describe yourself ==\n1. Eye\n2. Height\n3. Figure\n0. Exit\nMake your choice : ",
"== Your eye color ==\n1. Black\n2. Blue\n0. Exit\nMake your choice : " ,
"== Your height ==\n1. Tall\n2. Short\n0. Exit\nMake your choice : " ,
"== Your figure ==\n1. Fat\n2. Thin\n0. Exit\nMake your choice : "
};

list_t *dialog_sheet = (list_t *)0;
list_t *static_dialog_sheet[4];

char image[256] = "You have ";

inline list_t *
create_glist( void ) {
register int i,j;

list_t *ret = (list_t *)malloc(sizeof(list_t));
list_t *p;
list_t *q;

/* create the first sheet */
ret->lst_type = NODE_LIST;
ret->menu = meu_list[0];
ret->item_count = 4;
ret->data_item.sub_list = (list_t *)0;

p = ret;
for(i = 1; i < 4; ++i) {
p->next = (struct Glist *)malloc(sizeof(list_t));
p = (list_t *)p->next;
p->menu = meu_list[i];
p->lst_type = NODE_LIST;
p->item_count = 3;
}

p->next = (struct Glist *)0;
p = (list_t *)ret->next;
i = 0;
j = 0;

while(p) {

p->data_item.sub_list = (list_t *)malloc(sizeof(list_t));
q = p->data_item.sub_list;

q->lst_type = NODE_ELEMENT;
q->data_item.loc.id_x = (index_t)i;
q->data_item.loc.id_y = (index_t)j++;

q->next = (struct Glist *)malloc(sizeof(list_t));
q = (list_t *)q->next;
q->lst_type = NODE_ELEMENT;
q->data_item.loc.id_x = (index_t)i++;
q->data_item.loc.id_y = (index_t)j;
q->next = (struct Glist *)0;

j = 0;

p = (list_t *)p->next;
}

return ret;
}

inline void
destory_list(list_t *lst) {
list_t *p = lst;
list_t *q = (list_t *)0;

while(p) {
if(p->lst_type == NODE_LIST) {
q = (list_t *)p->data_item.sub_list;
if(q)
destory_list(q);
}
q = (list_t *)p->next;
free(p);
p = q;
}
}

inline void
init_dialog_sheet() {
list_t *p;
register int i = 0;

dialog_sheet = create_glist();
p = dialog_sheet;
while(p) {
static_dialog_sheet[i++] = p;
p = (list_t *)p->next;
}
}

inline void
debug() {
list_t *p = (list_t *)dialog_sheet->next;
list_t *q;
while(p) {
q = (list_t *)p->data_item.sub_list;
printf("(%d,%d)",q->data_item.loc.id_x , q->data_item.loc.id_y);
q = (list_t *)q->next;
printf(",(%d,%d)\n",q->data_item.loc.id_x,q->data_item.loc.id_y);
p = (list_t *)p->next ;
}
}

inline void
main_menu() {
int c;

list_t *p = static_dialog_sheet[0];
do {
printf("%s",p->menu);
scanf("%d",&c);
if(c >= 1 && c <= 3 ) {
p = static_dialog_sheet[c];
do { /* sub menu */
printf("%s",p->menu);
scanf("%d",&c);
if(c == 0) {
c = 1;
break;
}
else if(c == 1) {
p = p->data_item.sub_list;
strcat(image, description[p->data_item.loc.id_x][p->data_item.loc.id_y]);
strcat(image, " ");
p = static_dialog_sheet[0];
break;
}
else if(c == 2) {
p = (list_t *)p->data_item.sub_list->next;
strcat(image, description[p->data_item.loc.id_x][p->data_item.loc.id_y]);
strcat(image, " ");
p = static_dialog_sheet[0];
break;
}
} while( c != 0 );
}
} while( c != 0 );

if(strlen(image) == 9)
printf("You have nothing!\n");
else
printf("%s\n", image);
}
/* main.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <conio.h>

#include "Glist.h"

int main() {
init_dialog_sheet();
main_menu();
destory_list(dialog_sheet);
getch();
return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:21
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

赫夫曼自动编码
/*
工程名称: W32_Huffman */
/*
Huffman.h */
#ifndef __HUFFMAN_H
#define __HUFFMAN_H

#include "huffman.cpp"

#endif
/* Huffman.cpp */
typedef unsigned int uint_t;

typedef struct huffman {
uint_t weight; /* 权值 */

struct huffman *lchild;
struct huffman *rchild;

} HuffmanTreeNode, *PHuffmanNode;

typedef struct lnklst {
PHuffmanNode HNode; /* 赫夫曼树 */

struct lnklst *next;
struct lnklst *prev;
} LnkListNode, *PLnkListNode;

typedef struct {
uint_t count; /* 树的棵数 */

PLnkListNode head; /* 链表结构,表示一行赫夫曼树 */
PLnkListNode tail;
} LnkList;

/* ===================================================================================================================== */
inline PHuffmanNode
create_huffman_node( uint_t weight ) {
PHuffmanNode node = (PHuffmanNode)malloc(sizeof(HuffmanTreeNode));
assert(node);

/* 确保权值有效 */
assert(weight > (uint_t)0);

/* 使用传入的权值初始化赫夫曼结点 */
node->weight = weight;
node->lchild = (PHuffmanNode)0;
node->rchild = (PHuffmanNode)0;

return node;
}

inline PLnkListNode
create_lnklst_node( PHuffmanNode node ) {
PLnkListNode lstNode = (PLnkListNode)malloc(sizeof(LnkListNode));
assert(lstNode);

/* 初始化链表结点 */
lstNode->HNode = node;
lstNode->next = (PLnkListNode)0;
lstNode->prev = (PLnkListNode)0;

return lstNode;
}

inline void
add_to_lnklist( LnkList *lst, PLnkListNode lstNode ) {
/* 却保lstNode有效 */
assert(lstNode);

/* 第一次添加结点 */
if(!lst->head) {
lst->head = lstNode;
lst->tail = lstNode;
lst->count = (uint_t)1;
}
else {
lst->tail->next = lstNode;
lstNode->prev = lst->tail;
lst->tail = lstNode;
++lst->count;
}
}

inline void
remove_from_lnklist( LnkList *lst, PLnkListNode lstNode ) {
/* 辅助结点 */
PLnkListNode pnode = (PLnkListNode)0;

assert(lstNode);

/* 如果结点为链表头 */
if(lstNode == lst->head) {
pnode = lstNode->next;
pnode->prev = (PLnkListNode)0;
lst->head = pnode;
--lst->count;
free(lstNode);
}
/* 如果是两表尾部 */
else if(lstNode == lst->tail) {
pnode = lstNode->prev;
pnode->next = (PLnkListNode)0;
lst->tail = pnode;
--lst->count;
free(lstNode);
}
else {
pnode = lstNode->next;
pnode->prev = lstNode->prev;
lstNode->prev->next = pnode;
--lst->count;
free(lstNode);
}

}

inline void
find_and_insert( LnkList *lst, PLnkListNode plst ) {
/* 根据赫夫曼树的节点权值,插入链表的适当位置 */
PLnkListNode pcur = (PLnkListNode)0;

if(!lst->head)
add_to_lnklist(lst,plst);
else {
pcur = lst->head;
while(pcur) {
if(plst->HNode->weight >= pcur->HNode->weight) {
if(pcur->next && plst->HNode->weight <= pcur->next->HNode->weight) {
plst->prev = pcur;
plst->next = pcur->next;
pcur->next->prev = plst;
pcur->next = plst;
++lst->count;
break;
}
else if(!pcur->next) {
add_to_lnklist(lst, plst);
break;
}
}
pcur = pcur->next;
}
}
}

/* ===================================================================================================================== */

inline void
swap_lnklst_node( PLnkListNode lstNode1, PLnkListNode lstNode2 ) {
PHuffmanNode pnode = (PHuffmanNode)0;

assert(lstNode1 && lstNode2);

/* 交换链表内容 */
pnode = lstNode1->HNode;
lstNode1->HNode = lstNode2->HNode;
lstNode2->HNode = pnode;
}

inline void
select_sort( LnkList *lst ) {
/* 根据权值做一个从小到大的选择排序 */
PLnkListNode cmp_node = lst->head;
PLnkListNode p;

assert(cmp_node);

while(cmp_node) {
p = cmp_node->next;
while(p) {
/* 如果目标位置权值比比较位置的权值小,则将它们交换 */
if(p->HNode->weight < cmp_node->HNode->weight)
swap_lnklst_node(cmp_node,p);
p = p->next;
}
cmp_node = cmp_node->next;
}
}

/* ===================================================================================================================== */

inline PHuffmanNode
merge_tree( PHuffmanNode root1, PHuffmanNode root2 ) {
/* 合并两棵赫夫曼树,并返回合并后新树根的指针 */
PHuffmanNode retRoot = (PHuffmanNode)0;

assert(root1 && root2);

/* 创建一棵合并后的赫夫曼树 */
retRoot = create_huffman_node(root1->weight + root2->weight);

retRoot->lchild = root1;
retRoot->rchild = root2;

return retRoot;
}

inline void
destory_tree( PHuffmanNode root ) {
/* 销毁一棵树 */
PHuffmanNode plchild, prchild;
if(root) {
plchild = root->lchild;
prchild = root->rchild;
free(root);
root = (PHuffmanNode)0;

destory_tree(plchild);
destory_tree(prchild);
}
}

/* ===================================================================================================================== */
inline void
convert_to_lnklst( LnkList *lst, uint_t Wi[], uint_t size ) {
/* 将权值数组转换成赫夫曼树并添加到链表 */
PLnkListNode plstnode = (PLnkListNode)0;
PHuffmanNode phufnode = (PHuffmanNode)0;
register uint_t i;

for(i = 0; i < size; ++i) {
phufnode = create_huffman_node(Wi[i]);
plstnode = create_lnklst_node(phufnode);
add_to_lnklist(lst,plstnode);
}

/* 排序链表 */
select_sort(lst);
}

inline PHuffmanNode
Huffman_code( uint_t Wi[], uint_t size) {
/* 根据权值数组进行赫夫曼编码,并返回赫夫曼树的树根 */
LnkList lst;
PLnkListNode p1,p2,pnode;
PHuffmanNode phnode;

/* 初始化链表 */
lst.count = (uint_t)0;
lst.head = (PLnkListNode)0;
lst.tail = (PLnkListNode)0;

/* 转换链表 */
convert_to_lnklst(&lst, Wi, size);

/* 开始合并树 */
while(lst.count > 1) {
/* 取两棵根权值最小的树进行合并,并将合并结果写入链表 */
p1 = lst.head;
p2 = lst.head->next;
/* 合并两树 */
phnode = merge_tree(p1->HNode, p2->HNode);
/* 为该树创建一个链表节点 */
pnode = create_lnklst_node(phnode);
/* 将新树添加入链表 */
find_and_insert(&lst,pnode);
/* 删除旧树的两个链表节点 */
remove_from_lnklist(&lst,p1);
remove_from_lnklist(&lst,p2);
}

/* 工作完成删除链表 */
/* 首先获取创建的赫夫曼树的根便于返回 */
phnode = lst.head->HNode;
pnode = lst.head;
while(pnode) {
p1 = pnode->next;
free(pnode);
pnode = p1;
}
lst.head = lst.tail = (PLnkListNode)0;

return phnode;
}

inline void
PreOrderTraverse( PHuffmanNode root, uint_t pos, char code[]) {
if(root) {

if(!root->lchild && !root->rchild)
printf("%u is coded to %s\n",root->weight, code);

code[pos] = '0';
PreOrderTraverse(root->lchild, pos+1, code);

code[pos] = '1';
PreOrderTraverse(root->rchild, pos+1, code);

}
}
/* main.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <conio.h>

#include "huffman.h"

int main() {
uint_t n = 0;
uint_t *Wi = (uint_t *)0;
register uint_t i;
char buf[80];

PHuffmanNode HuffmanTree;

memset(buf,(char)0,80 * sizeof(char));

printf("How many weight you wanna import ( 1...65535 ) ? ");
scanf("%u",&n);

Wi = (uint_t *)malloc(n * sizeof(uint_t));
assert(Wi);

printf("Input weights in turn \n");
for(i = 0; i < n; ++i)
scanf("%u",&Wi[i]);

HuffmanTree = Huffman_code(Wi, n);

PreOrderTraverse(HuffmanTree,0,buf);
getch();

destory_tree(HuffmanTree);
free(Wi);

return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:25
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

KMP模式匹配算法(不用next函数)
/* 工程名称: W32_KMP */
/* main.cpp */
#include <stdio.h>
#include <conio.h>

inline int KMP(char *s,char *p);

int main() {
char *s1 = "abacabcdabbab";
char *s2 = "abc";
printf("index = %d\n",KMP(s1,s2));
getch();
return 0;
}

/************************************************
*
* 函数名: KMP
*
* 参数表: s - 主串的指针; p - 模式串的指针
*
* 功能介绍: 比较p是否为s的子串,若是则返回在
* s中起始的索引号。
*
************************************************/

inline int
KMP(char *s, char *p) {
char *ptr_s = s; /* 映射字符串指针s */
char *ptr_p = p; /* 映射字符串指针p */
char *ptr_bak = s; /* 用于定位在*ptr_s与*p内容相同的位置 */

int addr_delta; /* 计算相匹配的内容长度,调整ptr_p对比起始位置 */

while(*ptr_s && *ptr_p) {
if(*ptr_s == *ptr_p) {
/* 如果匹配字符与对比串的首字符相等,则备份该指针 */
if(*ptr_s == *p)
ptr_bak = ptr_s;
++ptr_s;
++ptr_p;
}
else {
/* 计算新位置与备份指针的差,这便是以匹配的字符数,从而使p+addr_delta获得开始匹配的起始位置 */
addr_delta = (int)(ptr_s - ptr_bak);
/* 若备份位置与当前位置相同,则将当前位置一下下一个位置,从头开始比较 */
if(!addr_delta) {
++ptr_s;
ptr_bak = ptr_s;
ptr_p = p;
}
else {
ptr_p = p + addr_delta;
/* 若备份位置与模式串的首位置都不匹配当前位置的字符串,则继续向后比较,重设备份位置和匹配位置 */
if(*ptr_p != *ptr_s && *p != *ptr_s) {
++ptr_s;
ptr_bak = ptr_s;
ptr_p = p;
}
else {
/* 若备份位置与当前位置字符不匹但与模式串首字符相匹,则重设匹配位置和备份位置 */
if(*p == *ptr_s) {
ptr_p = p;
ptr_bak = ptr_s;
}
}
}
}
}
if(*ptr_p)
return -1;
else
/* 计算索引 */
return (int)((ptr_s - s) - (ptr_p - p));
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:26
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

一元多项式加减乘
/* 工程名称: W32_Polynomial */
/* Polynomial.h */
#ifndef __POLYNOMIAL_H
#define __POLYNOMIAL_H

#define TRUE 0x01
#define FALSE 0x00

/* 错误代码 */
#define ERR_ON_SUCCESS 0x00 /* 没有错误 */
#define ERR_STACK_OVERRUN 0x01 /* 堆栈溢出 */
#define ERR_ITEM_EXIST 0x02 /* 欲添加项已存在 */
#define ERR_PTR_INVAILD 0x04 /* 无效的指针传入 */
#define ERR_LNKLST_INVAILD 0x08 /* 无效的链表 */
#define ERR_ACTCODE_INVAILD 0x10 /* 无效的操作码 */
#define ERR_ADD_IMPOSSIBLE 0x20 /* 无法做加运算 */
#define ERR_SUB_IMPOSSIBLE 0x40 /* 无法做减运算 */
#define ERR_EMPTY_POLY 0x80 /* 多项式链表为空 */
#define ERR_UNKNOWN 0xff /* 位置错误 */

/* 操作码 */
#define ACT_ADD 0x01 /* 多项式加 */
#define ACT_SUB 0x02 /* 多项式减 */
#define ACT_MUP 0x03 /* 多项式乘 */

/* 嵌入式功能配置常数 */
#define AUTO_DESTORY_EXIST FALSE /* 欲插入的项已存在时,自动删除创建的项 */
#define CHK_PARAMETER_PTR TRUE /* 检查作为参数传入的指针是否有效 */
#define RAISE_ERR TRUE /* 错误报告函数 */
#define CRITICAL_ABORT TRUE /* 错误异常中断 */
#define COPY_LNKLST TRUE /* 链表复制函数 */
#define SIMPLE_OUTPUT TRUE /* 简单的多项式输出函数 */
#define SIMPLE_INPUT TRUE /* 简单的多项式输入函数 */

#define EPSILON 1E-6 /* 极小值定义,用于浮点数趋近比较 */

#include "Polynomial.cpp"

#endif
/* Polynomial.cpp */
typedef struct Polynomial {
float p;
int e;
struct Polynomial *next;
struct Polynomial *prev;
} poly;

typedef struct {
poly *head;
poly *tail;
int length;
} poly_lnklst;

typedef unsigned int uint;

#if RAISE_ERR == TRUE
inline void
raise_err(uint err) {
switch(err) {
case ERR_STACK_OVERRUN:
printf("Error %#x : Stack overrun\n", err);
break;
case ERR_ITEM_EXIST:
printf("Error %#x : Item is already exist\n",err);
break;
case ERR_PTR_INVAILD:
printf("Error %#x : Invaild pointer\n",err);
break;
case ERR_LNKLST_INVAILD:
printf("Error %#x : Invaild Linked-List\n",err);
break;
case ERR_ACTCODE_INVAILD:
printf("Error %#x : Invaild Action Code\n",err);
break;
case ERR_ADD_IMPOSSIBLE:
printf("Error %#x : Item cannot be added\n",err);
break;
case ERR_SUB_IMPOSSIBLE:
printf("Error %#x : Item cannot be subtracted\n",err);
break;
case ERR_EMPTY_POLY:
printf("Error %#x : Polynomial grows empty!\n",err);
break;
case ERR_UNKNOWN:
printf("Error %#x : Unknown error\n",err);
break;
}
}
#endif

inline uint
destory_polynomial(poly_lnklst *plst) {
poly *p = (poly *)0;
poly *tmp = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
#endif

p = plst->head;
/* 逐一释放链表项目 */
while(p) {
tmp = p->next;
free(p);
p = tmp;
}

/* 释放两表容器 */
free(plst);
plst = (poly_lnklst *)0;
return ERR_ON_SUCCESS;
}

inline uint
init_poly_lnklst(poly_lnklst *plst) {
#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
#endif

plst->head = (poly *)0;
plst->tail = (poly *)0;
plst->length = (int)0;

return ERR_ON_SUCCESS;
}

inline poly *
create_poly_item(float p, int e, uint *err) {
poly *item =
(poly *)malloc(sizeof(poly));
if(!item) {
*err = ERR_STACK_OVERRUN;
return (poly *)0;
}
item->p = p;
item->e = e;
item->next = (poly *)0;
item->prev = (poly *)0;
*err = ERR_ON_SUCCESS;
return item;
}

#if COPY_LNKLST == TRUE
inline poly_lnklst *
copy_polynomial(poly_lnklst *plstSrc,uint *err) {
poly *pSrc = (poly *)0;
poly *pDes = (poly *)0;
poly *tmp = (poly *)0;
poly_lnklst *plstDes = (poly_lnklst *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plstSrc) {
*err = ERR_LNKLST_INVAILD;
return (poly_lnklst *)0;
}
#endif

plstDes = (poly_lnklst *)malloc(sizeof(poly_lnklst));
if(!plstDes) {
*err = ERR_STACK_OVERRUN;
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}

/* 复制表首 */
pSrc = plstSrc->head;
if(!pSrc) {
free(plstDes);
*err = ERR_EMPTY_POLY;
return (poly_lnklst *)0;
}
pDes = create_poly_item(pSrc->p,pSrc->e,err);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}
plstDes->head = plstDes->tail = pDes;
plstDes->length = plstSrc->length;
/* 逐一复制余下的项目 */
pSrc = pSrc->next;
while(pSrc) {
tmp = create_poly_item(pSrc->p,pSrc->e,err);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}
/* 创建链表的节点连接 */
pDes->next = tmp;
tmp->prev = pDes;
pDes = tmp;
plstDes->tail = tmp;
pSrc = pSrc->next;
}
return plstDes;
}
#endif

inline uint
connect_to_lst(poly_lnklst *plst, poly *item) {
poly *p = (poly *)0,
*tmp = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
if(!item)
return ERR_PTR_INVAILD;
#endif

if(!plst->head) {
plst->head = plst->tail = item;
item->next = item->prev = (poly *)0;
plst->length = (int)1;
}
else {
p = plst->head;
while(p) {
tmp = p->next;
#if AUTO_DESTORY_EXIST == TRUE
if(p->e == item->e) {
free(item);
item = (poly *)0;
return ERR_ITEM_EXIST;
}
#else
if(p->e == item->e)
return ERR_ITEM_EXIST;
#endif
if(tmp) {
if((p->e < item->e) && (tmp->e > item->e)) {
item->prev = p;
item->next = tmp;
p->next = item;
tmp->prev = item;
++plst->length;
break;
}
}
else {
if(p->e > item->e && !p->prev) {
item->prev = (poly *)0;
item->next = p;
p->prev = item;
plst->head = item;
++plst->length;
break;
}
else {
item->prev = p;
item->next = (poly *)0;
p->next = item;
plst->tail = item;
++plst->length;
break;
}
}
p = tmp;
}
}
return ERR_ON_SUCCESS;
}

inline uint
remove_item(poly_lnklst *plst, poly *item) {
poly *tmp_prev = (poly *)0;
poly *tmp_next = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
if(!plst->head || !item)
return ERR_PTR_INVAILD;
#endif

tmp_prev = item->prev;
tmp_next = item->next;

if(tmp_prev) {
tmp_prev->next = tmp_next;
if(tmp_next)
tmp_next->prev = tmp_prev;
else
plst->tail = tmp_prev;
}
else {
plst->head = tmp_next;
if(!tmp_next)
return ERR_EMPTY_POLY;
if(!tmp_next->next)
plst->tail = tmp_next;
tmp_next->prev = (poly *)0;
}

--plst->length;
free(item);
item = (poly *)0;
return ERR_ON_SUCCESS;
}

inline uint
merge_item(poly *des, poly *src, uint code) {
poly *tmp = (poly *)0;

poly *tmp_prev = (poly *)0;
poly *tmp_next = (poly *)0;

#if CHK_PARAMETER_PTR == TRUE
if(!des || !src)
return ERR_PTR_INVAILD;
#endif

switch(code) {
case ACT_ADD:
if(des->e == src->e) {
des->p += src->p;
return ERR_ON_SUCCESS;
}
else
return ERR_ADD_IMPOSSIBLE;
case ACT_SUB:
if(des->e == src->e) {
des->p -= src->p;
return ERR_ON_SUCCESS;

}
else
return ERR_SUB_IMPOSSIBLE;
case ACT_MUP:
des->p *= src->p;
des->e += src->e;
return ERR_ON_SUCCESS;
default:
return ERR_ACTCODE_INVAILD;
}
}

inline uint
find_and_merge(poly_lnklst *plst) {
poly *tmp = (poly *)0;
poly *tmp_next = (poly *)0;
poly *tmp_prev = (poly *)0;
poly **container = (poly **)0;
register int i = 0;
register int j;
uint err;

#if CHK_PARAMETER_PTR == TRUE
if(!plst)
return ERR_LNKLST_INVAILD;
if(!plst->head)
return ERR_PTR_INVAILD;
#endif

tmp = plst->head;
container = (poly **)malloc(sizeof(poly *) * plst->length);

container[0] = tmp;
tmp = tmp->next;
while(tmp) {
for(j = i; j >=0; --j) {
if(!container[j])
continue;
if(container[j]->e == tmp->e) {
err = merge_item(tmp,container[j],ACT_ADD);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

err = remove_item(plst,container[j]);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

/* 如果系数为0 */
if(tmp->p <= EPSILON) {
tmp_next = tmp->next;
err = remove_item(plst,tmp);

if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

tmp = tmp_next;
}
else
tmp = tmp->next;
break;
}
}
/* 如果没有合并,则存入容器 */
if(j < 0) {
container[++i] = tmp;
tmp = tmp->next;
}
}
free(container);
return ERR_ON_SUCCESS;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:33
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

inline uint
polynomial_opr(poly_lnklst *plstDes, poly_lnklst *plstSrc, uint oprcode) {
poly *pSrc = (poly *)0;
poly *pDes = (poly *)0;
poly *tmp = (poly *)0;
poly_lnklst *plstOpr = (poly_lnklst *)0;
poly_lnklst *plstRet = (poly_lnklst *)0;

uint err;

#if CHK_PARAMETER_PTR == TRUE
if(!plstDes || !plstSrc)
return ERR_LNKLST_INVAILD;
if(!plstDes->head || !plstSrc->head)
return ERR_PTR_INVAILD;
#endif

pSrc = plstSrc->head;

switch(oprcode) {
case ACT_ADD:
case ACT_SUB:
while(pSrc) {
pDes = plstDes->head;
while(pDes) {
if(pSrc->e == pDes->e) {
err = merge_item(pDes,pSrc,oprcode);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}

/* 如果相加后的系数为0,则删除此项 */
if(pDes->p <= EPSILON) {
tmp = pDes->next;
err = remove_item(plstDes,pDes);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
pDes = tmp;
}
break;
}
else {
if(pDes->next) {
if((pSrc->e > pDes->e) && (pSrc->e < pDes->next->e)) {
tmp = create_poly_item(pSrc->p,pSrc->e,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
tmp->prev = pDes;
tmp->next = pDes->next;
pDes->next->prev = tmp;
pDes->next = tmp;
++plstDes->length;
break;
}
}
else {
tmp = create_poly_item(pSrc->p,pSrc->e,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
if(pDes->e > tmp->e && !pDes->prev) {
tmp->prev = (poly *)0;
tmp->next = pDes;
pDes->prev = tmp;
plstDes->head = tmp;
++plstDes->length;
break;
}
else {
plstDes->tail->next = tmp;
tmp->prev = plstDes->tail;
tmp->next = (poly *)0;
plstDes->tail = tmp;
++plstDes->length;
break;
}
}
}
pDes = pDes->next;
}
pSrc = pSrc->next;
}
return ERR_ON_SUCCESS;
case ACT_MUP:
/* 复制旧链表 */
plstOpr = copy_polynomial(plstDes,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 首先计算第一项,保存到返回链表 */
pDes = plstOpr->head;
while(pDes) {
err = merge_item(pDes,pSrc,ACT_MUP);
pDes = pDes->next;
}
plstRet = copy_polynomial(plstOpr,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 删除plstOpr */
err = destory_polynomial(plstOpr);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 计算其余项 */
pSrc = pSrc->next;
while(pSrc) {
plstOpr = copy_polynomial(plstDes,&err);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
pDes = plstOpr->head;
while(pDes) {
err = merge_item(pDes,pSrc,ACT_MUP);
pDes = pDes->next;
}
/* 两链表相加 */
err = polynomial_opr(plstRet,plstOpr,ACT_ADD);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 删除plstOpr */
err = destory_polynomial(plstOpr);
if(err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return err;
#endif
}
/* 计算下一项 */
pSrc = pSrc->next;
}
/* 清除原表达式 */
pDes = plstDes->head;
while(pDes) {
pSrc = pDes->next;
free(pDes);
pDes = pSrc;
}
plstDes->head = plstRet->head;
plstDes->tail = plstRet->tail;
plstDes->length = plstRet->length;

free(plstRet);

return ERR_ON_SUCCESS;
default:
return ERR_ACTCODE_INVAILD;
}
}

#if SIMPLE_OUTPUT == TRUE
inline void
print_polynomial(poly_lnklst *plst) {
poly *p = plst->head;
printf("P = %c%.1fx^%d",p->p > 0 ? '\0' : '-',p->p,p->e);
p = p->next;
while(p) {
printf("%c%.1fx^%d",p->p > 0 ? '+' : '-',p->p,p->e);
p = p->next;
}
printf("\n");
}
#endif

#if SIMPLE_INPUT == TRUE
inline poly_lnklst *
input_polynomial(uint *err) {
poly *item = (poly *)0;
poly_lnklst *plst = (poly_lnklst *)malloc(sizeof(poly_lnklst));

int item_count = 0;
int i = 0;

*err = init_poly_lnklst(plst);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
return (poly_lnklst *)0;
#endif
}

printf("Input how many items do you want to constructure a polynomial? ");
scanf("%d",&item_count);

while(item_count > 0) {
item = (poly *)malloc(sizeof(poly));
if(!item) {
*err = ERR_STACK_OVERRUN;
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if CRITICAL_ABORT == TRUE
exit(1);
#else
free(plst);
return (poly_lnklst *)0;
#endif
}
printf("(p,e) ? ");
scanf("%f,%d",&item->p,&item->e);
*err = connect_to_lst(plst,item);
if(*err != ERR_ON_SUCCESS) {
#if RAISE_ERR == TRUE
raise_err(*err);
#endif
#if AUTO_DESTORY_EXIST == FALSE
free(item);
#endif
continue;
}
--item_count;
}
*err = ERR_ON_SUCCESS;
return plst;
}
#endif
/* main.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#include "Polynomial.h"

int main() {
poly_lnklst *plstA, *plstB;
uint err;
/* inputs */
printf("Input polynomial A\n");
plstA = input_polynomial(&err);
printf("Input polynomial B\n");
plstB = input_polynomial(&err);
/* mutiply */
err = polynomial_opr(plstA,plstB,ACT_MUP);
print_polynomial(plstA);
getch();
err = destory_polynomial(plstA);
err = destory_polynomial(plstB);
return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:34
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

迷宫算法(用栈实现)
/* 工程名称: W32_Maze */
/* Maze.h */
#ifndef __MAZE_H
#define __MAZE_H

#define TRUE 0x01
#define FALSE 0x00

#define DEFAULT_MAZE TRUE

#define PATH_BLOCK 0x00
#define PATH_WALK_ON 0x01
#define PATH_FOOT_PRINT 0x02

#define MAX_COL 10

#include "Maze.cpp"

#endif
/* Maze.cpp */
/* 无符号整型 */
typedef unsigned int uint_t;
/* 地图数据 */
typedef int map_t;

typedef struct track {
/* 在迷宫矩阵中的位置 */
uint_t x;
uint_t y;

/* 可用方向 */
uint_t direct;

struct track *next;
struct track *prev;
} maze_track;

typedef struct {
/* 路径 */
maze_track *track_ptr;
/* 迷宫出口的位置 */
uint_t endx;
uint_t endy;
/* 当前栈的长度 */
int size;
} maze_path;

int count = 0;

/* 方向查询表 */
uint_t directInqTbl[4] = {0x01, 0x02, 0x04, 0x08};
/* 方向使用表 */
uint_t directUseTbl[4] = {0xE, 0xD, 0xB, 0x7};

/* 方向算数常数 */
int directTbl[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};

/* 墙、路和足迹的符号集 */
unsigned char symbolic[3] = { '#', 0x20, '*' };
//unsigned char symbolic[3] = {0x2A, 0x20, '#'};

#if DEFAULT_MAZE == TRUE
static map_t def_maze[10][10] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 1, 1, 1, 0, 1, 0,
0, 1, 1, 0, 1, 1, 1, 0, 1, 0,
0, 1, 1, 1, 1, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 1, 1, 0, 1, 0,
0, 1, 1, 1, 0, 1, 1, 0, 1, 0,
0, 1, 0, 1, 1, 1, 0, 1, 1, 0,
0, 1, 0, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#endif

inline maze_path *
create_path(void) {
maze_path *path = (maze_path *)malloc(sizeof(maze_path));
assert(path);
path->track_ptr = (maze_track *)0;
path->size = (int)0;
return path;
}

inline void
generate_enterance(maze_path *path,uint_t x, uint_t y) {
if(path && !path->size) {
path->track_ptr = (maze_track *)malloc(sizeof(maze_track));
assert(path->track_ptr);
path->track_ptr->direct = (uint_t)0;
path->track_ptr->x = x;
path->track_ptr->y = y;
path->track_ptr->next = (maze_track *)0;
path->track_ptr->prev = (maze_track *)0;
++path->size;
}
}

inline void
designate_exit(maze_path *path, uint_t x, uint_t y) {
if(path) {
path->endx = x;
path->endy = y;
}
}

inline void
print_maze(map_t maze[][MAX_COL], uint_t row, uint_t col) {
register uint_t i,j;
for(i = 0; i < row; ++i) {
for(j = 0; j < col; ++j)
printf("%c",*(symbolic + maze[i][j]));
printf("\n");
}
}

inline uint_t
find_my_way(map_t maze[][MAX_COL], uint_t row, uint_t col, maze_path *path) {
register int i,j;
uint_t x;
uint_t y;
uint_t ex;
uint_t ey;

maze_track *ptrack = (maze_track *)0;
maze_track *ptmp = (maze_track *)0;

if(path) {
/* 设置入口方向量 */
assert(path->track_ptr);
for(i = 0; i < 4; ++i) {
x = path->track_ptr->x + directTbl[i][0];
y = path->track_ptr->y + directTbl[i][1];
if( (x < 0 || x >= col) || (y < 0 || y >= row))
continue;
if(maze[y][x] == PATH_WALK_ON)
path->track_ptr->direct |= directInqTbl[i];
}
ptrack = path->track_ptr;
ex = path->endx;
ey = path->endy;
while(ptrack->x != ex || ptrack->y != ey) {
/* 获取行进方向 */
for(i = 0; i < 4; ++i) {
/* 方向有效,则该方向被使用 */
if( ((ptrack->direct & directInqTbl[i]) >> i) == 0x01 ) {
ptrack->direct &= directUseTbl[i];
break;
}
}
/* 当入口方向量用完,则宣告无解 */
if(i == 4) {
if(!ptrack->prev) {
printf("No way to solve!\n");
return FALSE;
}
/* 当最后个路径节点用完方向量,则折回 */
else {
/* 清除足迹 */
maze[ptrack->y][ptrack->x] = PATH_WALK_ON;
ptmp = ptrack->prev;
ptmp->next = (maze_track *)0;
free(ptrack);
ptrack = ptmp;
--path->size;
++count;
continue;
}
}

ptmp = (maze_track *)malloc(sizeof(maze_track));
assert(ptmp);
ptrack->next = ptmp;
++path->size;
/* 创建链表结构,在旧位置处留下足迹 */
maze[ptrack->y][ptrack->x] = PATH_FOOT_PRINT;

/* 设置新位置 */
ptmp->x = ptrack->x + directTbl[i][0];
ptmp->y = ptrack->y + directTbl[i][1];
ptmp->direct = (uint_t)0;

ptmp->prev = ptrack;
ptrack = ptmp;
ptmp->next = (maze_track *)0;

/* 为下一个路径节点获取方向信号 */
for(j = 0; j < 4; ++j) {
x = ptrack->x + directTbl[j][0];
y = ptrack->y + directTbl[j][1];
if( (x < 0 || x >= col) || (y < 0 || y >= row))
continue;
if(maze[y][x] == PATH_WALK_ON && maze[y][x] != PATH_FOOT_PRINT)
ptrack->direct |= directInqTbl[j];
}
}
maze[ey][ex] = PATH_FOOT_PRINT;
return TRUE;
}
return FALSE;
}

inline void
destory_path(maze_path *path) {
assert(path);

maze_track *track = path->track_ptr;
maze_track *tmp = (maze_track *)0;

while(track) {
tmp = track->next;
free(track);
track = tmp;
}

free(path);
path = (maze_path *)0;
}
/* main.cpp */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <assert.h>

#include "Maze.h"

int main() {
uint_t ret;
/* 创建路径链 */
maze_path *path = create_path();
maze_track *p;
/* 初始化入口 */
generate_enterance(path,1,1);
/* 初始化终点 */
designate_exit(path,8,8);

printf("Before explorer\n");
print_maze(def_maze,10,10);
/* 开始探索 */
ret = find_my_way(def_maze,10,10,path);
if(ret == TRUE) {
p = path->track_ptr;
printf("(%u,%u)",p->x, p->y);
p = p->next;
while(p) {
printf("->(%u,%u)",p->x, p->y);
p = p->next;
}
printf("\n");

printf("\nAfter explorer\n");
print_maze(def_maze,10,10);
}
printf("\nReturn count = %d\n",count);
getch();
destory_path(path);
return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:36
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

贴了这些算法希望和大家学习讨论使用,并非鼓励大家抄袭作业~


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-10-28 19:38
快速回复:自学数据结构所写的一些算法~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022874 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved