Telephone Directory Using BST
Source Code:
#include <iostream>
#include <string>
using namespace std;
struct phone{
int Num;
int code;
string address;
string name;
phone*right;
phone*left;
};phone*root=NULL;
phone*nodeptr=NULL;
//insert
void insert(string nme,string add, int phno,int Code)
{
phone*p=new phone;
p->name=nme;
p->address=add;
p->Num=phno;
p->code=Code;
p->right=NULL;
p->left=NULL;
if(root==NULL)
{
root=p;
cout<<"PHone nUmber is saved \n";
}
else
{
nodeptr=root;
while(nodeptr!=NULL)
{
if(phno<nodeptr->Num)
{
if(nodeptr->left!=NULL)
{
nodeptr=nodeptr->left;
}
else
{
nodeptr->left=p;
cout<<"PHone nUmber is saved \n";
break;
}
}
else if(phno>nodeptr->Num)
{
if(nodeptr->right!=NULL)
{
nodeptr=nodeptr->right;
}
else
{
nodeptr->right=p;
cout<<"PHone nUmber is saved \n";
break;
}
}
else
{
cout<<"Phone number is already saved \n";
break;
}
}
}
}
//update
void update(int C,int phno){
bool flag=false;
if(root==NULL)
{
cout<<" phone Record is empty \n";
}
else
{
phone*temp=root;
while(temp!=NULL)
{
if(temp->Num==phno && temp->code==C)
{
string nme,add;
cout<<"Number found \n";
cout<<"Enter Address : ";
cin.ignore();
getline(cin,add);
cout<<"Enter Name: ";
cin.ignore();
getline(cin,nme);
temp->address=add;
temp->name=nme;
flag=true;
break;
}
else if(phno<temp->Num)
{
temp=temp->left;
}
else
temp=temp->right;
}
}
if(flag==false)
{
cout<<"Such Number does not exist \n";
}
else
cout<<"Updated \n";
}
//search
void search(int c,int phno)
{
bool flag=false;
if(root==NULL)
{
cout<<" phone Record is empty \n";
}
else
{
phone*temp=root;
while(temp!=NULL)
{
if(temp->Num==phno && temp->code==c)
{
cout<<"***************************** \n";
cout<<"NAme: "<<temp->name<<endl;
cout<<"Address: "<<temp->address<<endl;
cout<<"Phone Number: 0"<<temp->code<<temp->Num<<endl;
flag=true;
break;
}
else if(phno<temp->Num)
{
temp=temp->left;
}
else
temp=temp->right;
}
}
if(flag==false)
{
cout<<"Such Number does not exist \n";
}
else
cout<<"FOund \n";
}
//display
void display(phone*ndeptr)
{
if(root==NULL)
{
cout<<"TRee is empty \n";
}
else
{
if(ndeptr!=NULL)
{
display(ndeptr->left);
cout<<"***************************** \n";
cout<<"NAme: "<<ndeptr->name<<endl;
cout<<"Address: "<<ndeptr->address<<endl;
cout<<"Phone Number: 0"<<ndeptr->code<<ndeptr->Num<<endl;
display(ndeptr->right);
}
else
return;
}
}
//delete
phone *Delete(phone *nodeptr, int data,int c) {
bool flag=false;
if (nodeptr == NULL)
return nodeptr;
else if (data < nodeptr->Num)
nodeptr->left = Delete(nodeptr->left, data,c);
else if (data > nodeptr->Num)
nodeptr->right = Delete(nodeptr->right, data,c);
else {
if (root == nodeptr && nodeptr->left == NULL && nodeptr->right == NULL) {
cout << "Root Null" << endl;
root = NULL;
}
if (nodeptr->left == NULL && nodeptr->right == NULL) {
cout << "Left Right Null" << endl;
delete nodeptr;
flag=true;
nodeptr = NULL;
}
else if (nodeptr->left == NULL) {
cout << "Left is null" << endl;
phone *temp = nodeptr;
if (nodeptr == root) {
cout << "Root left" << endl;
root = nodeptr->right;
}
nodeptr = nodeptr->right;
delete temp;
flag=true;
}
else if (nodeptr->right == NULL) {
cout << "Right is Null" << endl;
phone *temp = nodeptr;
if (nodeptr == root) {
cout << "Root left" << endl;
root = nodeptr->left;
}
nodeptr = nodeptr->left;
delete temp;
flag=true;
}
else {
cout << "Else is running" << endl;
phone *temp = nodeptr->right;
while (temp->left) {
temp = temp->left;
}
nodeptr->Num = temp->Num;
nodeptr->right = Delete(nodeptr->right, temp->Num,temp->code);
}
}
if(flag==true)
cout<<"Deleted Sucessfully \n";
else
cout<<"VAlue not found \n";
return nodeptr;
}
int main()
{
int ch,phno,Code;
string nme,add;
do
{
cout<<"****************************** \n";
cout<<" Telephone Directory \n";
cout<<"****************************** \n";
cout<<"1: Add New Contact \n";
cout<<"2: Delete Contact \n";
cout<<"3: Update Contact \n";
cout<<"4: Search Contact \n";
cout<<"5: View All \n";
cout<<"6: Exit \n";
cout<<"****************************** \n";
cout<<"Enter CHoice: ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"EnterNAme: ";
// cin>>nme;
cin.ignore();
getline(cin,nme);
cout<<"ENter Address: ";
// cin>>add;
//cin.ignore();
getline(cin,add);
cout<<"insert code: ";
cin>>Code;
cout<<"Enter Phone number: ";
cin>>phno;
insert(nme,add,phno,Code);
// cout<<nme<<endl;
// cout<<add<<endl;
// cout<<phno<<endl;
break;
}
case 2:
{
cout<<"ENter code: ";
cin>>Code;
cout<<"Enter Phone number: ";
cin>>phno;
Delete(root,phno,Code);
break;
}
case 3:
{
cout<<" Enter Code: ";
cin>>Code;
cout<<"Enter NUmber: ";
cin>>phno;
update(Code,phno);
}
case 4:
{
cout<<" Enter Code: ";
cin>>Code;
cout<<"Enter NUmber: ";
cin>>phno;
search(Code,phno);
break;
}
case 5:
{
// system("CLS");
display(root);
cout<<endl;
break;
}
case 6:
{
break;
}
default:
{
cout<<"Invalid output \n";
break;
}
}
}while(ch!=6);
}
Water Hack Burns 2 lb of Fat OVERNIGHT
ReplyDeleteOver 160 thousand men and women are trying a simple and secret "liquids hack" to lose 1-2lbs every night in their sleep.
It's simple and it works all the time.
This is how you can do it yourself:
1) Grab a drinking glass and fill it with water half full
2) Now learn this strange HACK
and you'll be 1-2lbs thinner as soon as tomorrow!