Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int hashSize;
- typedef struct Node {
- char *key;
- struct Node *Next;
- }Node;
- Node **HashTable;
- void init() {
- HashTable = (Node**)malloc(hashSize * (sizeof(Node*)));
- int i = 0;
- for (i = 0; i < hashSize; i++)
- HashTable[i] = NULL;
- }
- void add(const char *cheie ) {
- int index = hash(cheie, hashSize), ok=1;
- Node *nod = (Node*)malloc(sizeof(Node)), *aux = NULL, *aux2=NULL;
- nod->key = (char*)malloc(strlen(cheie) + 1);
- strcpy(nod->key, cheie);
- nod->Next = NULL;
- if (HashTable[index] == NULL)
- HashTable[index] = nod;
- else {
- aux = HashTable[index];
- ok = 1;
- if (strcmp(aux->key, cheie) == 0)
- ok = 0;
- while (aux!=NULL) {
- aux2 = aux;
- if (strcmp(aux->key, cheie) == 0)
- ok = 0;
- aux = aux->Next;
- }
- if (ok == 1)
- aux2->Next = nod;
- else
- fprintf(stderr,"eroare");
- free(aux);
- }
- }
- void erase(const char *cheie) {
- int index = hash(cheie, hashSize), ok=0;
- Node *aux=NULL, *p=NULL, *aux2=NULL;
- if (HashTable[index] != NULL)
- if (strcmp(HashTable[index]->key, cheie) == 0)
- if (HashTable[index]->Next != NULL) {
- aux = HashTable[index]->Next;
- free(HashTable[index]);
- HashTable[index] = aux;
- }
- else {
- free(HashTable[index]);
- HashTable[index] = NULL;
- }
- else {
- aux = HashTable[index];
- p = NULL;
- ok = 0;
- while (aux != NULL) {
- p = aux;
- aux = aux->Next;
- if (strcmp(aux->key, cheie) == 0) {
- ok = 1;
- break;
- }
- }
- if (ok == 1) {
- if (aux->Next != NULL) {
- aux2 = aux->Next;
- free(aux);
- p->Next = aux2;
- }
- else {
- free(aux);
- p->Next = NULL;
- }
- }
- }
- }
- void find(const char *cheie, const char *fisier)
- {
- int index = hash(cheie, hashSize), ok = 0, size = 0;
- FILE *f = NULL;
- Node *aux = NULL;
- if (HashTable[index] != NULL) {
- aux = HashTable[index];
- while (aux != NULL) {
- if (strcmp(aux->key, cheie) == 0)
- ok = 1;
- aux = aux->Next;
- }
- if (ok == 1)
- if (fisier!=NULL) {
- f = fopen(fisier, "a+");
- if (f==NULL)
- fprintf(stderr,"eroare");
- else {
- fclose(f);
- f = fopen(fisier, "r");
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fclose(f);
- f = fopen(fisier, "a+");
- if (size != 0)
- fprintf(f, "True\n");
- else
- fprintf(f, "True\n");
- fclose(f);
- }
- }
- else
- printf("True\n");
- else {
- if (fisier != NULL) {
- f = fopen(fisier, "a+");
- fclose(f);
- f = fopen(fisier, "r");
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fclose(f);
- f = fopen(fisier, "a+");
- if (size != 0)
- fprintf(f, "False\n");
- else
- fprintf(f, "False\n");
- fclose(f);
- }
- else
- printf("False\n");
- }
- free(aux);
- }
- else
- if (fisier != NULL) {
- f = fopen(fisier, "a+");
- fprintf(f, "False\n");
- fclose(f);
- }
- else
- printf("False\n");
- }
- void clearHash() {
- int i = 0;
- Node *p = NULL, *aux = NULL;
- for (i = 0; i < hashSize; i++)
- if (HashTable[i] != NULL) {
- aux = HashTable[i];
- while (aux != NULL) {
- p = aux;
- aux = aux->Next;
- free(p);
- }
- HashTable[i] = NULL;
- }
- }
- void print() {
- int i = 0;
- Node* aux = NULL;
- for (i = 0; i <hashSize; i++)
- if (HashTable[i] != NULL) {
- if (HashTable[i]->Next != NULL) {
- aux = HashTable[i];
- while (aux != NULL) {
- if (aux->Next != NULL)
- printf("%s ", aux->key);
- else
- printf("%s", aux->key);
- aux = aux->Next;
- }
- }
- else
- printf("%s", HashTable[i]->key);
- printf("\n");
- }
- free (aux);
- }
- void ret_hash(const char *fisier) {
- int i = 0, nr = 0, size = 0;
- FILE *f = NULL;
- Node *aux = NULL;
- if (fisier == 0)
- print();
- else {
- f = fopen(fisier, "a+");
- fclose(f);
- f = fopen(fisier, "r");
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fclose(f);
- f = fopen(fisier, "a+");
- for (i = 0; i < hashSize; i++)
- if (HashTable[i] != NULL)
- nr++;
- if (nr != 0 && size!=0)
- fprintf(f, "\n");
- for (i = 0; i < hashSize; i++) {
- aux = HashTable[i];
- while (aux != NULL) {
- if (aux->Next != NULL)
- fprintf(f, "%s ", aux->key);
- else
- fprintf(f, "%s", aux->key);
- aux = aux->Next;
- }
- if(HashTable[i]!=NULL)
- fprintf(f, "\n");
- }
- }
- }
- void print_bucket(int index) {
- Node *aux=NULL;
- if (HashTable[index] != NULL) {
- aux = HashTable[index];
- while (aux != NULL) {
- if (aux->Next != NULL)
- printf("%s ", aux->key);
- else
- printf("%s", aux->key);
- aux = aux->Next;
- }
- printf("\n");
- }
- }
- void ret_bucket(int index, const char *fisier) {
- Node *aux=NULL;
- FILE *f=NULL;
- if (index<0 || index >hashSize || HashTable[index]==NULL || strchr("qwertyuiopasdfghjklzxcvbnm",index)) {
- fprintf(stderr,"eroare");
- }
- else
- if (fisier == 0)
- print_bucket(index);
- else {
- aux = HashTable[index];
- f = fopen(fisier, "a+");
- while (aux != NULL) {
- if (aux->Next != NULL)
- fprintf(f, "%s ", aux->key);
- else
- fprintf(f, "%s", aux->key);
- aux = aux->Next;
- }
- fprintf(f, "\n");
- fclose(f);
- }
- }
- void resize(int val) {
- char **temp = NULL;
- int nr = 0;
- int i = 0;
- Node *aux = NULL;
- for (i = 0; i < hashSize; i++)
- if (HashTable[i] != NULL) {
- aux = HashTable[i];
- while (aux != NULL) {
- nr++;
- aux = aux->Next;
- }
- }
- temp = (char**)malloc(nr * sizeof(char*));
- nr = -1;
- for (i = 0; i < hashSize; i++)
- if (HashTable[i] != NULL) {
- aux = HashTable[i];
- while (aux != NULL) {
- nr++;
- temp[nr] = (char*)malloc(strlen(aux->key) + 1);
- strcpy(temp[nr], aux->key);
- aux = aux->Next;
- }
- }
- if (val == 2)
- hashSize *= 2;
- else {
- hashSize /= 2;
- if (hashSize < 2)
- return;
- }
- free(HashTable);
- init();
- for (i = 0; i <= nr; i++)
- add(temp[i]);
- }
- void interpret(char *line) {
- char *p;
- int j;
- p = strtok(line, " \n");
- if (p!=NULL)
- if (strcmp(p, "add") == 0) {
- p = strtok(NULL, " \n");
- add(p);
- }
- else
- if (strcmp(p, "remove") == 0) {
- p = strtok(NULL, " \n");
- erase(p);
- }
- else
- if (strcmp(p, "find") == 0) {
- p = strtok(NULL, " \n");
- char *q = NULL;
- q = (char*)malloc(strlen(p) + 1);
- strcpy(q, p);
- p = strtok(NULL, " \n");
- find(q, p);
- }
- else
- if (strcmp(p, "clear") == 0)
- clearHash();
- else
- if (strcmp(p, "print_bucket") == 0) {
- p = strtok(NULL, " \n");
- char *q = NULL;
- q = (char*)malloc(strlen(p) + 1);
- strcpy(q, p);
- int index = atoi(q);
- p = strtok(NULL, " \n");
- ret_bucket(index, p);
- }
- else
- if (strcmp(p, "print") == 0) {
- p = strtok(NULL, " \n");
- ret_hash(p);
- }
- else
- if (strcmp(p, "resize") == 0) {
- p = strtok(NULL, " \n");
- if (strcmp(p, "double") == 0)
- resize(2);
- else
- if (strcmp(p, "halve") == 0)
- resize(1);
- }
- else
- exit(0);
- }
- int main(int argv, char *argc[]) {
- FILE *f;
- char *line;
- int i;
- if (argv < 2) {
- fprintf(stderr,"eroare");
- exit(0);
- }
- else {
- hashSize = atoi(argc[1]);
- if(hashSize<=0)fprintf(stderr, "eroare");
- init();
- if (argv == 2)
- {
- f=stdin;
- while (!feof(f))
- {
- line=(char*)malloc(20000);
- fgets(line, 20000, f);
- interpret(line);
- free(line);
- }
- }
- else if (argv > 2) {
- for (i = 2; i < argv; i++) {
- f = fopen(argc[i], "r");
- line=(char*)malloc(20000);
- if(f==NULL) {
- fprintf(stderr,"eroare");
- }
- while (fgets(line,20000,f)) {
- interpret(line);
- }
- fclose(f);
- free(line);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement