Last active
November 29, 2021 13:44
-
-
Save giljr/5ac4adb0208f405fe930de176b5eb0e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define VECTORSIZE 10 | |
//Typedef definition | |
typedef struct HashList | |
{ int id; | |
char student[50]; | |
char email[50]; | |
struct HashList *prox; | |
} HashList_t; | |
typedef struct Table | |
{ | |
int Size; | |
HashList_t **ListKey; | |
} Table_t; | |
//Prototypes | |
int menu(); | |
int HashingFunction(int); | |
void InsertHash(Table_t *, int, char[], char[], int); | |
void ShowHash(Table_t *); | |
void RemoveHash(Table_t *, int, int); | |
void SearchHash(Table_t *, int, int); | |
int main() | |
{ | |
int op, c, id, pos; | |
Table_t *TableHash = (Table_t *)malloc(sizeof(Table_t)); | |
TableHash->Size = VECTORSIZE; | |
TableHash->ListKey = (HashList_t **)malloc(VECTORSIZE * sizeof(HashList_t *)); | |
char student[50], email[50]; | |
for (int i = 0; i < VECTORSIZE; i++) | |
TableHash->ListKey[i] = NULL; | |
while (1) | |
{ | |
op = menu(); | |
switch (op) | |
{ | |
case 1: //insert | |
printf("Type the id of the student: "); | |
scanf_s("%d", &id); | |
while ((c = getchar()) != '\n' && c != EOF) | |
{ | |
} | |
printf("Type the name of the student: "); | |
scanf_s("%[a-zA-z ]]", &student); | |
while ((c = getchar()) != '\n' && c != EOF) | |
{ | |
} | |
printf("Type the email of the student: "); | |
scanf_s("%[a-zA-z@ ]]", &email); | |
while ((c = getchar()) != '\n' && c != EOF) | |
{ | |
} | |
pos = HashingFunction(id); | |
printf("Inserting in position %d of Table...\n", pos); | |
system("pause"); | |
InsertHash(TableHash, id, student, email, pos); | |
break; | |
case 2: //remove | |
printf("Type the number to remove: "); | |
scanf_s("%d", &id); | |
while ((c = getchar()) != '\n' && c != EOF) | |
{ | |
} | |
pos = HashingFunction(id); | |
printf("Removing from position %d of Table...\n", pos); | |
printf("WARNING: The System returns if nothing is found, ok? \n"); | |
system("pause"); | |
RemoveHash(TableHash, id, pos); | |
break; | |
case 3: //search | |
printf("Type the number to search: "); | |
scanf_s("%d", &id); | |
while ((c = getchar()) != '\n' && c != EOF) | |
{ | |
} | |
pos = HashingFunction(id); | |
printf("Searching for idber %i ...\n", id); | |
printf("WARNING: The System returns if nothing is found, ok? \n"); | |
system("pause"); | |
SearchHash(TableHash, id, pos); | |
system("pause"); | |
break; | |
case 4: //show | |
ShowHash(TableHash); | |
system("pause"); | |
break; | |
case 5: | |
return 0; | |
break; | |
default: | |
printf("Invalid\n"); | |
break; | |
} | |
} | |
return 0; | |
} | |
int menu() | |
{ | |
int op, c; | |
system("Cls"); | |
printf("1. Insert in Hash\n"); | |
printf("2. Remove from Hash\n"); | |
printf("3. Search the Hash\n"); | |
printf("4. Show the Hash\n"); | |
printf("5. Exit\n"); | |
printf("Choose the operation: "); | |
scanf_s("%d", &op); | |
while ((c = getchar()) != '\n' && c != EOF) | |
{ | |
} | |
system("Cls"); | |
return op; | |
} | |
int HashingFunction(int id) | |
{ | |
return (id % VECTORSIZE); | |
} | |
void InsertHash(Table_t *TableHash, int id, char student[50], char email[50], int pos) | |
{ | |
HashList_t *NewItem; | |
NewItem = (HashList_t *)malloc(sizeof(HashList_t)); | |
NewItem->id = id; | |
strcpy(NewItem->student, student); | |
strcpy(NewItem->email, email); | |
NewItem->prox = NULL; | |
NewItem->prox = TableHash->ListKey[pos]; | |
TableHash->ListKey[pos] = NewItem; | |
} | |
void SearchHash(Table_t *TableHash, int id, int pos) | |
{ | |
HashList_t *ScanItem; | |
ScanItem = (HashList_t *)malloc(sizeof(TableHash)); | |
ScanItem = TableHash->ListKey[pos]; | |
HashList_t *Auxiliar; | |
Auxiliar = (HashList_t *)malloc(sizeof(HashList_t)); | |
if (TableHash->ListKey[pos]->id == id) | |
{ | |
TableHash->ListKey[pos] = TableHash->ListKey[pos]->prox; | |
printf("YES, The The student RU-%d was found in table!\n", id); | |
} | |
else | |
{ | |
while (ScanItem->id != id) | |
{ | |
Auxiliar = ScanItem; | |
ScanItem = ScanItem->prox; | |
} | |
Auxiliar->prox = ScanItem->prox; | |
printf("YES, The student RU-%d was found in table!\n", id); | |
} | |
} | |
void RemoveHash(Table_t *TableHash, int id, int pos) | |
{ | |
HashList_t *ScanItem; | |
ScanItem = (HashList_t *)malloc(sizeof(TableHash)); | |
ScanItem = TableHash->ListKey[pos]; | |
HashList_t *Auxiliar; | |
Auxiliar = (HashList_t *)malloc(sizeof(HashList_t)); | |
if (TableHash->ListKey[pos]->id == id) | |
{ | |
TableHash->ListKey[pos] = TableHash->ListKey[pos]->prox; | |
free(ScanItem); | |
} | |
else | |
{ | |
while (ScanItem->id != id) | |
{ | |
Auxiliar = ScanItem; | |
ScanItem = ScanItem->prox; | |
} | |
Auxiliar->prox = ScanItem->prox; | |
free(ScanItem); | |
} | |
} | |
void ShowHash(Table_t *TableHash) | |
{ | |
int v; | |
for (v = 0; v < TableHash->Size; v++) | |
{ | |
HashList_t *ScanItem = TableHash->ListKey[v]; | |
printf("\n List of keys by position %d:\n ", v); | |
while (ScanItem != NULL) | |
{ | |
printf("%d -> ", ScanItem->id); | |
printf("\n_____________________________________\n"); | |
printf("RU: %i\n", ScanItem->id); | |
printf("Student: %s\n", ScanItem->student); | |
printf("Email: %s\n", ScanItem->email); | |
printf("_____________________________________\n"); | |
ScanItem = ScanItem->prox; | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment