Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define dbg(x) printf("\n--bug: %d --\n",x)
- #define MXX 26
- using namespace std;
- struct tokens{
- string id;
- string key;
- struct tokens *next;
- }*heads[MXX],*temp,*prv,*dummy;
- int bg=1;
- bool search(string x,string y)
- {
- for(int i=0;i<MXX;i++)
- {
- temp=heads[i];
- while(temp!=NULL)
- {
- if(temp->id==x && temp->key==y)
- {
- return true;
- }
- temp=temp->next;
- }
- }
- return false;
- }
- int hashkey(string x)
- {
- int sum=0;
- for(int i=0;i<x.size();i++)
- {
- sum+=x[i];
- }
- return sum%MXX;
- }
- void insert(string x,string y)
- {
- bool exist=search(x,y);
- if(exist==false)
- {
- int position=hashkey(x);
- temp=heads[position];
- if(temp==NULL) //if list is empty
- {
- dummy = new tokens;
- dummy->id=x;
- dummy->key=y;
- dummy->next=NULL;
- heads[position]=dummy;
- dbg(bg++);
- }
- else
- {
- dummy=new tokens;
- dummy->id=x;
- dummy->key=y;
- dummy->next= heads[position];
- heads[position]=dummy;
- }
- }
- else {
- printf("Already exits\n\n");
- }
- }
- void del(string x,string y)
- {
- for(int i=0;i<MXX;i++)
- {
- if(heads[i]==NULL) continue;
- temp=heads[i];
- prv=heads[i];
- if(heads[i]->id==x && heads[i]->key==y)
- {
- heads[i]=heads[i]->next;
- // dbg(1000);
- delete(temp);
- return;
- }
- while(temp!=NULL)
- {
- if(temp->id==x && temp->key==y)
- {
- printf("Successfully Deleted\n");
- prv->next=temp->next;
- delete(temp);
- return;
- }
- prv=temp;
- temp=temp->next;
- }
- }
- }
- void update(string x, string y, string nw)
- {
- for(int i=0;i<MXX;i++)
- {
- temp=heads[i];
- while(temp!=NULL)
- {
- if(temp->id==x && temp->key==y)
- {
- printf("Successfully Updated\n");
- temp->key=nw;
- }
- temp=temp->next;
- }
- }
- }
- void show()
- {
- printf("======Showing the contents======\n\n");
- for(int i=0;i<MXX;i++)
- {
- temp=heads[i];
- printf("Link %d : ",i);
- while(temp!=NULL)
- {
- cout<<"< "<<temp->id<<" , "<<temp->key <<" > ";
- temp=temp->next;
- }
- cout<<endl;
- cout<<endl;
- }
- }
- int main()
- {
- freopen("input.txt","r",stdin);// freopen("output.txt","w",stdout);
- for(int i=0;i<MXX;i++) heads[i]=NULL; //init
- string one,two;
- do{
- printf("=====Please insert your desired input=====\n\n");
- printf("1. Insert\n2. Search\n3. Delete\n4. Show\n5. Update\nq. Quit\n\n");
- string inp;
- cin>>inp;
- if(inp=="1")
- {
- printf("insert identifier\n");
- cin>>one;
- printf("insert keyword\n");
- cin>>two;
- insert(one,two);
- }else if(inp=="2")
- {
- printf("insert identifier\n");
- cin>>one;
- printf("insert keyword\n");
- cin>>two;
- bool res=search(one,two);
- (res)?cout<<"Found\n": cout<<"Not Found\n";
- }else if(inp=="3")
- {
- printf("insert identifier\n");
- cin>>one;
- printf("insert keyword\n");
- cin>>two;
- del(one,two);
- }else if(inp=="4")
- {
- show();
- }else if(inp=="5")
- {
- printf("insert identifier\n");
- cin>>one;
- printf("insert old keyword\n");
- cin>>two;
- string three;
- printf("insert new keyword\n");
- cin>>three;
- update(one,two,three);
- }else if(inp=="q")
- {
- break;
- }
- }while(true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment