0% found this document useful (0 votes)
9 views15 pages

Assing 8

The document outlines the implementation of a hash table using linked lists in C, including functions for inserting, searching, deleting, and displaying keys. It features a menu-driven interface for user interaction and supports separate chaining for collision resolution. The hash table can handle basic operations efficiently with a defined table size of 10.

Uploaded by

yashodapawar10
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)
9 views15 pages

Assing 8

The document outlines the implementation of a hash table using linked lists in C, including functions for inserting, searching, deleting, and displaying keys. It features a menu-driven interface for user interaction and supports separate chaining for collision resolution. The hash table can handle basic operations efficiently with a defined table size of 10.

Uploaded by

yashodapawar10
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/ 15

setA

#include <stdio.h>

#include <stdlib.h>

#define TABLE_SIZE 10

// Define a node for the linked list

struct Node {

int key;

struct Node* next;

};

// Define a hash table structure

struct HashTable {

struct Node* table[TABLE_SIZE];

};

// Function to create a new node

struct Node* createNode(int key) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->key = key;

newNode->next = NULL;

return newNode;

}
// Hash function (modulo method)

int hashFunction(int key) {

return key % TABLE_SIZE;

// Initialize the hash table

void initializeTable(struct HashTable* hashTable) {

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

hashTable->table[i] = NULL;

// Insert a key into the hash table

void insert(struct HashTable* hashTable, int key) {

int index = hashFunction(key);

struct Node* newNode = createNode(key);

// Insert at the beginning of the linked list (separate chaining)

newNode->next = hashTable->table[index];

hashTable->table[index] = newNode;

printf("Key %d inserted at index %d\n", key, index);

// Search for a key in the hash table

void search(struct HashTable* hashTable, int key) {


int index = hashFunction(key);

struct Node* current = hashTable->table[index];

while (current) {

if (current->key == key) {

printf("Key %d found at index %d\n", key, index);

return;

current = current->next;

printf("Key %d not found.\n", key);

// Delete a key from the hash table

void delete(struct HashTable* hashTable, int key) {

int index = hashFunction(key);

struct Node* current = hashTable->table[index];

struct Node* prev = NULL;

while (current) {

if (current->key == key) {

if (prev) {

prev->next = current->next;

} else {

hashTable->table[index] = current->next;
}

free(current);

printf("Key %d deleted from index %d\n", key, index);

return;

prev = current;

current = current->next;

printf("Key %d not found for deletion.\n", key);

// Display the hash table

void display(struct HashTable* hashTable) {

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

struct Node* current = hashTable->table[i];

printf("Index %d: ", i);

while (current) {

printf("%d -> ", current->key);

current = current->next;

printf("NULL\n");

// Menu-driven program to perform operations


void menu() {

struct HashTable hashTable;

initializeTable(&hashTable);

int choice, key;

while (1) {

printf("\nMenu:\n");

printf("1. Insert Key\n");

printf("2. Search Key\n");

printf("3. Delete Key\n");

printf("4. Display Hash Table\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(&hashTable, key);

break;

case 2:

printf("Enter key to search: ");

scanf("%d", &key);

search(&hashTable, key);
break;

case 3:

printf("Enter key to delete: ");

scanf("%d", &key);

delete(&hashTable, key);

break;

case 4:

display(&hashTable);

break;

case 5:

printf("Exiting the program.\n");

return;

default:

printf("Invalid choice. Please try again.\n");

// Main function

int main() {

menu();

return 0;

Menu:
1. Insert Key

2. Search Key

3. Delete Key

4. Display Hash Table

5. Exit

Enter your choice: 1

Enter key to insert: 15

Key 15 inserted at index


setB

#include <stdio.h>

#include <stdlib.h>

#define TABLE_SIZE 10

// Define a node for the doubly linked list

struct Node {

int key;

struct Node* prev;

struct Node* next;

};

// Define the hash table structure

struct HashTable {

struct Node* table[TABLE_SIZE];

};

// Function to create a new node

struct Node* createNode(int key) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->key = key;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;
}

// Hash function (modulo method)

int hashFunction(int key) {

return key % TABLE_SIZE;

// Initialize the hash table

void initializeTable(struct HashTable* hashTable) {

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

hashTable->table[i] = NULL;

// Insert a key into the hash table

void insert(struct HashTable* hashTable, int key) {

int index = hashFunction(key);

struct Node* newNode = createNode(key);

// Insert at the beginning of the doubly linked list (separate chaining)

if (hashTable->table[index] == NULL) {

hashTable->table[index] = newNode;

} else {

struct Node* head = hashTable->table[index];

newNode->next = head;
head->prev = newNode;

hashTable->table[index] = newNode;

printf("Key %d inserted at index %d\n", key, index);

// Search for a key in the hash table

void search(struct HashTable* hashTable, int key) {

int index = hashFunction(key);

struct Node* current = hashTable->table[index];

while (current) {

if (current->key == key) {

printf("Key %d found at index %d\n", key, index);

return;

current = current->next;

printf("Key %d not found.\n", key);

// Delete a key from the hash table

void delete(struct HashTable* hashTable, int key) {

int index = hashFunction(key);

struct Node* current = hashTable->table[index];


while (current) {

if (current->key == key) {

if (current->prev) {

current->prev->next = current->next;

} else {

hashTable->table[index] = current->next;

if (current->next) {

current->next->prev = current->prev;

free(current);

printf("Key %d deleted from index %d\n", key, index);

return;

current = current->next;

printf("Key %d not found for deletion.\n", key);

// Display the hash table

void display(struct HashTable* hashTable) {

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

struct Node* current = hashTable->table[i];

printf("Index %d: ", i);


while (current) {

printf("%d <-> ", current->key);

current = current->next;

printf("NULL\n");

// Menu-driven program to perform operations

void menu() {

struct HashTable hashTable;

initializeTable(&hashTable);

int choice, key;

while (1) {

printf("\nMenu:\n");

printf("1. Insert Key\n");

printf("2. Search Key\n");

printf("3. Delete Key\n");

printf("4. Display Hash Table\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {
case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(&hashTable, key);

break;

case 2:

printf("Enter key to search: ");

scanf("%d", &key);

search(&hashTable, key);

break;

case 3:

printf("Enter key to delete: ");

scanf("%d", &key);

delete(&hashTable, key);

break;

case 4:

display(&hashTable);

break;

case 5:

printf("Exiting the program.\n");

return;

default:

printf("Invalid choice. Please try again.\n");

}
}

// Main function

int main() {

menu();

return 0;

Menu:

1. Insert Key

2. Search Key

3. Delete Key

4. Display Hash Table

5. Exit

Enter your choice: 1

Enter key to insert: 15

Key 15 inserted at index 5

Menu:

1. Insert Key

2. Search Key

3. Delete Key

4. Display Hash Table

5. Exit

Enter your choice: 1

Enter key to insert: 25


Key 25 inserted at index 5

Menu:

1. Insert Key

2. Search Key

3. Delete Key

4. Display Hash Table

5. Exit

Enter your choice: 4

Index 0: NULL

Index 1: NULL

Index 2: NULL

Index 3: NULL

Index 4: NULL

Index 5: 25 <-> 15 <-> NULL

Index 6: NULL

Index 7: NULL

Index 8: NULL

Index 9: N

You might also like