Labels

Thursday 25 June 2015

Dictionary Practical

#include<iostream> 
using namespace std; 
#include<stdlib.h> 
#include<string.h> 
struct node 

   char kword[10]; 
   char meaning[10]; 
   node *left; 
   node *right; 
   node *prev; 
}; 
class dic 

    public: 
        node *root,*temp,*knode,*temp2; 
        void calldel(char[]); 
        void call(char[]); 
        void creats(); 
        void insert(node *,node *); 
        void inorder(node *); 
        dic(); 
        void display(); 
        node* search(node *,char[]); 
        void modify(char [],char[]); 
}; 
dic::dic() 

      temp=root=knode=temp2=NULL; 
      temp2=new node; 
      knode=new node; 
 } 
void dic :: creats() 

           char ans; 
           do 
           { 
        node *newnode; 
        newnode=new node; 
        newnode->left=NULL; 
        newnode->right=NULL; 
        newnode->prev=NULL; 
        cout<<"enter keyword for newnode"; 
        cin>>newnode->kword; 
        cout<<"enter meaning for newnode"; 
        cin>>newnode->meaning; 
        if (root==NULL) 
        { 
        root=newnode; 
        temp=root; 
        } 
           else{ 
         insert(root,newnode); 
        } 
         cout<<"\n do u want continue (y/n)\n"; 
         cin>>ans; 
           }while(ans=='y'); 

void dic :: insert(node *root,node *newnode) 
 { 
         int i=0,f; 
          while(newnode->kword[i]!='\n') 
          {   f=0; 
            if((int(newnode->kword[i]))>(int(root->kword[i]))){ 
                f=1; 
                break;} 
            else if((int(newnode->kword[i]))==(int(root->kword[i]))){ 
                f=1; 
             } 
             else{ 
              f=0; 
              break; 
              } 
          i++; 
          } 

        if(f==0){ 
              if(root->left==NULL) { 
                root->left=newnode; 
                newnode->prev=root; 
                } 

          else 

            insert(root->left,newnode); 

             } 

        if(f==1)    { 
           if(root->right==NULL){ 
               root->right=newnode; 
               newnode->prev=root; 
               } 
        else 

             insert(root->right,newnode); 
           } 
         } 


void dic:: inorder(node *root) 

        if(root!=NULL){ 
        inorder(root->left); 
        cout<<"\n"<<root->kword; 
        cout<<"\t"<<root->meaning<<"\n"; 
        inorder(root->right); 
       } 

void dic::display() 

      cout<<"\ninorder traversal of tbt\n"; 
      inorder(root); 

node* dic:: search(node *temp1,char word[10]) 

int i=0,j; 
if(temp1!=NULL) 


j=strcmp(temp1->kword,word); 
if(j==0) 
    { 
    cout<<"\n"<<temp1->kword<<"\t"<<temp1->meaning; 
    temp2=temp1; 
    return temp1; 
    } 
else 

 a: 
    if((int(word[i]))>(int(temp1->kword[i]))) 
    search(temp1->right,word); 
    else if((int(word[i]))<(int(temp1->kword[i]))) 
    search(temp1->left,word); 
    else{ 
    i++; 
    goto a;  } 
    } 


return 0; 


void dic:: modify(char word1[10],char mean[10]) 

search(root,word1); 
strcpy(temp2->meaning,mean); 
cout<<"Modified"; 

void dic:: calldel(char word[10]){ 
node *temp1=NULL; 
temp2=NULL; 
search(root,word); 
if(temp2==NULL) 
    cout<<"No element is here"; 
else if(temp2->left==NULL&&temp2->right==NULL){ 
    if(temp2==root){ 
        delete(root); 
        root=NULL;} 
       //    strcpy(temp2->kword,NULL); 
       //    strcpy(temp2->meaning,NULL); 
    else { 
    temp2->prev->left=NULL; 
    delete(temp2); 
    } 
    cout<<"Sucessfully deleted"; 
    } 
else if(temp2->left!=NULL){ 
    temp1=temp2; 
    temp2=temp2->left; 
    while(temp2->right!=NULL) 
    temp2=temp2->right; 
    if(temp2->left!=NULL) 
        temp2->prev->right=temp2->left; 
    if(temp1==root) 
    root=temp2; 
    else 
    temp1=temp2; 
    delete(temp2); 
    cout<<"...sucessfully deleted"; 

else{ 
    temp1=temp2; 
    temp2=temp2->right; 
    while(temp2->left!=NULL) 
    temp2=temp2->left; 
    if(temp2->right!=NULL) 
        temp2->prev->left=temp2->right; 
    if(temp1==root) 
    root=temp2; 
    else 
    temp1=temp2; 
    delete(temp2); 
    cout<<"... sucessfully deleted"; 
 } 
 } 
void dic:: call(char word[]) 

search(root,word); 
//cout<<knode->kword<<"\t"<<knode->meaning; 

int main() 

       dic t1; 
       int i; 
      char ans; 
      char word[10],word1[10],mean[10]; 
       
       do 
    { 

        cout<<"1.create\n"; 
        cout<<"2.display\n"; 
        cout<<"3.search\n"; 
        cout<<"4.Delete\n"; 
        cout<<"5.Modify"; 
        cin>>i; 
        switch(i) 
         { 
           case 1:t1.creats(); 
             break; 
           case 2:t1.display(); 
             break; 
           case 3:cout<<"Enter the word to search"; 
              cin>>word; 
            t1.call(word); 
            break; 
           case 4:cout<<"Enter the word to delete"; 
              cin>>word; 
              t1.calldel(word); 
              break; 
           case 5:cout<<"Enter the word to be modified and its meaning"; 
             cin>>word1>>mean; 
            t1.modify(word1,mean);break; 
           default: 
           cout<<"enter correct choice"; break; 
         } 

            cout<<"\ndo u wanna cont (y/n)\n"; 
            cin>>ans; 
     }while(ans=='y'); 
return 0;