
数据结构与算法
sotff
不断学习进步
展开
-
队列
数组实现:#include using namespace std;typedef int T;class Queue{ T a[5]; int b, n;//队首位置和有效元素个数public: Queue():b(0),n(0){} Queue& push(const T& d){ if(full()) throw "满"; a[(b+n++)%5] =原创 2014-03-05 14:58:12 · 559 阅读 · 0 评论 -
快速排序
#include using std::swap;void sort(int* a, int n){ if(n<=1) return; if(n==2){ if(a[1]<a[0]) swap(a[1],a[0]); return; } swap(a[n/2],a[0]); int jie=a[0]; int* L=a+1; int* R=a+n-1; while(L原创 2014-03-05 22:00:11 · 680 阅读 · 0 评论 -
选择排序
#include using std::swap;void sort(int* a, int n){ //反复n-1次 for(int i=0; i<n-1; i++){ // 第i次从第i~n个数据中找到最小元素是谁 int min = i; for(int j=i+1; j<n; j++) if(a[j]<a[min]) min = j; // 把它跟第i个原创 2014-03-05 21:58:57 · 578 阅读 · 0 评论 -
插入排序
void sort(int* a, int n){ int j; for(int i=1; i<n; i++){ int t = a[i]; for(j=i;j>0&&t<a[j-1];j--) a[j]=a[j-1]; a[j] = t; }}原创 2014-03-05 21:56:03 · 593 阅读 · 0 评论 -
二分查找法
#include using namespace std;class Person{ string name; int age; string addr;public: Person(const char* n, int a, const char* ad) :name(n),age(a),addr(ad){} friend bool operator<(const Person原创 2014-03-05 21:06:42 · 606 阅读 · 0 评论 -
二叉树
#include using namespace std;typedef char T;class bst{ struct Node{ T data; Node* L; Node* R; Node(const T& d):data(d),L(),R(){} Node(const T& d,Node*l,Node*r):data(d),L(l),R(r){} }; t原创 2014-03-05 18:56:44 · 590 阅读 · 0 评论 -
冒泡排序
#include using std::swap;void sort(int* a, int n){ bool changed; do{ changed = false; for(int i=1; i<n; i++){ if(a[i]<a[i-1]){ swap(a[i],a[i-1]); changed = true; } } --n; }原创 2014-03-05 21:48:59 · 585 阅读 · 0 评论 -
栈
#include #include using namespace std;typedef string T;class Stack{ T a[5]; int cur;public: Stack():cur(0){} void push(const T& d)throw(const char*);//如栈 T pop()throw(const c原创 2014-03-05 13:48:16 · 611 阅读 · 0 评论 -
链表
单链表:#includeusing namespace std;typedef int T;class List{ //内部的自定义成员,内部成员 struct Node{ T data; Node* next; Node(const T& d=T()):data(d),next(NULL){};//T() 匿名原创 2014-03-04 23:16:20 · 582 阅读 · 0 评论 -
实现qsort函数原型
快速排序,作为能唯一能存在于库函数里的排序算法,qsort()的优势就在于非常灵活.用man qsort 查看其文档void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));发现它的优势就提现在他的参数用 使用了 void * 和函数指针.参数:原创 2014-03-30 11:25:42 · 2282 阅读 · 0 评论