0% found this document useful (0 votes)
145 views5 pages

Practical File Submission: Ganpat University U.V.Patel College of Engineering

This document contains a C program implementing hash tables. The program defines a hash table data structure with array of structs to store key-value pairs. It implements functions for inserting elements into the hash table, searching for elements, and deleting elements. The main function tests the insert, search and delete functions and displays the output of the hash table.

Uploaded by

Daksh Maradiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views5 pages

Practical File Submission: Ganpat University U.V.Patel College of Engineering

This document contains a C program implementing hash tables. The program defines a hash table data structure with array of structs to store key-value pairs. It implements functions for inserting elements into the hash table, searching for elements, and deleting elements. The main function tests the insert, search and delete functions and displays the output of the hash table.

Uploaded by

Daksh Maradiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ganpat University

U.V.Patel College of Engineering


B. Tech in Computer Engineering/IT/CE-AI

Practical File Submission

2CEIT304: Data Structures

B.Tech Semester: III

Academic Year: AUG-DEC-2022 (Odd Sem.)

Prepared By:
Himanshu
21012011153
Practical 10

Aim: Implement Hash Tables

C Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define SIZE 20
struct DataItem
{
int data;
int key;
};
struct DataItem*
hashArray[SIZE];
struct DataItem* dummyItem;
struct DataItem* item;
int hashCode(int key)
{
return key % SIZE;
}
void insert(int key,int data)
{
struct DataItem*item= (structDataItem*)
malloc(sizeof(struct DataItem));
item->data=data;
item->key=key;
int hashIndex=hashCode(key);
while(hashArray[hashIndex]!=NULL && hashArray[hashIndex]->key!=-1)
{
++hashIndex;
hashIndex%=SIZE;
}
hashArray[hashIndex]=item;
}
struct DataItem*search(int key)
{
int hashIndex=hashCode(key);
while(hashArray[hashIndex]!=NULL)
{
if(hashArray[hashIndex]-
>key==key) return
21012011153 Himanshu B.Tech CE
hashArray[hashIndex];
++hashIndex;
hashIndex%=SIZE;
}
return NULL;
}
struct DataItem* delete(struct DataItem*item)
{

21012011153 Himanshu B.Tech CE


int key=item->key; int
hashIndex=hashCode(key);
while(hashArray[hashIndex]!=NULL)
{
if(hashArray[hashIndex]->key==key)
{
struct DataItem*temp=hashArray[hashIndex];
hashArray[hashIndex]=dummyItem; return
temp;
}
++hashIndex;
hashIndex%=SIZE;
}
return
NULL;
}
void display()
{
int i=0;
for(i=0;i<SIZE;i++)
{
if(hashArray[i]!=NULL)
printf("(%d,%d)",hashArray[i]->key,hashArray[i]->data); else
printf("~");
}
printf("\n");
}
int main()
{
printf("\n21012011153 \n Himanshu\n");
dummyItem=(struct DataItem*)malloc(sizeof(struct DataItem));
dummyItem->data=-1;
dummyItem->key=-1;
insert(1,98);
insert(2,70);
insert(42,40);
insert(4,25);
insert(12,44);
insert(14,43);
insert(17,34);
insert(13,75);
insert(37,47);
display();
item=search(37);
if(item!=NULL)
{
printf("element found:%d\n",item->data);

21012011153 Himanshu B.Tech CE


}
else
{
printf("element not found\n");
}
delete(item); item=search(37);
if(item!=NULL)
printf("element found:%d\n",item->data);
else
printf("element not found\n");
}

OUTPUT:-

21012011153 Himanshu B.Tech CE

You might also like