GithubHelp home page GithubHelp logo

aniket-acad / data_structures Goto Github PK

View Code? Open in Web Editor NEW
17.0 10.0 11.0 59 KB

This repository has all the source code required for Data Structures courses CSL 102 and CSL 210 @ IIIT Nagpur

C 100.00%
array linked-list tree data-structures cprogramming graph hashtable

data_structures's Introduction

csl102_ds_iiitn

This repository has all the source code required for Data Structures course CSL 102 @ IIIT Nagpur

data_structures's People

Contributors

aniket-acad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

data_structures's Issues

Loss after deletion overlap

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.