数据结构&算法
ruan875417
这个作者很懒,什么都没留下…
展开
-
【线性表】顺序表
SeqList.h#ifndef SEQLIST_H#define SEQLIST_H#includeconst int default_size = 100;//SeqListtemplateclass SeqList{public: typedef T value_type; typedef T* pointer; SeqList(int size = defaul原创 2015-11-07 15:07:22 · 353 阅读 · 0 评论 -
【图】BFS和DFS
一、BFS#include#includeusing namespace std;const int max_num = 100;class BFS{public: typedef int VertexType; typedef int EdgeType; BFS(); void create_graph(); void bfs_traverse();private:原创 2016-03-15 20:18:15 · 352 阅读 · 0 评论 -
【图】最短路径Dijkstra算法和Floyd算法
一、Dijkstra算法#includeusing namespace std;const int maxint = 10000;class Dij{public: void initial(int m, int n); void read_case(); void dijkstra(int begin, int end); void search_path(int begi原创 2016-03-30 19:46:53 · 798 阅读 · 0 评论 -
【图】邻接矩阵和邻接表
#includeusing namespace std;class AdjacencyMatrix{public: typedef int VertexType; typedef int EdgeType; AdjacencyMatrix(); ~AdjacencyMatrix(); void create_graph(); void print_graph();privat原创 2016-03-07 20:30:19 · 610 阅读 · 0 评论 -
【树】Trie树
#include#includeusing namespace std;//Trie树结点struct TrieNode{ bool is_string;//标记该结点处是否构成单词 TrieNode* next[26]; TrieNode() :is_string(false){ for (int i = 0; i < 26; i++) next[i] = NULL;原创 2016-02-23 16:10:54 · 309 阅读 · 0 评论 -
【图】最短路径Bellman-Ford算法
#includeusing namespace std;const int max_num = 500;const int max_len = 10000;typedef struct Edge{ int begin;//起点 int end;//终点 int weight;//权值 }Edge;class BellmanFord{public:原创 2016-04-04 22:39:47 · 740 阅读 · 0 评论 -
【图】最小生成树Prim算法和Kruskal算法
一、Prim算法#includeusing namespace std;const int max = 10000;class Hdu1233{public: void initial(int n); void read_case(); void prim(); void print_result();private: int path[101][101]; int d原创 2016-03-23 20:42:48 · 476 阅读 · 0 评论 -
【图】拓扑排序
#include#include#includeusing namespace std;class Hdu3342{public: void initial(int n, int m); void read_case(); bool topological_sort(); void print_result();private: vector vec[101];//节点的邻原创 2016-04-10 15:28:41 · 360 阅读 · 0 评论 -
【排序】冒泡排序、选择排序、插入排序、希尔排序
#includeusing namespace std;void swap(int* a, int* b){ int temp = *a; *a = *b; *b = temp;}void bubble_sort1(int nums[],int len){ for (int i = 0; i < len; i++){ for (int j = len - 1; j>i; j原创 2016-05-14 13:38:51 · 336 阅读 · 0 评论 -
【排序】堆排序
MyHeap.h#ifndef MY_HEAP_H #define MY_HEAP_H #include #include #define max_value -99999999 //仿函数 templatestruct MyLess{ bool operator()(const T& x, const T& y) const { return x < y原创 2016-05-14 13:44:04 · 314 阅读 · 0 评论 -
【排序】归并排序(递归和非递归版本)
#includeusing namespace std;void merge(int* a, int* temp, int begin, int middle, int end){ int i = begin; int j = middle + 1; int k = 0; while (i <= middle&&j <= end){//比较两个指针所指向的元素,选择相对小的元素放入到原创 2016-05-14 13:47:36 · 2414 阅读 · 0 评论 -
【排序】快速排序
#includeusing namespace std;int pnumsrtition(int nums[], int low, int high){ int pivot_key = nums[low];//用区间的第1个记录作为基准 while (low < high){//从区间两端交替向中间扫描,直到low=high为止 while (low = pi原创 2016-05-14 13:49:08 · 382 阅读 · 0 评论 -
【排序】基数排序
#includeusing namespace std;//求数据的最大位数int get_max_bit(int nums[], int len){ int count; int max = 1; int* temp = new int[len]; for (int i = 0; i < len; i++) temp[i] = nums[i]原创 2016-05-14 13:54:13 · 407 阅读 · 0 评论 -
【排序】桶排序
#includeusing namespace std;typedef struct node{ int key; struct node* next; node(int k) :key(k), next(NULL){}}node;void bucket_sort(int nums[],int len,int bucket_size){ node** bucket_table原创 2016-05-14 14:00:56 · 436 阅读 · 0 评论 -
【树】哈弗曼树和哈弗曼编码
#include#includeusing namespace std;const int max_size = 50;struct HuffmanNode{ int weight;//weight表示权值数据域 int parent;//par=0表明结点是独立的,par>0为它的双亲下标 int lchild, rchild;//lchild, rchild为左原创 2016-01-13 16:37:08 · 473 阅读 · 0 评论 -
【树】已知二叉树前序和中序遍历求后序遍历,及中序和后序遍历求前序遍历
#includeusing namespace std;//已知二叉树前序遍历和中序遍历,求后序遍历void binary_tree_postorder(char* preorder,char* inorder,int length){ if (length == 0) return; char root = *preorder; int index = 0; for (; in原创 2016-01-11 13:30:00 · 769 阅读 · 0 评论 -
【线性表】双向循环链表
DualCirculationList.h#ifndef DUALCIRCULATIONLIST_H#define DUALCIRCULATIONLIST_H#include//list nodetemplatestruct list_node{ typedef list_node* pointer; T data; pointer prior; p原创 2015-11-08 18:38:46 · 336 阅读 · 0 评论 -
【线性表】静态链表
StaticLinkedList.h#ifndef STATICLINKEDLIST_H#define STATICLINKEDLIST_H#includeconst int max_size = 1000;//list nodetemplatestruct list_node{ T data; int cur;//cursor};//StaticLinkedList原创 2015-11-08 20:52:32 · 374 阅读 · 0 评论 -
【线性表】单链表
SingleLinkedList.h#ifndef SINGLELINKEDLIST_H#define SINGLELINKEDLIST_H#include//list nodetemplatestruct list_node{ typedef list_node* pointer; T data; pointer next; list_node() :next(NULL)原创 2015-11-08 14:16:25 · 325 阅读 · 0 评论 -
【栈】顺序栈
SeqStack.h#ifndef SEQSTACK_H#define SEQSTACK_H#includeint default_size = 20;// SeqStacktemplateclass SeqStack{public: typedef T& reference; SeqStack(int size = default_size);//constructor原创 2015-11-11 21:38:39 · 340 阅读 · 0 评论 -
【栈】链式栈
LinkedStack.h#ifndef LINKEDSTACK_H#define LINKEDSTACK_H#include//Stack nodetemplatestruct stack_node{ typedef stack_node* pointer; T data; pointer next; stack_node() :next(NULL){} stack_n原创 2015-11-11 21:48:46 · 318 阅读 · 0 评论 -
【队列】循环队列
CircularQueue.h#ifndef CIRCULARQUEUE_H#define CIRCULARQUEUE_Hint default_size = 10;#include//CircularQueuetemplateclass CircularQueue{public: typedef T& reference; CircularQueue(int size原创 2015-11-14 10:36:38 · 334 阅读 · 0 评论 -
【队列】链式队列
LinkedQueue.h#ifndef LINKEDQUEUE_H#define LINKEDQUEUE_h#include//queue nodetemplatestruct queue_node{ typedef queue_node* pointer; T data; pointer next; queue_node() :next(NULL){} queue_n原创 2015-11-14 13:57:13 · 360 阅读 · 0 评论 -
【栈】两栈共享空间
SeqDoubleStack.h#ifndef SEQDOUBLESTACK_H#define SEQDOUBLESTACK_H#includeint default_size = 20;// SeqDoubleStacktemplateclass SeqDoubleStack{public: typedef T& reference; SeqDoubleStack(int原创 2015-11-15 10:38:22 · 371 阅读 · 0 评论 -
【栈,队列】两队列实现栈
一、用STL的queue实现MyStack.h#ifndef MYSTACK_H#define MYSTACK_H#include#include//MyStacktemplateclass MyStack{public: typedef T value_type; typedef T& reference; MyStack();//constructor ~MyS原创 2015-11-21 15:17:25 · 454 阅读 · 0 评论 -
【栈,队列】两栈实现队列
一、用STL中的stack实现MyQueue.h#ifndef MYQUEUE_H#define MYQUEUE_h#include#include//MyQueuetemplateclass MyQueue{public: typedef T value_type; typedef T& reference; MyQueue();//constructor ~My原创 2015-11-15 14:00:12 · 328 阅读 · 0 评论 -
【树】二叉树的各种操作
BinaryTree.h#ifndef BINARYTREE_H#define BINARYTREE_H#include#include#include//binary tree nodestruct binary_tree_node{ char data; binary_tree_node* lchild; binary_tree_node* rchild; binar原创 2015-12-13 18:44:55 · 421 阅读 · 0 评论 -
【树】二叉排序树
BinarySortTree.h#ifndef BINARYSORTTREE_H#define BINARYSORTTREE_H#include//binary sort tree nodestruct binary_sort_tree_node{ int data; binary_sort_tree_node* lchild; binary_sort_tree_node* rc原创 2016-01-18 18:45:35 · 515 阅读 · 0 评论 -
【String】引用计数实现String
String.h#ifndef STRING_H#define STRING_H#define _CRT_SECURE_NO_WARNINGS#include//Stringclass String{public: String(const char* str = NULL);//constructor String(const String& rhs);//copy cons原创 2015-11-29 20:45:44 · 746 阅读 · 1 评论 -
【查找】二分查找、插值查找、斐波那契查找
#includeusing namespace std;int binary_search(int* data, int n, int key){ int low, high, mid; low = 0; high = n-1; while (low <= high){ mid = low + (high - low) / 2; if (key < data[mid])原创 2016-05-29 19:33:09 · 607 阅读 · 0 评论