1、实验汇总容易:顺序表实验:#includeusing namespace std;class list int data100; int size;public: void create(int *a); int find(int j); int insert(int item,int j); int del(int j); int getSize()return size;void list:create(int *a) int i; for(i=1;ij;i-) datai=datai-1; dataj=item; size+; return 1;int list:del(int j) i
2、nt i; for(i=j;iT; t0=T; for(i=1;iti; list li; li.create(t); for(i=1;ili.getSize();i+) coutli.find(i) ; coutli.find(i)j; coutli.find(j)jitem; li.insert(item,j); for(i=1;ili.getSize();i+) coutli.find(i) ; coutli.find(i)j; li.del(j); for(i=1;ili.getSize();i+) coutli.find(i) ; coutli.find(i)endl; return
3、 0;顺序查找:#includeusing namespace std;int main() int text,T,i,item,j; int t100; cintext; for(j=0;jT; for(i=1;iti; cinitem; for(i=T;i0;i-) if(ti=item) break; if(i0) cout1 i T-i+1endl; else cout0 i T-i+1endl; return 0;单链表实验:#includeusing namespace std;class Nodepublic: int data; Node *next; Node(Node *p
4、trNext=NULL) next=ptrNext; Node(int i,Node *ptrNext=NULL) data=i; next=ptrNext; ;class list Node *head; int size;public: list()head=new Node();size=0; Node *index(int i); int find(int i); int insert(int item,int j); int del(int j); int getSize()return size;Node *list:index(int i) if(i=0)return head;
5、 Node *p=head-next; int j; for(j=1;jnext; return p;int list:find(int i) Node *p=index(i); return p-data;int list:insert(int item,int j) Node *p=index(j-1); Node *q=new Node(item,p-next); p-next=q; size+; return 1; int list:del(int j) Node *p=index(j-1); Node *q=index(j); p-next=q-next; delete q; siz
6、e-; return 1;int main() int T,i,item,j,t100; list li; cinT; for(i=1;iti; for(i=1;i=T;i+) li.insert(ti,i); for(i=1;ili.getSize();i+) coutli.find(i) ; coutli.find(i)j; coutli.find(j)jitem; li.insert(item,j); for(i=1;ili.getSize();i+) coutli.find(i) ; coutli.find(i)j; li.del(j); for(i=1;ili.getSize();i
7、+) coutli.find(i) ; coutli.find(i)endl; return 0;折半查找实验:#includeusing namespace std;int main() int text,T,t100,i,j; int low,high,mid,key,count; cintext; for(j=0;jT; for(i=1;iti; low=1;high=T; count=0; cinkey; while(lowkey) high=mid-1; else if(tmidkey) low=mid+1; else break; if(low=high) cout1 mid co
8、untendl; else cout0 0 countendl; return 0;快速排序实验:#includeusing namespace std;void ShowSeqList(int *Key,int n) int i; for(i=1;in;i+) coutKeyi ; coutKeyiendl;int First=1;void QuickSort(int low,int high,int *Key) int i,j,Pivotkey; i=low; j=high; Pivotkey=Keylow; while(lowhigh) while(lowhigh)&(PivotkeyK
9、eyhigh) high-; if(lowhigh) Keylow=Keyhigh; low+; while(lowhigh)&(KeylowPivotkey) low+; if(lowhigh) Keyhigh=Keylow; high-; Keylow=Pivotkey; if(First) ShowSeqList(Key,j); First=0; if(ilow-1)QuickSort(i,low-1,Key); if(high+1t; for(i=1;iKeyi; ShowSeqList(Key,t); QuickSort(1,t,Key); ShowSeqList(Key,t); r
10、eturn 0;希尔排序实验:#includeusing namespace std;void shellsort(int *v,int n) int gab,I,k,j,temp; for(gab=n/2;gab0;gab/=2) for(i=n;i0;i-=gab) for(k=0;kgab;k+) for(j=k;jvj+gab) temp=vj; vj=vj+gab; vj+gab=temp; for(i=0;in-1;i+) coutvi” “; coutviT; for(i=0;iti; for(i=0;iT-1;i+) coutti” “; couttiendl; shellso
11、rt(t,T); return 0;较难:图的深度优先搜索实验:#includeusing namespace std;class DNSTraverse int MaxNodeNo; int AdjMatrix100100; int Visited100;public: int DFSNodeNo; int DFSNode100; void Create(int *e,int n); void DFS(int v);void DNSTraverse:Create(int *e,int n) int i,j; MaxNodeNo=n; DFSNodeNo=0; for(i=0;in;i+) f
12、or(j=0;jn;j+) AdjMatrixij=ei*n+j; for(i=0;in;i+) Visitedi=0;void DNSTraverse:DFS(int v) int j; DFSNodeDFSNodeNo+=v; Visitedv=1; for(j=0;j0&Visitedj=0) DFS(j);int main() int e100; int i,j,m; int T,S; DNSTraverse *D=new DNSTraverse(); cinT; for(m=0;mS; for(i=0;iS;i+) for(j=0;jei*S+j; D-Create(e,S); D-
13、DFS(0); for(i=0;iDFSNodeNo;i+) coutDFSNodei ; coutendl; return 0;最短路径实验:#includeusing namespace std; #define INFINITY 100#define MAX 100typedef structint VertexNum;char VertexMAX;int AdjMatrixMAXMAX;Graph;Graph MGraph;char PathMAXMAX;int DestMAX; void CreateGraph(Graph *G)int I,j;cinG-VertexNum;for(
14、i=1;iVertexNum;i+) cinG-Vertexi;for(i=1;iVertexNum;i+) for(j=1;jVertexNum;j+) cinG-AdjMatrixij;for(i=1;iVertexNum;i+) for(j=1;jVertexNum;j+) if(G-AdjMatrixij=-1) G-AdjMatrixij=INFINITY; void ShortestPath(Graph *G,char StartVexChar)int I,j,m,StartVex,CurrentVex,MinDest;int VisitedMAX;for(i=1;iVertexN
15、um;i+) if(G-Vertexi=StartVexChar) StartVex=I; break; for(i=1;iVertexNum;i+) Pathi0=0; Desti=INFINITY; if(G-AdjMatrixStartVexiAdjMatrixStartVexi; Pathi1=G-VertexStartVex; Pathi2=G-Vertexi; Pathi0=2; Visitedi=F; DestStartVex=0;VisitedStartVex=T;for(i=1;iVertexNum;i+) MinDest=INFINITY; for(j=1;jVertexN
16、um;j+) if(Visitedj=F&DestjMinDest) CurrentVex=j; MinDest=Destj; VisitedCurrentVex=T; for(j=1;jVertexNum;j+) if(Visitedj=F)&(MinDest+G-AdjMatrixCurrentVexjAdjMatrixCurrentVexj; for(m=0;mVertexj; void ShowPath(Graph *G)int I,j;for(i=1;iVertexNum;i+) coutVertexi”(“Desti0) for(j=1;j=Pathi0;j+) cout” “Pa
17、thij; coutStartVex;ShortestPath(&Mgraph,StartVex);ShowPath(&Mgraph);return 0;难:Huffman树:#include using namespace std;#include #define ERROR -1 #define CORRECT 1 #define MAXC 15 #define MAXH 100 #define MAXS 100 typedef struct char c; int weight; int parent,lchild,rchild; char codeMAXC; HTNode, *Huff
18、manTree; HTNode HTMAXH; void Select(int i1,int *s1,int *s2) unsigned int j,s; s=0; for(j=1;j=i1;j+) if(HTj.parent=0) if(s=0) s=j; if(HTj.weightHTs.weight) s=j; *s1=s; s=0; for(j=1;j=i1;j+) if(HTj.parent=0)&(j!=*s1) if(s=0) s=j; if(HTj.weightHTs.weight) s=j; *s2=s; void CreateHTree(int n,char *c,int
19、*w) int i,m,s1,s2; m=2*n-1; for(i=1;i=n;+i) HTi.parent=HTi.lchild=HTi.rchild=0; HTi.weight=wi-1; HTi.c=ci-1; for(;i=m;+i) HTi.parent=HTi.lchild=HTi.rchild=HTi.weight=0; for(i=n+1;i=m;+i) Select(i-1,&s1,&s2); HTs1.parent=i; HTs2.parent=i; HTi.lchild=s1; HTi.rchild=s2; HTi.weight=HTs1.weight+HTs2.weig
20、ht; void HCode(int n) int i,j,m,c,f,start; char cdMAXC; m=MAXC; cdm-1=0; for(i=1;i=n;i+) start=m-1; for(c=i, f=HTi.parent; f!=0; c=f, f=HTf.parent) if(HTf.lchild=c) cd-start=0; else cd-start=1; for(j=0;jm-start;j+) HTi.codej=cdstart+j; HTi.codej=0; void ShowHCode(int n) int i; for(i=1;in;i+) coutHTi
21、.code ; coutHTi.codeendl; int ShowHEncode(int n,char *eString) int i,j; for(j=0;jstrlen(eString);j+) for(i=1;i=n;i+) if(eStringj=HTi.c) coutn) return(ERROR); coutendl; return(CORRECT); int ShowHDecode(int n,char *dString) int i,c; int Root; Root=2*n-1; c=Root; for(i=0;istrlen(dString);i+) if(HTc.lchild=0)&(HTc.rchild=0) coutHTc.c; c=Root; if(dStringi=0) if(HTc.lchild=0) return(ERROR); c=HTc.lchild; else if(HTc.rchild=0) return(ERROR); c=HTc.rchild; coutHTc.cendl; return(CORRECT); int main() int i,n; char cMAXH
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1