0% found this document useful (0 votes)
83 views10 pages

Hash Table Implementation Guide

This C program implements a hash table using separate chaining for collision handling. It defines a struct node to store key-value pairs, with next pointers for chaining. An array of struct node pointers implements the hash table, with indexes mapped from keys using a modulo operation. Functions are defined to insert keys into the table by adding nodes to chains, search the table by traversing chains, and display the full contents of the table by traversing each chain. The main loop prompts the user to insert, display, search, or exit the program.

Uploaded by

JAYA DHARSHINI G
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)
83 views10 pages

Hash Table Implementation Guide

This C program implements a hash table using separate chaining for collision handling. It defines a struct node to store key-value pairs, with next pointers for chaining. An array of struct node pointers implements the hash table, with indexes mapped from keys using a modulo operation. Functions are defined to insert keys into the table by adding nodes to chains, search the table by traversing chains, and display the full contents of the table by traversing each chain. The main loop prompts the user to insert, display, search, or exit the program.

Uploaded by

JAYA DHARSHINI G
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/ 10

/////////////////Reg no:953619104017/////////////

//////////////////Jaya Dharshini G/////////////////

/////////////////Separate Chaining////////////////

#include <stdio.h>

#include <stdlib.h>

#define TABLE_SIZE 10

struct node

int data;

struct node *next;

};

struct node *head[TABLE_SIZE]={NULL},*c;

void insert()

int i,key;

printf("\nenter a value to insert into hash table\n");

scanf("%d",&key);

i=key%TABLE_SIZE;

struct node * newnode=(struct node *)malloc(sizeof(struct node));

newnode->data=key;

newnode->next = NULL;

if(head[i] == NULL)

head[i] = newnode;

else

{
c=head[i];

while(c->next != NULL)

c=c->next;

c->next=newnode;

void search()

int key,index;

printf("\nenter the element to be searched\n");

scanf("%d",&key);

index=key%TABLE_SIZE;

if(head[index] == NULL)

printf("\n Search element not found\n");

else

for(c=head[index];c!=NULL;c=c->next)

if(c->data == key)

printf("search element found\n");

break;

}
}

if(c==NULL)

printf("\n Search element not found\n");

void display()

int i;

for(i=0;i<TABLE_SIZE;i++)

printf("\nentries at index %d\n",i);

if(head[i] == NULL)

printf("No Hash Entry");

else

for(c=head[i];c!=NULL;c=c->next)

printf("%d->",c->data);

}
main()

int opt,key,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

You might also like