摘要: template<class Elem>//用数组实现的队列class AQueue:public Queue<Elem>{private: int size; int front; int rear; Elem *listArray;public: AQueue(int sz=DefaultListSize) { size=sz+1; rear=0;front=1; listArray=new Elem[size]; } ~AQueue() { delete [] listArray... 阅读全文
posted @ 2012-02-16 18:50 苍术厚朴 阅读(211) 评论(0) 推荐(0) 编辑
摘要: template<class Elem>class AStack:public Stack<Elem> //数组实现的栈类{private: int size; int top; Elem *listArray;public: AStack(int sz=DefaultListSize) { size=sz;top=0;listArray=new Elem[sz]; } ~AStack() { delete [] listArray; } void clear() { top... 阅读全文
posted @ 2012-02-16 18:45 苍术厚朴 阅读(240) 评论(0) 推荐(0) 编辑
摘要: template<class Elem,class Comp>//Insertion Sortvoid inssort(Elem A[],int n){ for(int i=1;i<n;i++) for(int j=i;(j>0)&&(Comp::lt(A[j],A[j-1]));j--) swap(A,j,j-1);}//Bubble Sorttemplate<class Elem,class Comp>void bubsort(Elem A[],int n){ for(int i=0;i<n-1;i++) for(int j=n.. 阅读全文
posted @ 2012-02-16 18:41 苍术厚朴 阅读(363) 评论(0) 推荐(0) 编辑
摘要: #include "LList.h"#define UNVISITED 0//A graph ADTclass Graph{public: virtual int n()=0; virtual int e()=0; virtual int first(int)=0; virtual int next(int,int)=0; virtual int setEdge(int ,int,int)=0; virtual int delEdge(int,int)=0; virtual int weight(int ,int)=0; virtual int... 阅读全文
posted @ 2012-02-16 18:39 苍术厚朴 阅读(322) 评论(0) 推荐(0) 编辑
摘要: #include <stdio.h>#include<stdlib.h>#define MaxN 6int visited[MaxN]={0};typedef struct ArcNode{ int adjvex; double weight; struct ArcNode *nextarc;}EdgeNode;typedef struct VNode{ char data; struct ArcNode *firstarc;}AdjList[MaxN];typedef struct Graph{ int Vnum; AdjList Vertices;... 阅读全文
posted @ 2012-02-16 18:37 苍术厚朴 阅读(251) 评论(0) 推荐(0) 编辑
摘要: //回溯法解决旅行售货员问题template<class Type>class Traveling{ friend Type TSP(int **,int [],int,Type); friend void main(void);public: Type BBTSP(int v[]);private: void Backtrack(int i); int n, //图G的顶点数 *x, //当前解 *bestx; //当前最优解 Type **a, ... 阅读全文
posted @ 2012-02-16 18:34 苍术厚朴 阅读(1087) 评论(1) 推荐(0) 编辑
摘要: //使用分治策略,设计解棋盘覆盖问题的简洁算法#define SIZE 512static int tile=0;//L型骨牌号int Board[SIZE][SIZE];void ChessBoard(int tr,int tc,int dr,int dc,int size){ if(size==1) return; int t=tile++,s=size/2;//分割棋盘 //覆盖左上角子棋盘 if(dr<tr+s&&dc<tc+s) ChessBoard(tr,tc,dr,dc,s); else{ //此棋盘中无特殊方格 //... 阅读全文
posted @ 2012-02-16 18:32 苍术厚朴 阅读(219) 评论(0) 推荐(0) 编辑
摘要: #include<iostream>using namespace std;//动态规划解决矩阵连乘问题void MatrixChain(int *p,int n,int **m,int **s){ for(int i=1;i<=n;i++) //初始化m[i][i] m[i][i]=0; for(int r=2;r<=n;r++) //矩阵链的长度2,3,4... for(int i=1;i<=n-r+1;i++) //确定i { in... 阅读全文
posted @ 2012-02-16 18:31 苍术厚朴 阅读(175) 评论(0) 推荐(0) 编辑
摘要: #include<iostream>using namespace std;//动态规划解决最长公共子串void LCSLength(int m,int n,char *x,char *y,int **c,int **b){ int i,j; for(i=1;i<=m;i++) c[i][0]=0; for(i=1;i<=n;i++) c[0][i]=0; for(i=1;i<=m;i++) for(j=1;j<=n;j++) { if(x[i]==x[j]) { c[... 阅读全文
posted @ 2012-02-16 18:29 苍术厚朴 阅读(213) 评论(0) 推荐(0) 编辑
摘要: //动态规划解决0-1背包问题template<class Type>void Knapsack(Type v,int w,int c,int n,Type **m){ int jMax=min(w[n]-1,c); for(int j=0;j<=jMax;j++) m[n][j]=0; for(int j=w[n];j<=c;j++) m[n][j]=v[n]; for(int i=n-1;i>1;i--) { jMax=min(w[i]-1,c); for(int j=0;j<=jMax;j++) ... 阅读全文
posted @ 2012-02-16 18:27 苍术厚朴 阅读(1367) 评论(1) 推荐(0) 编辑