随笔分类 - 数据结构与算法
摘要:不相交集类型声明及函数实现: /* disjoint_set.h */#ifndef _DISJOINT_SET_H#define _DISJOINT_SET_H#define NUMSETS 5typedef int disjoint_set[NUMSETS + 1];typedef int set_type;typedef int element_type;void initia...
阅读全文
摘要:功能:查找集合S中第k个最小元。 快速选择算法修改自快速排序算法,当算法终止时,第k个最小元就在位置k上。这破坏了原来的排序;如果不希望这样,那么需要做一份拷贝。 快速选择函数: /* quick_select.h */#ifndef _QUICK_SELECT_H#define _QUICK_SELECT_Hvoid qselect(int array[], int k, int l...
阅读全文
摘要:快速排序函数: /* quick_sort.h */#ifndef _QUICK_SORT_H#define _QUICK_SORT_H#define CUTOFF (3)void quick_sort(int array[], int n); //快速排序的驱动例程int median3(int array[], int left, int right)...
阅读全文
摘要:通用函数: /* common.h */#ifndef _COMMON_H#define _COMMON_Hvoid swap(int *ap, int *bp);void print_array(const int array[], int n);#endif /* common.c */#include "common.h"#include void swap(int *ap, in...
阅读全文
摘要:通用函数: /* common.h */#ifndef _COMMON_H#define _COMMON_Hvoid swap(int *ap, int *bp);void print_array(const int array[], int n);#endif /* common.c */#include "common.h"#include void swap(int *ap, in...
阅读全文
摘要:实例功能:接收一个含有整数元素的数组和一个包含元素个数的整数,将数组中的元素从小到大重新排序。并输出排序前后的数组。 下面以模块划分的思想来实现此功能。 打印数组元素模块: /* common.h */#ifndef _COMMON_H#define _COMMON_Hvoid print_array(const int array[], int n);#endif /* commo...
阅读全文
摘要:实例功能:接收一个含有整数元素的数组和一个包含元素个数的整数,将数组中的元素从小到大重新排序。并输出排序前后的数组。 下面以模块划分的思想来实现此功能。 打印数组元素模块: /* common.h */#ifndef _COMMON_H#define _COMMON_Hvoid print_array(const int array[], int n);#endif /* commo...
阅读全文
摘要:/* bin_heap.h */#ifndef _BIN_HEAP_H#define _BIN_HEAP_Hstruct heap_struct;typedef struct heap_struct *priority_queue;priority_queue initialize(int max_elements);void destroy(priority_queue h);void m...
阅读全文
摘要:/* hash_sep.h */#ifndef _HASH_SEP_H#define _HASH_SEP_H#define MIN_TABLE_SIZE 5struct list_node;typedef struct list_node *position;struct hash_tbl;typedef struct hash_tbl *hash_table;typedef unsi...
阅读全文
摘要:分离链接法(separate chaining):将散列到同一个值的所有元素保留到一个表中(用指针实现的单链表)。 /* 实现分离链接法所需要的类型声明 */#ifndef _HashSep_H#define _HahsSep_Hstruct ListNode;typedef struct ListNode *Position;struct HashTbl;typedef struct H...
阅读全文
摘要:/* hash function */typedef unsigned int Index; IndexHash( const char *Key, int TableSize ) //Key为要散列的关键字, TableSize为散列表的大小{ unsigned int HashVal = 0; while( *Key != '\0' ) HashV...
阅读全文
摘要:参考自:http://flynoi.blog.hexun.com/31272178_d.html 霍纳法则简介 假设有n+2个实数a0,a1,…,an,和x的序列,要对多项式Pn(x)= anxn+an-1xn-1+…+a1x+a0求值,直接方法是对每一项分别求值,并把每一项求的值累加起来,这种方法十分低效,它需要进行n+(n-1)+…+1=n(n+1)/2次乘法运算和n次加法运算。有没有更高...
阅读全文
摘要:/* AVL树的节点声明 */#ifndef _AVLTREE_H#define _AVLTREE_Hstruct AvlNode;typedef struct AvlNode *Position;typedef struct AvlNode *AvlTree;AvlTree MakeEmpty(AvlTree T);Position Find(ElementType X, AvlTree ...
阅读全文
摘要:AVL(Adelson-Velskii和Landis)树是带有平衡条件的二叉查找树。 一棵AVL树是其每个节点的左子树和右子树的高度最多差1的二叉查找树。(空子树的高度定义为-1) AVL树的每一个节点(在其节点结构中)保留高度信息。 在高度为h的AVL树中,最少节点数S(h)由S(h)=S(h-1)+S(h-2)+1给出。对于h=0,S(h)=1;h=1,S(h)=2。可以看出函数S(h)...
阅读全文
摘要:/* search_tree.h */#ifndef _SEARCH_TREE_H#define _SEARCH_TREE_Hstruct tree_node;typedef struct tree_node *position;typedef struct tree_node *search_tree;search_tree make_empty(search_tree t);positi...
阅读全文
摘要:二叉查找树:对于树中的每个节点X,它的左子数种所有关键字值小于X的关键字,而它的右子树种所有关键字值大于X的关键字值。 /* 二叉查找树声明 */#ifndef _TREE_Hstruct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;SearchTree MakeEmpty( ...
阅读全文
摘要:/* queue.h */#ifndef _QUEUE_H#define _QUEUE_Hstruct queue_record;typedef struct queue_record *queue;int is_empty( queue q );int is_full( queue q );queue create_queue( int max_elements );void dispos...
阅读全文
摘要:队列的数组实现 /* 队列的类型声明 */ #ifndef _Queue_hstruct QueueRecord;typedef struct QueueRecord *Queue;int IsEmpty( Queue Q );int IsFull( Queue Q );Queue CreateQueue( int MaxElements );v...
阅读全文
摘要:声明:本程序读入一个中缀表达式,将该中缀表达式转换为后缀表达式并输出后缀表达式。 注意:支持+、-、*、/、(),并且输入时每输入完一个数字或符号都要加一个空格,特别注意的是在整个表达式输入完成时也要加一个空格后再回车。这是该程序的一个不足之处,有待改进。 /* infix_to_postfix.c */#include #include #include #include struct...
阅读全文
摘要:用户输入一个后缀表达式,程序计算该后缀表达式的值并输出结果: /* postfix_expression.c */#include "stack.h"#include #include #include int main(){ int i, flag; char c, data_string[10]; float data, f1, f2, result; stac...
阅读全文