DSPS: Dictionary(word+meaning)
#include <iostream>
#include<string.h>
using namespace std;
class node
{
public:
char word[10];
char meaning[10];
node *left;
node *right;
};
class tree
{
public:
node *root,*n;
public:
tree();
void create();
void insert(node*,node*);
void search(node*);
void inorder(node*);
void del(node*);
};
tree::tree()
{
root=NULL;
}
void tree::search(node *temp)
{
char key[10];
cout<<"enter the word u wanna search";
cin>>key;
int found=0;
do
{
if(strcmp(temp->word,key)==0)
{
found=1;
cout<<"word found"<<endl;
cout<<temp->word<<" "<<temp->meaning<<endl;
}
else
{
if(strcmp(temp->word,key)>0)
{
temp=temp->left;
}
else
{
temp=temp->right;
}
}
}while(found==0);
}
void tree::create()
{
char key;
do
{
n=new node;
n->left=NULL;
n->right=NULL;
cout<<"enter a word for dictionary";
cin>>n->word;
cout<<"enter a meaning of word for dictionary";
cin>>n->meaning;
if(root==NULL)
{
root=n;
}
else
{
insert(root,n);
}
cout<<"do you want to enter more nodes?"<<endl<<"if yes press 'y' else 'n'";
cin>>key;
}while(key=='y'||key=='Y');
}
void tree::insert(node *root,node *n)
{
if(strcmp(n->word,root->word)<0) //to insert an element in the left side
{
if(root->left==NULL) //if endlleft element is empty
{
root->left=n;
}
else //if left element is not empty
{
insert(root->left,n);
}
}
if(strcmp(n->word,root->word)>0) //for right side
{
if(root->right==NULL)
{
root->right=n;
}
else
{
insert(root->right,n);
}
}
}
void tree::inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
cout<<temp->word<<" "<<temp->meaning<<endl;
inorder(temp->right);
}
}
void tree::del(node *temp)
{
}
int main()
{
int ch;
cout << "!!!Made by PARASHAR!!!" << endl; // prints !!!Made by PARASHAR!!!
tree t;
do
{
cout<<"Program for dictionary"<<endl;
cout<<"1)CREATE dictionary"<<endl;
cout<<"2)ADD a word"<<endl;
cout<<"3)DISPLAY dictionary"<<endl;
cout<<"4)DELETE a word"<<endl;
cout<<"5)SEARCH"<<endl;
cout<<"6)EXIT"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.create();
break;
case 3:
t.inorder(t.root);
break;
case 4:
t.del(t.root);
break;
case 5:
t.search(t.root);
break;
}
}while(ch<6);
return 0;
}
Comments
Post a Comment