资源描述:
《资源管理器(数据结构)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//模拟资源管理器#include#include#include#include#definemax100typedefstructTree{chardata[10];structTreechild,brother;}CsTree,Tnode;typedefstruct{chardata[max][10];inttop;}seqstack;intpushstack(seqstack&s,charch){if(s.top==max-1){printf
2、("栈满!");return0;}s.top++;strcpy(s.data[s.top],ch);return1;}voidpopstack(seqstack&s){if(s.top==-1){printf("栈空!");return;}elses.top--;}voidprintstack(seqstacks){inti=0;if(s.top==-1){printf("栈空!");return;}while(i<=s.top){printf("%s",s.data[i]);i++;}printf("");
3、}//创建树voidcreate(CsTree&T){charstr[20];scanf("%s",str);if(strcmp(str,"#")==0)T=NULL;else{T=(Tnode)malloc(sizeof(Tnode));strcpy(T->data,str);create(T->child);create(T->brother);}}//先序遍历voidpreorder(CsTreeT){if(T){printf("%s",T->data);preorder(T->child);preorder(T->b
4、rother);}}//销毁树voiddestroy(CsTree&T){if(T==NULL)return;if(T->child)destroy(T->child);if(T->brother)destroy(T->brother);free(T);T=NULL;}//凹入显示1voidprint(CsTreeT,intindent){inti;if(T==NULL)return;else{for(i=0;idata);print(T->
5、child,indent+1);print(T->brother,indent);//兄弟节点不需再缩进}}//由给定节点信息查找并返回其指针voidfind(CsTreeT,charpdata,CsTree&pfind){if(T==NULL)return;else{if(strcmp(T->data,pdata)==0)pfind=T;else{find(T->child,pdata,pfind);find(T->brother,pdata,pfind);}}}//由给点节点信息查找并返回其指针和其父节点指针voidfi
6、ndparent(CsTreeT,charpdata,CsTreep,CsTree&pfind,CsTree&parent){if(T==NULL)return;else{if(strcmp(T->data,pdata)==0){pfind=T;parent=p;return;}else{p=T;//暂存上一级节点findparent(T->child,pdata,p,pfind,parent);p=T;findparent(T->brother,pdata,p,pfind,parent);}}}//插入voidinsert
7、(CsTreeT,charpdata,charnewdata){CsTreepfind=NULL,pnew=NULL,pchild=NULL;find(T,pdata,pfind);if(pfind==NULL){printf("所找节点不存在!");return;}else{find(pfind,newdata,pchild);if(pchild){printf("所找节点已存在!");return;}else{pnew=(Tnode)malloc(sizeof(Tnode));if(pnew==NULL)exit
8、(0);strcpy(pnew->data,newdata);if(pfind->child){pnew->brother=pfind->child;pnew->child=NULL;}else{pnew->child=NULL;pnew->brother=NULL;}pfind->chi