数据结构程序.docx
- 文档编号:5742548
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:14
- 大小:389.42KB
数据结构程序.docx
《数据结构程序.docx》由会员分享,可在线阅读,更多相关《数据结构程序.docx(14页珍藏版)》请在冰豆网上搜索。
数据结构程序
数据结构程序
1、第三章字符串
2、第五章二叉树递归中序遍历
3、第五章二叉树非递归后序遍历
4、第六章有向图的深度遍历(邻接矩阵)
5、第六章画有向图的深度遍历和宽度遍历的图(邻接表)
6、根据后序、中序遍历画出相应的二叉树
姓名:
谌菂
学号:
201223050110
1、字符串
#include
#include
usingnamespacestd;
constintdfsize=128;
classqs
{
public:
qs(intsz=dfsize);
qs(constchar*init);
~qs(){delete[]ch;}
intlength()const{returncurlength;}
voidout()
{
cout<<"输出字符串"< cout< } voidij(char*h,char*c); voidbj(char*p); private: char*ch; intcurlength; intmaxsize; }; qs: : qs(intsz) {maxsize=sz; ch=newchar[maxsize+1]; if(ch==NULL) { cout<<"存储分配错误\n"; exit (1);} curlength=0; ch[0]='\0';} qs: : qs(constchar*init) {intlen=strlen(init); maxsize=(len>dfsize)? len: dfsize; ch=newchar[maxsize+1]; if(ch==NULL) {cout<<"存储分配出错! \n";exit (1);} curlength=len; strcpy(ch,init);} voidqs: : ij(char*h,char*c) { inthlen=strlen(h),clen=strlen(c); char*a=newchar[hlen+1],*r=newchar[clen]; strcpy(a,h); for(inti=0;i<=hlen;i++) ch[curlength+i]=a[i]; curlength=curlength+hlen; for(intj=0;j<=clen;j++) ch[curlength+j]=r[j]; curlength=curlength+hlen;} voidqs: : bj(char*p) {char*f; intplen=strlen(p),len=strlen(ch); intmax=(len>plen)? len: plen; for(inti=0;i { if(*(ch+i)>*(p+i)) break; else if(*(ch+i)<*(p+i)) {f=ch; ch=p; p=f; break; }} cout< voidmain() { chars[]="谌",t[]="菂"; qsq1(s),q2(t); q1.ij(s,t); q2.bj(s); } 2、二叉树递归中序遍历 #include usingnamespacestd; template structbinode { ddata; binode }; template classbitree {public: bitree() {root=creat(root);} ~bitree() {release(root);} voidinorder() {inorder(root);} private: binode binode voidrelease(binode voidinorder(binode }; emplate binode : creat(binode {dch;cin>>ch; cout<<"*bt="< if(ch=='#')bt=NULL; else{ bt=newbinode bt->data=ch; bt->lchild=creat(bt->lchild); bt->rchild=creat(bt->rchild); }returnbt; }; template voidbitree : release(binode { if(bt! =NULL) { release(bt->lchild); release(bt->rchild); deletebt;} }; template voidbitree : inorder(binode { if(bt==NULL) { cout<<"#"; return;} else { inorder(bt->lchild); cout< inorder(bt->rchild); } } voidmain() { cout<<"前序遍历请输入字符结点,#表示空子树"< bitree cout<<"\n中序遍历"< a.inorder(); cout< } 3、二叉树非递归后序遍历 #include usingnamespacestd; constintmaxsize=100; template structbinode { ddata; binode template structfbinode {fbinode(){ptr=NULL;} binode intflag;}; template classbitree {public: bitree() {root=creat(root);} ~bitree() {release(root);} voidpostorder(); private: binode binode voidrelease(binode template binode : creat(binode {dch;cin>>ch; cout<<"*bt="< if(ch=='#')bt=NULL; else{ bt=newbinode bt->data=ch; bt->lchild=creat(bt->lchild); bt->rchild=creat(bt->rchild);} returnbt; }; template voidbitree : release(binode { if(bt! =NULL) { release(bt->lchild); release(bt->rchild); deletebt; } }; template voidbitree : postorder() { binode fbinode inttop=-1; while(bt! =NULL||top! =-1) {while(bt! =NULL) {s[++top].ptr=bt;s[top].flag=1; bt=bt->lchild;} while(top! =-1&&s[top].flag==2) {bt=s[top--].ptr;cout< if(top! =-1){s[top].flag=2;bt=s[top].ptr;bt=bt->rchild;} else { bt=NULL;}} cout< voidmain() {cout<<"前序遍历请输入字符结点,#表示空子树"< bitree cout<<"\n后序遍历"< a.postorder(); cout< 4、有向图的深度遍历 #include usingnamespacestd; constintmaxsize=10; template classmgraph { public: mgraph(da[],intn,inte); ~mgraph(){}; voiddfstraverse(intv); voidbfstraverse(intv); private: dvertex[maxsize]; intarc[maxsize][maxsize]; intvisited[maxsize]; intvertexnum,arcnum; }; template mgraph : mgraph(da[],intn,inte) { inti,j,k;vertexnum=n;arcnum=e; for(i=0;i for(i=0;i for(j=0;j arc[i][j]=0; cout<<"依次输入每一条边"< for(k=0;k {cin>>i>>j;arc[i][j]=1;} cout<<"输出邻接矩阵: "< for(i=0;i {for(j=0;j cout<<"\t"< cout< } for(j=0;j }; template voidmgraph : dfstraverse(intv) { intj; visited[v]=1; cout<<""< for(j=0;j if(arc[v][j]==1&&visited[j]==0)dfstraverse(j); }; voidmain() { chara[]={'1','2','3','4','5'}; mgraph cout<<"深度优先遍历: "< ta.dfstraverse(0); cout< } 5、第六章画有向图的深度遍历和宽度遍历的图(邻接表)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序