sak1b

Untitled

Mar 5th, 2018
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define dbg(x) printf("\n--bug: %d --\n",x)
  3. #define MXX 26
  4.  
  5. using namespace std;
  6.  
  7. struct tokens{
  8.  
  9. string id;
  10. string key;
  11. struct tokens *next;
  12.  
  13. }*heads[MXX],*temp,*prv,*dummy;
  14.  
  15. int bg=1;
  16.  
  17. bool search(string x,string y)
  18. {
  19. for(int i=0;i<MXX;i++)
  20. {
  21. temp=heads[i];
  22. while(temp!=NULL)
  23. {
  24. if(temp->id==x && temp->key==y)
  25. {
  26. return true;
  27. }
  28. temp=temp->next;
  29. }
  30. }
  31. return false;
  32. }
  33.  
  34. int hashkey(string x)
  35. {
  36. int sum=0;
  37. for(int i=0;i<x.size();i++)
  38. {
  39. sum+=x[i];
  40. }
  41. return sum%MXX;
  42. }
  43.  
  44.  
  45. void insert(string x,string y)
  46. {
  47. bool exist=search(x,y);
  48. if(exist==false)
  49. {
  50. int position=hashkey(x);
  51. temp=heads[position];
  52.  
  53. if(temp==NULL) //if list is empty
  54. {
  55. dummy = new tokens;
  56.  
  57. dummy->id=x;
  58. dummy->key=y;
  59. dummy->next=NULL;
  60.  
  61. heads[position]=dummy;
  62. dbg(bg++);
  63. }
  64. else
  65. {
  66.  
  67. dummy=new tokens;
  68. dummy->id=x;
  69. dummy->key=y;
  70. dummy->next= heads[position];
  71. heads[position]=dummy;
  72.  
  73. }
  74.  
  75. }
  76. else {
  77.  
  78. printf("Already exits\n\n");
  79. }
  80. }
  81.  
  82.  
  83. void del(string x,string y)
  84. {
  85. for(int i=0;i<MXX;i++)
  86. {
  87. if(heads[i]==NULL) continue;
  88. temp=heads[i];
  89. prv=heads[i];
  90.  
  91. if(heads[i]->id==x && heads[i]->key==y)
  92. {
  93. heads[i]=heads[i]->next;
  94. // dbg(1000);
  95. delete(temp);
  96. return;
  97. }
  98.  
  99.  
  100. while(temp!=NULL)
  101. {
  102. if(temp->id==x && temp->key==y)
  103. {
  104. printf("Successfully Deleted\n");
  105. prv->next=temp->next;
  106. delete(temp);
  107. return;
  108. }
  109. prv=temp;
  110. temp=temp->next;
  111. }
  112. }
  113.  
  114. }
  115.  
  116. void update(string x, string y, string nw)
  117. {
  118. for(int i=0;i<MXX;i++)
  119. {
  120. temp=heads[i];
  121.  
  122. while(temp!=NULL)
  123. {
  124. if(temp->id==x && temp->key==y)
  125. {
  126. printf("Successfully Updated\n");
  127. temp->key=nw;
  128. }
  129.  
  130. temp=temp->next;
  131. }
  132. }
  133.  
  134. }
  135.  
  136. void show()
  137. {
  138. printf("======Showing the contents======\n\n");
  139.  
  140.  
  141. for(int i=0;i<MXX;i++)
  142. {
  143. temp=heads[i];
  144. printf("Link %d : ",i);
  145.  
  146. while(temp!=NULL)
  147. {
  148. cout<<"< "<<temp->id<<" , "<<temp->key <<" > ";
  149. temp=temp->next;
  150. }
  151. cout<<endl;
  152. cout<<endl;
  153. }
  154. }
  155.  
  156. int main()
  157. {
  158. freopen("input.txt","r",stdin);// freopen("output.txt","w",stdout);
  159.  
  160. for(int i=0;i<MXX;i++) heads[i]=NULL; //init
  161. string one,two;
  162.  
  163. do{
  164. printf("=====Please insert your desired input=====\n\n");
  165. printf("1. Insert\n2. Search\n3. Delete\n4. Show\n5. Update\nq. Quit\n\n");
  166.  
  167. string inp;
  168. cin>>inp;
  169. if(inp=="1")
  170. {
  171. printf("insert identifier\n");
  172. cin>>one;
  173. printf("insert keyword\n");
  174. cin>>two;
  175. insert(one,two);
  176.  
  177.  
  178. }else if(inp=="2")
  179. {
  180. printf("insert identifier\n");
  181. cin>>one;
  182. printf("insert keyword\n");
  183. cin>>two;
  184. bool res=search(one,two);
  185. (res)?cout<<"Found\n": cout<<"Not Found\n";
  186.  
  187. }else if(inp=="3")
  188. {
  189. printf("insert identifier\n");
  190. cin>>one;
  191. printf("insert keyword\n");
  192. cin>>two;
  193. del(one,two);
  194.  
  195. }else if(inp=="4")
  196. {
  197. show();
  198.  
  199. }else if(inp=="5")
  200. {
  201. printf("insert identifier\n");
  202. cin>>one;
  203. printf("insert old keyword\n");
  204. cin>>two;
  205. string three;
  206. printf("insert new keyword\n");
  207. cin>>three;
  208. update(one,two,three);
  209.  
  210. }else if(inp=="q")
  211. {
  212. break;
  213. }
  214.  
  215. }while(true);
  216.  
  217.  
  218.  
  219.  
  220. return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment