Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loss after deletion overlap #3

Open
marklove5102 opened this issue Jun 16, 2022 · 0 comments
Open

Loss after deletion overlap #3

marklove5102 opened this issue Jun 16, 2022 · 0 comments

Comments

@marklove5102
Copy link

marklove5102 commented Jun 16, 2022

hash_table.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 5

struct keyval_item {
   int data;   
   int key;
   struct keyval_item* next;
};

struct keyval_item* dataBucket[SIZE] = {NULL}; 

int hashFunc(int key) {
   return key % SIZE;
}

struct keyval_item* search(int key) {
   //get the hash 
   int hashIndex = hashFunc(key);  
   struct keyval_item* head_item = dataBucket[hashIndex]; 

   while(NULL != head_item){
      if(head_item->key == key){
         return head_item;
      }
      head_item = head_item->next;
   }

   return NULL;
}

void insert(int key,int data) {
   struct keyval_item *item = (struct keyval_item*) malloc(sizeof(struct keyval_item));
   item->data = data;  
   item->key = key;

   //get the hash 
   int hashIndex = hashFunc(key);
   if(NULL != dataBucket[hashIndex]){
      // insert before the current and create a new head
      item->next = dataBucket[hashIndex];
   }  
   dataBucket[hashIndex] = item;
}

void delete(struct keyval_item* item) {
   int key = item->key;

   //get the hash 
   int hashIndex = hashFunc(key);
   free(dataBucket[hashIndex]);
   dataBucket[hashIndex] = NULL;    
}

void display() {
   int i = 0;
	
   for(i = 0; i<SIZE; i++) {
      if(dataBucket[i] != NULL){
         printf(" (%d,%d) pair at index = %d\n",dataBucket[i]->key,dataBucket[i]->data, i);
         struct keyval_item* np = dataBucket[i]->next;
         for(; np != NULL; np=np->next)
          printf(" (%d,%d) pair at index = %d\n",np->key,np->data,i);
	 }
   }
	
   printf("\n");
}

int main() {
   

   insert(1, 20);
   insert(2, 70);
   insert(42, 80);
   insert(4, 25);
   insert(12, 44);
   insert(14, 32);
   insert(17, 11);
   insert(13, 78);
   insert(37, 97);
   insert(1, 99);
   display();
   struct keyval_item* item = search(37);
  
   item = search(1);
   if(item != NULL) {
      printf("1Element found: %d\n", item->data);
   } else {
      printf("1Element not found\n");
   }
      item = search(2);
   if(item != NULL) {
      printf("2Element found: %d\n", item->data);
   } else {
      printf("2Element not found\n");
   }
      item = search(4);
   if(item != NULL) {
      printf("4Element found: %d\n", item->data);
   } else {
      printf("4Element not found\n");
   }
      item = search(14);
   if(item != NULL) {
      printf("14Element found: %d\n", item->data);
   } else {
      printf("14Element not found\n");
   }
      item = search(12);
   if(item != NULL) {
      printf("12Element found: %d\n", item->data);
   } else {
      printf("12Element not found\n");
   }
   
         item = search(13);
   if(item != NULL) {
      printf("13Element found: %d\n", item->data);
   } else {
      printf("13Element not found\n");
   }
         item = search(17);
   if(item != NULL) {
      printf("17Element found: %d\n", item->data);
   } else {
      printf("17Element not found\n");
   }
         item = search(42);
   if(item != NULL) {
      printf("42Element found: %d\n", item->data);
   } else {
      printf("42Element not found\n");
   }
   
   if(item != NULL) {
      printf("item Element found: %d\n", item->data);
   } else {
      printf("item Element not found\n");
   }
   item = search(37);
   delete(item);
   item = search(37);

   if(item != NULL) {
      printf("del 37 Element found: %d\n", item->data);
   } else {
      printf("del 37 Element not found\n");
   }
   
   
   item = search(1);
   if(item != NULL) {
      printf("search-1Element found: %d\n", item->data);
   } else {
      printf("search-1Element not found\n");
   }
      item = search(2);
   if(item != NULL) {
      printf("2Element found: %d\n", item->data);
   } else {
      printf("2Element not found\n");
   }
      item = search(4);
   if(item != NULL) {
      printf("4Element found: %d\n", item->data);
   } else {
      printf("4Element not found\n");
   }
      item = search(14);
   if(item != NULL) {
      printf("14Element found: %d\n", item->data);
   } else {
      printf("14Element not found\n");
   }
      item = search(12);
   if(item != NULL) {
      printf("12Element found: %d\n", item->data);
   } else {
      printf("12Element not found\n");
   }
   
         item = search(13);
   if(item != NULL) {
      printf("13Element found: %d\n", item->data);
   } else {
      printf("13Element not found\n");
   }
         item = search(17);
   if(item != NULL) {
      printf("17Element found: %d\n", item->data);
   } else {
      printf("17Element not found\n");
   }
         item = search(42);
   if(item != NULL) {
      printf("42Element found: %d\n", item->data);
   } else {
      printf("42Element not found\n");
   }
}
 (1,99) pair at index = 1
 (1,20) pair at index = 1
 (37,97) pair at index = 2
 (17,11) pair at index = 2
 (12,44) pair at index = 2
 (42,80) pair at index = 2
 (2,70) pair at index = 2
 (13,78) pair at index = 3
 (14,32) pair at index = 4
 (4,25) pair at index = 4

1Element found: 99
2Element found: 70
4Element found: 25
14Element found: 32
12Element found: 44
13Element found: 78
17Element found: 11
42Element found: 80
item Element found: 80
del 37 Element not found
search-1Element found: 99
2Element not found
4Element found: 25
14Element found: 32
12Element not found
13Element found: 78
17Element not found
42Element not found

del 37 = ALL index 2
(37,97) pair at index = 2
(17,11) pair at index = 2
(12,44) pair at index = 2
(42,80) pair at index = 2
(2,70) pair at index = 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant