Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int hashSize;
  5.  
  6. typedef struct Node {
  7. char *key;
  8. struct Node *Next;
  9. }Node;
  10.  
  11. Node **HashTable;
  12.  
  13.  
  14. void init() {
  15. HashTable = (Node**)malloc(hashSize * (sizeof(Node*)));
  16. int i = 0;
  17. for (i = 0; i < hashSize; i++)
  18. HashTable[i] = NULL;
  19. }
  20.  
  21. void add(const char *cheie ) {
  22. int index = hash(cheie, hashSize), ok=1;
  23. Node *nod = (Node*)malloc(sizeof(Node)), *aux = NULL, *aux2=NULL;
  24. nod->key = (char*)malloc(strlen(cheie) + 1);
  25. strcpy(nod->key, cheie);
  26. nod->Next = NULL;
  27. if (HashTable[index] == NULL)
  28. HashTable[index] = nod;
  29. else {
  30. aux = HashTable[index];
  31. ok = 1;
  32. if (strcmp(aux->key, cheie) == 0)
  33. ok = 0;
  34. while (aux!=NULL) {
  35. aux2 = aux;
  36. if (strcmp(aux->key, cheie) == 0)
  37. ok = 0;
  38. aux = aux->Next;
  39. }
  40. if (ok == 1)
  41. aux2->Next = nod;
  42. else
  43. fprintf(stderr,"eroare");
  44. free(aux);
  45. }
  46. }
  47.  
  48. void erase(const char *cheie) {
  49. int index = hash(cheie, hashSize), ok=0;
  50. Node *aux=NULL, *p=NULL, *aux2=NULL;
  51. if (HashTable[index] != NULL)
  52. if (strcmp(HashTable[index]->key, cheie) == 0)
  53. if (HashTable[index]->Next != NULL) {
  54. aux = HashTable[index]->Next;
  55. free(HashTable[index]);
  56. HashTable[index] = aux;
  57. }
  58. else {
  59. free(HashTable[index]);
  60. HashTable[index] = NULL;
  61. }
  62. else {
  63. aux = HashTable[index];
  64. p = NULL;
  65. ok = 0;
  66. while (aux != NULL) {
  67. p = aux;
  68. aux = aux->Next;
  69. if (strcmp(aux->key, cheie) == 0) {
  70. ok = 1;
  71. break;
  72. }
  73. }
  74. if (ok == 1) {
  75. if (aux->Next != NULL) {
  76. aux2 = aux->Next;
  77. free(aux);
  78. p->Next = aux2;
  79. }
  80. else {
  81. free(aux);
  82. p->Next = NULL;
  83. }
  84. }
  85. }
  86. }
  87.  
  88. void find(const char *cheie, const char *fisier)
  89. {
  90. int index = hash(cheie, hashSize), ok = 0, size = 0;
  91. FILE *f = NULL;
  92. Node *aux = NULL;
  93. if (HashTable[index] != NULL) {
  94. aux = HashTable[index];
  95. while (aux != NULL) {
  96. if (strcmp(aux->key, cheie) == 0)
  97. ok = 1;
  98. aux = aux->Next;
  99. }
  100. if (ok == 1)
  101. if (fisier!=NULL) {
  102. f = fopen(fisier, "a+");
  103. if (f==NULL)
  104. fprintf(stderr,"eroare");
  105. else {
  106. fclose(f);
  107. f = fopen(fisier, "r");
  108. fseek(f, 0, SEEK_END);
  109. size = ftell(f);
  110. fclose(f);
  111. f = fopen(fisier, "a+");
  112. if (size != 0)
  113. fprintf(f, "True\n");
  114. else
  115. fprintf(f, "True\n");
  116. fclose(f);
  117. }
  118. }
  119. else
  120. printf("True\n");
  121. else {
  122. if (fisier != NULL) {
  123. f = fopen(fisier, "a+");
  124. fclose(f);
  125. f = fopen(fisier, "r");
  126. fseek(f, 0, SEEK_END);
  127. size = ftell(f);
  128. fclose(f);
  129. f = fopen(fisier, "a+");
  130. if (size != 0)
  131. fprintf(f, "False\n");
  132. else
  133. fprintf(f, "False\n");
  134. fclose(f);
  135. }
  136. else
  137. printf("False\n");
  138. }
  139. free(aux);
  140. }
  141. else
  142. if (fisier != NULL) {
  143. f = fopen(fisier, "a+");
  144. fprintf(f, "False\n");
  145. fclose(f);
  146. }
  147. else
  148. printf("False\n");
  149. }
  150.  
  151. void clearHash() {
  152. int i = 0;
  153. Node *p = NULL, *aux = NULL;
  154. for (i = 0; i < hashSize; i++)
  155. if (HashTable[i] != NULL) {
  156. aux = HashTable[i];
  157. while (aux != NULL) {
  158. p = aux;
  159. aux = aux->Next;
  160. free(p);
  161. }
  162. HashTable[i] = NULL;
  163. }
  164. }
  165.  
  166. void print() {
  167. int i = 0;
  168. Node* aux = NULL;
  169. for (i = 0; i <hashSize; i++)
  170. if (HashTable[i] != NULL) {
  171. if (HashTable[i]->Next != NULL) {
  172. aux = HashTable[i];
  173. while (aux != NULL) {
  174. if (aux->Next != NULL)
  175. printf("%s ", aux->key);
  176. else
  177. printf("%s", aux->key);
  178. aux = aux->Next;
  179. }
  180. }
  181. else
  182. printf("%s", HashTable[i]->key);
  183. printf("\n");
  184. }
  185. free (aux);
  186. }
  187.  
  188. void ret_hash(const char *fisier) {
  189. int i = 0, nr = 0, size = 0;
  190. FILE *f = NULL;
  191. Node *aux = NULL;
  192. if (fisier == 0)
  193. print();
  194. else {
  195. f = fopen(fisier, "a+");
  196. fclose(f);
  197. f = fopen(fisier, "r");
  198. fseek(f, 0, SEEK_END);
  199. size = ftell(f);
  200. fclose(f);
  201. f = fopen(fisier, "a+");
  202. for (i = 0; i < hashSize; i++)
  203. if (HashTable[i] != NULL)
  204. nr++;
  205. if (nr != 0 && size!=0)
  206. fprintf(f, "\n");
  207. for (i = 0; i < hashSize; i++) {
  208. aux = HashTable[i];
  209. while (aux != NULL) {
  210. if (aux->Next != NULL)
  211. fprintf(f, "%s ", aux->key);
  212. else
  213. fprintf(f, "%s", aux->key);
  214. aux = aux->Next;
  215. }
  216. if(HashTable[i]!=NULL)
  217. fprintf(f, "\n");
  218. }
  219. }
  220. }
  221.  
  222. void print_bucket(int index) {
  223. Node *aux=NULL;
  224. if (HashTable[index] != NULL) {
  225. aux = HashTable[index];
  226. while (aux != NULL) {
  227. if (aux->Next != NULL)
  228. printf("%s ", aux->key);
  229. else
  230. printf("%s", aux->key);
  231. aux = aux->Next;
  232. }
  233. printf("\n");
  234. }
  235. }
  236.  
  237. void ret_bucket(int index, const char *fisier) {
  238. Node *aux=NULL;
  239. FILE *f=NULL;
  240. if (index<0 || index >hashSize || HashTable[index]==NULL || strchr("qwertyuiopasdfghjklzxcvbnm",index)) {
  241. fprintf(stderr,"eroare");
  242. }
  243. else
  244. if (fisier == 0)
  245. print_bucket(index);
  246. else {
  247. aux = HashTable[index];
  248. f = fopen(fisier, "a+");
  249. while (aux != NULL) {
  250. if (aux->Next != NULL)
  251. fprintf(f, "%s ", aux->key);
  252. else
  253. fprintf(f, "%s", aux->key);
  254. aux = aux->Next;
  255. }
  256. fprintf(f, "\n");
  257. fclose(f);
  258. }
  259. }
  260.  
  261. void resize(int val) {
  262. char **temp = NULL;
  263. int nr = 0;
  264. int i = 0;
  265. Node *aux = NULL;
  266. for (i = 0; i < hashSize; i++)
  267. if (HashTable[i] != NULL) {
  268. aux = HashTable[i];
  269. while (aux != NULL) {
  270. nr++;
  271. aux = aux->Next;
  272. }
  273. }
  274. temp = (char**)malloc(nr * sizeof(char*));
  275. nr = -1;
  276. for (i = 0; i < hashSize; i++)
  277. if (HashTable[i] != NULL) {
  278. aux = HashTable[i];
  279. while (aux != NULL) {
  280. nr++;
  281. temp[nr] = (char*)malloc(strlen(aux->key) + 1);
  282. strcpy(temp[nr], aux->key);
  283. aux = aux->Next;
  284. }
  285. }
  286. if (val == 2)
  287. hashSize *= 2;
  288. else {
  289. hashSize /= 2;
  290. if (hashSize < 2)
  291. return;
  292. }
  293. free(HashTable);
  294. init();
  295. for (i = 0; i <= nr; i++)
  296. add(temp[i]);
  297. }
  298.  
  299. void interpret(char *line) {
  300. char *p;
  301. int j;
  302. p = strtok(line, " \n");
  303. if (p!=NULL)
  304. if (strcmp(p, "add") == 0) {
  305. p = strtok(NULL, " \n");
  306. add(p);
  307. }
  308. else
  309. if (strcmp(p, "remove") == 0) {
  310. p = strtok(NULL, " \n");
  311. erase(p);
  312. }
  313. else
  314. if (strcmp(p, "find") == 0) {
  315. p = strtok(NULL, " \n");
  316. char *q = NULL;
  317. q = (char*)malloc(strlen(p) + 1);
  318. strcpy(q, p);
  319. p = strtok(NULL, " \n");
  320. find(q, p);
  321. }
  322. else
  323. if (strcmp(p, "clear") == 0)
  324. clearHash();
  325. else
  326. if (strcmp(p, "print_bucket") == 0) {
  327. p = strtok(NULL, " \n");
  328. char *q = NULL;
  329. q = (char*)malloc(strlen(p) + 1);
  330. strcpy(q, p);
  331. int index = atoi(q);
  332. p = strtok(NULL, " \n");
  333. ret_bucket(index, p);
  334. }
  335. else
  336. if (strcmp(p, "print") == 0) {
  337. p = strtok(NULL, " \n");
  338. ret_hash(p);
  339. }
  340. else
  341. if (strcmp(p, "resize") == 0) {
  342. p = strtok(NULL, " \n");
  343. if (strcmp(p, "double") == 0)
  344. resize(2);
  345. else
  346. if (strcmp(p, "halve") == 0)
  347. resize(1);
  348. }
  349. else
  350. exit(0);
  351. }
  352.  
  353. int main(int argv, char *argc[]) {
  354. FILE *f;
  355. char *line;
  356. int i;
  357. if (argv < 2) {
  358. fprintf(stderr,"eroare");
  359. exit(0);
  360. }
  361. else {
  362. hashSize = atoi(argc[1]);
  363. if(hashSize<=0)fprintf(stderr, "eroare");
  364. init();
  365. if (argv == 2)
  366. {
  367. f=stdin;
  368. while (!feof(f))
  369. {
  370. line=(char*)malloc(20000);
  371. fgets(line, 20000, f);
  372. interpret(line);
  373. free(line);
  374. }
  375. }
  376. else if (argv > 2) {
  377. for (i = 2; i < argv; i++) {
  378. f = fopen(argc[i], "r");
  379. line=(char*)malloc(20000);
  380. if(f==NULL) {
  381. fprintf(stderr,"eroare");
  382. }
  383. while (fgets(line,20000,f)) {
  384. interpret(line);
  385. }
  386. fclose(f);
  387. free(line);
  388. }
  389. }
  390. }
  391. return 0;
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement