From 2b3a002ccc36b46e1832c7088095c90e06e89422 Mon Sep 17 00:00:00 2001 From: createunique <142560870+createunique@users.noreply.github.com> Date: Fri, 8 Sep 2023 09:19:46 +0530 Subject: [PATCH 1/2] Hashing_O(1)_TC.c --- HASHING/Hashing_O(1)_TC.c | 159 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 HASHING/Hashing_O(1)_TC.c diff --git a/HASHING/Hashing_O(1)_TC.c b/HASHING/Hashing_O(1)_TC.c new file mode 100644 index 0000000..8df34de --- /dev/null +++ b/HASHING/Hashing_O(1)_TC.c @@ -0,0 +1,159 @@ +#include +#include +#include + +#define MAX_SIZE 1000 + +typedef struct +{ + int arr[MAX_SIZE]; + int size; + int* map; +}temp; + +void initialize(temp* ds) +{ + ds->size= 0; + ds->map =(int*)calloc(MAX_SIZE, sizeof(int)); // to initialise all with zeroe's + + if (ds->map == NULL) + { + printf("Memory allocation failed for initialisation !!!!\n"); + exit(1); + } +} + +void add(temp* ds, int x) +{ + if (ds->map[x] !=0) // duplicate Numbers are inserted again + { + printf(" %d is already present ! "); + return; + } + + int index=(ds->size)+1; + ds->arr[index] =x; + ds->size++ ; + ds->map[x]=index; // in hash map equating number index to non zero + printf("Successfully added %d \n",x); +} + +void removeNumber(temp* ds,int x) +{ + if (ds->map[x] == 0) // no number was found + { + printf("%d was not found to delete ! ",x); + return; + } + + int index = ds->map[x]; + int lastnum = ds->size; + ds->arr[index]= ds->arr[lastnum]; + ds->size-- ; + ds->map[ds->arr[index]]=index; + ds->map[x] =0; + printf("Successfully removed %d \n",x); +} + +int search(temp* ds,int x) +{ + if (ds->map[x] != 0) + return ds->map[x]; + return 0; +} + +int getRandom(temp* ds) +{ + int rand_idx = (rand() % (ds->size)) + 1; + return ds->arr[rand_idx]; +} + +void printNumbersAndHash(temp* ds) +{ + int i; printf("Array Numbers : [ "); + for (i = 1; i< ds->size; i++) + { + printf("%d , ",ds->arr[i]); + } + printf("%d ]\n",ds->arr[i]); + + printf("Hash Table : \n"); + for (int j = 1; j < MAX_SIZE; j++) + { + if (ds->map[j] != 0) { + printf("[ %d | %d ]\n",j,ds->map[j]); + } + } + printf("\n"); +} + +int main() +{ + temp ds; + initialize(&ds); + + int sel, val; + srand(time(NULL)); + + int index; + + do + { + printf("\nMenu:\n"); + printf("1. Add Number \n"); + printf("2. Remove Number \n"); + printf("3. Search Number\n"); + printf("4. Get Random Number \n"); + printf("5. Print Numbers and Hash Table\n"); + printf("6. Exit\n"); + printf("Enter your selection : "); + scanf("%d",&sel); + + switch (sel) + { + case 1: + printf("Enter number to add: "); + scanf("%d",&val); + add(&ds,val); + break; + + case 2: + printf("Enter number to remove: "); + scanf("%d",&val); + removeNumber(&ds,val); + break; + + case 3: + printf("Enter number to search: "); + scanf("%d",&val); + index = search(&ds,val); + + if (index != 0) + printf("Number found at index %d\n",index); + else + printf("Number not found\n"); + + break; + + case 4: + val = getRandom(&ds); + printf("Random number : %d\n",val); + break; + + case 5: + printNumbersAndHash(&ds); + break; + + case 6: + printf("Exiting...\n"); + break; + + default: + printf("Invalid selection . Please try again.\n"); + } + } while(sel != 6); + + + + return 0; +} From 4dc1faccdbdee9b289a1c26ed53fd609f0d0175d Mon Sep 17 00:00:00 2001 From: createunique <142560870+createunique@users.noreply.github.com> Date: Fri, 8 Sep 2023 09:24:27 +0530 Subject: [PATCH 2/2] Delete HASHING directory - wrong file creation --- HASHING/Hashing_O(1)_TC.c | 159 -------------------------------------- 1 file changed, 159 deletions(-) delete mode 100644 HASHING/Hashing_O(1)_TC.c diff --git a/HASHING/Hashing_O(1)_TC.c b/HASHING/Hashing_O(1)_TC.c deleted file mode 100644 index 8df34de..0000000 --- a/HASHING/Hashing_O(1)_TC.c +++ /dev/null @@ -1,159 +0,0 @@ -#include -#include -#include - -#define MAX_SIZE 1000 - -typedef struct -{ - int arr[MAX_SIZE]; - int size; - int* map; -}temp; - -void initialize(temp* ds) -{ - ds->size= 0; - ds->map =(int*)calloc(MAX_SIZE, sizeof(int)); // to initialise all with zeroe's - - if (ds->map == NULL) - { - printf("Memory allocation failed for initialisation !!!!\n"); - exit(1); - } -} - -void add(temp* ds, int x) -{ - if (ds->map[x] !=0) // duplicate Numbers are inserted again - { - printf(" %d is already present ! "); - return; - } - - int index=(ds->size)+1; - ds->arr[index] =x; - ds->size++ ; - ds->map[x]=index; // in hash map equating number index to non zero - printf("Successfully added %d \n",x); -} - -void removeNumber(temp* ds,int x) -{ - if (ds->map[x] == 0) // no number was found - { - printf("%d was not found to delete ! ",x); - return; - } - - int index = ds->map[x]; - int lastnum = ds->size; - ds->arr[index]= ds->arr[lastnum]; - ds->size-- ; - ds->map[ds->arr[index]]=index; - ds->map[x] =0; - printf("Successfully removed %d \n",x); -} - -int search(temp* ds,int x) -{ - if (ds->map[x] != 0) - return ds->map[x]; - return 0; -} - -int getRandom(temp* ds) -{ - int rand_idx = (rand() % (ds->size)) + 1; - return ds->arr[rand_idx]; -} - -void printNumbersAndHash(temp* ds) -{ - int i; printf("Array Numbers : [ "); - for (i = 1; i< ds->size; i++) - { - printf("%d , ",ds->arr[i]); - } - printf("%d ]\n",ds->arr[i]); - - printf("Hash Table : \n"); - for (int j = 1; j < MAX_SIZE; j++) - { - if (ds->map[j] != 0) { - printf("[ %d | %d ]\n",j,ds->map[j]); - } - } - printf("\n"); -} - -int main() -{ - temp ds; - initialize(&ds); - - int sel, val; - srand(time(NULL)); - - int index; - - do - { - printf("\nMenu:\n"); - printf("1. Add Number \n"); - printf("2. Remove Number \n"); - printf("3. Search Number\n"); - printf("4. Get Random Number \n"); - printf("5. Print Numbers and Hash Table\n"); - printf("6. Exit\n"); - printf("Enter your selection : "); - scanf("%d",&sel); - - switch (sel) - { - case 1: - printf("Enter number to add: "); - scanf("%d",&val); - add(&ds,val); - break; - - case 2: - printf("Enter number to remove: "); - scanf("%d",&val); - removeNumber(&ds,val); - break; - - case 3: - printf("Enter number to search: "); - scanf("%d",&val); - index = search(&ds,val); - - if (index != 0) - printf("Number found at index %d\n",index); - else - printf("Number not found\n"); - - break; - - case 4: - val = getRandom(&ds); - printf("Random number : %d\n",val); - break; - - case 5: - printNumbersAndHash(&ds); - break; - - case 6: - printf("Exiting...\n"); - break; - - default: - printf("Invalid selection . Please try again.\n"); - } - } while(sel != 6); - - - - return 0; -}