/* DICTIONARY OPERATIONS */
#include<iostream>
#include<conio.h>
using namespace std;
class dictionary
{
struct node
{
int key;
int value;
struct node *next;
}*head,*curr,*prev;
public:dictionary()
{
head=NULL;
}
void insertion();
void deletion();
int length();
void display();
};
void dictionary::insertion()
{
node *temp=new node();
cout<<"\n Enter the key & value pair to insert into the dictionary list:\n";
cin>>temp->key;
cin>>temp->value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
curr=head;
prev=curr;
}
else if(temp->key<head->key)
{
temp->next=head;
head=temp;
curr=head;
prev=curr;
}
else
{
curr=head;
prev=curr;
while(curr->key<temp->key && curr->next!=NULL)
{
prev=curr;
curr=curr->next;
}
if(curr->next==NULL)
{
if(curr->key<temp->key)
{
curr->next=temp;
prev=curr;
}
else
{
temp->next=prev->next;
prev->next=temp;
}
}
else
{
temp->next=prev->next;
prev->next=temp;
}
}
}
void dictionary::deletion()
{
int key;
cout<<"\n Enter the key to be delete the pair of dictionary record:\n";
cin>>key;
curr=head;
while(curr!=NULL)
{
if(curr->key==key)
{
break;
}
prev=curr;
curr=curr->next;
}
if(curr==NULL)
{
cout<<"\n Dictionary list is empty\n";
}
else
{
if(curr==head)
{
head=curr->next;
}
else
{
prev->next=curr->next;
}
}
delete(curr);
}
int dictionary::length()
{
int count=0;
curr=head;
if(curr==NULL)
{
cout<<"\n Dictionary list is empty\n";
}
else
{
while(curr!=NULL)
{
count++;
curr=curr->next;
}
cout<<"\n The length of the dictionary list is:\n"<<count;
}
return(count);
}
void dictionary::display()
{
if(head==NULL)
{
cout<<"\n Dictionary list is empty\n";
}
else
{
node *temp=head;
while(temp!=NULL)
{
cout<<"<"<<temp->key<<","<<temp->value<<">";
temp=temp->next;
}
}
}
main()
{
dictionary d;
int ch;
while(1)
{
cout<<"\n 1.insertion\n 2.deletion\n 3.length\n 4.display\n 5.exit\n";
cout<<"\n Enter your choice:\n";
cin>>ch;
switch(ch)
{
case 1:d.insertion();
break;
case 2:d.deletion();
break;
case 3:d.length();
break;
case 4:d.display();
break;
case 5:exit(1);
break;
default:cout<<"Wrong choice";
}
}
}