个人电话号码查询系统
按理说 只要我不输4就不会结束程序呀 可是为什么在我添加两个联系人之后程序就结束了?
#include <iostream>
#include<malloc.h>
using namespace std;
struct stu_num
{
char stu_name[30];
int num;
int number;
};
typedef struct person_list
{
person_list *lchild;
person_list *rchild;
int height;
stu_num perfor_info;
}person_node;
person_node *request_person_node(const struct stu_num *value)
{
person_node *new_node;
//new_node=new person_node;
new_node=(person_node*)malloc(sizeof(person_node));
if(new_node==NULL)
{
perror("error");
return nullptr;
}
if(value!=nullptr)
{
new_node->perfor_info=*value;
new_node->lchild=nullptr;
new_node->rchild=nullptr;
}
return new_node;
}
int get_tree_height(person_node *root)
{
if(root=nullptr)
return 0;
return max(get_tree_height(root->lchild),get_tree_height(root->rchild))+1;
}
person_node *tree_node_rotate_left(person_node *root)
{
person_node *tmp;
tmp=root->lchild;
root->lchild=tmp->rchild;
tmp->rchild=root;
tmp->height=get_tree_height(tmp);
root->height=get_tree_height(root);
return tmp;
}
person_node *tree_node_rotate_right(person_node *root)
{
person_node *tmp;
tmp=root->rchild;
root->rchild=tmp->lchild;
tmp->lchild=root;
tmp->height=get_tree_height(tmp);
root->height=get_tree_height(root);
return tmp;
}
person_node *tree_node_rotate_left_right(person_node *root)
{
root->lchild=tree_node_rotate_right(root->lchild);
root=tree_node_rotate_left(root);
return root;
}
person_node *tree_node_rotate_right_left(person_node *root)
{
root->rchild=tree_node_rotate_left(root->lchild);
root=tree_node_rotate_right(root);
return root;
}
person_node *insert_node_to_tree(person_node *root,person_node *new_node)
{
if(root==nullptr)
return new_node;
if(root->perfor_info.number>new_node->perfor_info.number)
{
root->lchild=insert_node_to_tree(root->lchild,new_node);
}
else
{
root->rchild=insert_node_to_tree(root->rchild,new_node);
}
if(get_tree_height(root->lchild)-get_tree_height(root->rchild)==2)
{
if(new_node->perfor_info.number<root->lchild->perfor_info.number)
{
root=tree_node_rotate_left(root);
}
else
{
root=tree_node_rotate_left_right(root);
}
}
else if(get_tree_height(root->rchild)-get_tree_height(root->lchild)==2)
{
if(new_node->perfor_info.number>=root->lchild->perfor_info.number)
{
root=tree_node_rotate_right(root);
}
else
{
root=tree_node_rotate_right_left(root);
}
}
root->height=get_tree_height(root);
return root;
}
person_node *find_tree_node(person_node *root,int input_value)
{
if(root=nullptr)
return nullptr;
if(input_value<root->perfor_info.number)
{
return find_tree_node(root->lchild,input_value);
}
else if(input_value>root->perfor_info.number)
{
return find_tree_node(root->rchild,input_value);
}
return root;
}
void tree_for_each(person_node *root)
{
if(root=nullptr) return ;
tree_for_each(root->lchild);
cout<<"number: "<<root->perfor_info.number<<"name: "<<root->perfor_info.stu_name<<"phone number:"<<root->perfor_info.num<<endl;
tree_for_each(root->rchild);
}
int main(void)
{
int choose,input_value,a;
stu_num input_person_data;
person_node *root=nullptr,*new_node,*find_node;
while(1)
{
cout<<"****输入数字选择相应的指令****\n"<<endl;
cout<<"****1.添加联系人**************\n"<<endl;
cout<<"****2.查找联系人**************\n"<<endl;
cout<<"****3.输出所有联系人**********\n"<<endl;
cout<<"****4.退出本系统**************\n"<<endl;
cin>>choose;
switch(choose)
{
case 1:
cout<<"input number: "<<endl;
cin>>input_person_data.number;
cout<<"input name: "<<endl;
cin>>input_person_data.stu_name;
cout<<"input phone number: " <<endl;
cin>>input_person_data.num;
new_node=request_person_node(&input_person_data);
root=insert_node_to_tree(root,new_node);
break;
case 2:
cout<<"input number: "<<endl;
cin>>input_value;
find_node=find_tree_node(root,input_value);
cout<<"number: "<<find_node->perfor_info.number<<"name: "<<find_node->perfor_info.stu_name<<"phone number:"<<find_node->perfor_info.num<<endl;
break;
case 3:
cout<<"All members message: "<<endl;
tree_for_each(root);
break;
case 4:
goto log_out;
}
}
return 0;
log_out:
return 0;
}