数据结构小作业---家族族谱
生活随笔
收集整理的這篇文章主要介紹了
数据结构小作业---家族族谱
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
家族族譜樹
#include <iostream> #include<string> #include<iomanip> #include<cstring> using namespace std; bool found=false; typedef long long int ll; int next1[1000]; int number=0; string ch; typedef struct node{string name;char sex;int birthday;string introdeuce;string spouse;node *child;node *pre;node *next;int childnum;bool Root;bool adjective; }TreeNode;TreeNode *Q;void KMP_Pre(string ch,ll m,int next[1000]){ //ch為匹配串int i,j;i=j=next[0]=-1;i=0;while(i<m){while(-1!=j&&ch[i]!=ch[j])j=next[j];next[++i]=++j;}return ; }bool KMP(string ch,ll m,string sh,ll n){ //匹配事跡int i,j;KMP_Pre(ch, m, next1);i=j=0;while(i<n){while(j!=-1&&sh[i]!=ch[j])j=next1[j];i++;j++;if(j>=m){return true;}}return false; }bool SameTreeNode(string sh,string ch){KMP_Pre(ch,ch.length(),next1);if(KMP(ch, ch.length(), sh, sh.length()))return true;return false; } void InsertTreeNode (TreeNode *&root){root=new TreeNode;root->childnum=0;root->Root=true;root->next=NULL;root->pre=NULL;root->child=NULL;root->name=""; } bool EmptyTree(TreeNode *&root){if(root->child==NULL)return true;return false; } void AddTreeNode (TreeNode *&root ,TreeNode *&Q){TreeNode *p,*t=NULL;p=NULL;t=new TreeNode;cout<<"輸入一個姓名"<<endl;cin>>t->name;t->childnum=0;// cin>>t->sex;// cin>>t->birthday;// cin>>t->spouse;// cin>>t->adjective;t->child=NULL;t->next=NULL;t->pre=Q;if(Q->child!=NULL){p=Q->child;if(Q->childnum>1)while(p->next!=NULL)p=p->next;p->next=t;}elseQ->child=t;Q->childnum++;}void SearchTreeNode (TreeNode *&root,TreeNode *&Q,string ch){if(root->name==ch){Q=root;found=true;return;}if(root->next!=NULL){TreeNode *p;p=root->next;while(p){if(p->name==ch){found=1;Q=p;return;}p=p->next;}}if(root->child!=NULL){SearchTreeNode(root->child, Q, ch);}if(root->next!=NULL){TreeNode *p;p=root->next;while(p){SearchTreeNode(p, Q, ch);p=p->next;}}}void SearchTreeNodeIntrodeuce(TreeNode *&root,TreeNode *&Q,string ch){if(SameTreeNode(root->introdeuce, ch)){Q=root;found=true;return;}if(root->next!=NULL){TreeNode *p;p=root->next;while(p){if(SameTreeNode(p->name, ch)){found=1;Q=p;return;}p=p->next;}}if(root->child!=NULL){SearchTreeNodeIntrodeuce(root->child, Q, ch);}if(root->next!=NULL){TreeNode *p;p=root->next;while(p){SearchTreeNodeIntrodeuce(p, Q, ch);p=p->next;}}} void ChangeTreeNode (TreeNode *&Q){cout<<"選擇你要修改的信息(1、姓名 2、性別 3、生日、4、配偶 5、事跡)"<<endl;int option;cin>>option;switch (option) {case 1:cout<<"請輸入你的姓名"<<endl;cin>>Q->name;break;case 2:cout<<"請輸入你要修改性別"<<endl;cin>>Q->sex;break;case 3:cout<<"請輸入所要修改的生日"<<endl;cin>>Q->birthday;break;case 4:cout<<"請輸入你所要修改的配偶姓名"<<endl;cin>>Q->spouse;break;case 5:cout<<"請輸入你要修改的事跡"<<endl;cin>>Q->introdeuce;} }void TreeNodeChange(TreeNode *&Q){int change;cout<<"選擇修改的內容(1、姓名 2、性別 3、生日 4、事跡)"<<endl;cin>>change;switch(change){case 1:{cout<<"輸入修改的姓名"<<endl;cin>>Q->name;cout<<"修改成功"<<endl;break;}case 2:{cout<<"請輸入修改的性別(男 :M 、女 W)"<<endl;cin>>Q->sex;cout<<"修改成功"<<endl;break;}case 3:{cout<<"請輸入修改的生日"<<endl;cin>>Q->birthday;cout<<"修改成功"<<endl;break;}case 4:{cout<<"請輸入修改的事跡"<<endl;cin>>Q->introdeuce;cout<<"修改成功"<<endl;}}} void FindTreeNodePre(TreeNode *&Q){if(!Q->pre->Root){cout<<"他的父親的姓名為"<<Q->pre->name<<endl;cout<<Q->pre->sex<<endl;cout<<Q->pre->birthday<<endl;cout<<Q->pre->spouse<<endl;cout<<Q->pre->introdeuce;} } void FindTreeNodeChild(TreeNode *&Q){TreeNode *p;int k=1;if(Q->childnum>1){p=Q->child;cout<<"第"<<k++<<"個孩子"<<endl;cout<<p->name<<endl;while(p->next!=NULL){p=p->next;cout<<"第"<<k++<<"個孩子"<<endl;cout<<p->name<<endl;cout<<p->name<<endl;}}else if(Q->childnum==1)cout<<"他的孩子為"<<Q->child->name<<endl;elsecout<<"他沒有孩子"<<endl; } void DeleteTreeNode(TreeNode *&Q,string ch){TreeNode *p;p=Q->child;if(Q->childnum==1){delete Q->child;Q->childnum=0;Q->child=NULL;cout<<"刪除成功"<<endl;return ;}else if(Q->childnum>1){if(p->name==ch){Q->child=p->next;Q->childnum--;cout<<"刪除成功"<<endl;return ;}TreeNode *t;t=p;while(p->next!=NULL){t=p;p=p->next;if(p->name==ch){t->next=p->next;Q->childnum--;delete p;cout<<"刪除成功"<<endl;return ;}}} } int main() {TreeNode *root;InsertTreeNode(root);InsertTreeNode(Q);while(1){found=false;int option;cout<<"選擇操作(1.添加 2.查找(姓名) 3.查找(事跡) 4.修改 5.刪除 6.查找父親 7.查找子女 8.退出)"<<endl;cin>>option;switch (option) {case 1:{if(EmptyTree(root))AddTreeNode(root, root);else{cout<<"輸入插入人的父親姓名"<<endl;cin>>ch;SearchTreeNode(root, Q, ch);if(found)AddTreeNode(root, Q);elsecout<<"暫無改父親信息"<<endl;}break;}case 2:{cout<<"輸入要查找的人的姓名"<<endl;cin>>ch;SearchTreeNode(root, Q, ch);if(found)cout<<Q->name<<endl;elsecout<<"未能查到該人信息"<<endl;break;}case 3:{cout<<"輸入你所查找人事跡的關鍵字"<<endl;cin>>ch;SearchTreeNodeIntrodeuce(root, Q, ch);if(found)cout<<Q->name<<endl;elsecout<<"未能找到"<<endl;}case 4:{cout<<"輸入修改人的姓名"<<endl;cin>>ch;SearchTreeNode(root, Q, ch);if(found)TreeNodeChange(Q);elsecout<<"未能查到"<<endl;break;}case 5:{cout<<"輸入你需要刪除的人的姓名"<<endl;cin>>ch;SearchTreeNode(root, Q, ch);if(found){TreeNode *p;p=Q->pre;DeleteTreeNode(p, ch);}else cout<<"未能查到此人"<<endl;break;}case 6:{cout<<"請輸入你需要查找的人"<<endl;cin>>ch;SearchTreeNode(root, Q, ch);if(found){if(!Q->pre->Root){cout<<"他的父親是"<<Q->pre->name<<endl;}}break;}case 7:{cout<<"輸入你要查找的人的姓名"<<endl;cin>>ch;SearchTreeNode(root, Q, ch);if(found)FindTreeNodeChild(Q);elsecout<<"未能查到此人"<<endl;break;}case 8:{cout<<"歡迎再次使用"<<endl;exit(1);}}}}?
總結
以上是生活随笔為你收集整理的数据结构小作业---家族族谱的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机图画大赛作品六年级,六年级绘画比赛
- 下一篇: 一维激波C++编程