0% found this document useful (0 votes)
11 views

13

Uploaded by

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

13

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

#define TABLE_SIZE 10

struct node {
int data;
struct node *next;
};

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

void insert() {
int key;
printf("\nEnter a value to insert: ");
scanf("%d", &key);

int index = key % TABLE_SIZE;


struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = key;
newnode->next = head[index];
head[index] = newnode;
}

void display() {
for (int i = 0; i < TABLE_SIZE; i++) {
printf("Index %d: ", i);
if (head[i] == NULL) { // Check if there are no entries at this index
printf("No Hash Entry\n");
} else {
struct node *current = head[i];
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
}
}

int main() {
int choice;
while (1) {
printf("\n1. Insert\n2. Display\n3. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: insert(); break;
case 2: search(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

#include <stdio.h>

#define SIZE 7
int arr[SIZE];

void init() {
for (int i = 0; i < SIZE; i++) arr[i] = -1;
}

void insert(int value) {


int index = value % SIZE;
if (arr[index] == -1) {
arr[index] = value;
printf("%d inserted at arr[%d]\n", value, index);
} else {
printf("Collision at arr[%d]!\n", index);
}
}

void delete(int value) {


int index = value % SIZE;
if (arr[index] == value) {
arr[index] = -1;
printf("Deleted %d from arr[%d]\n", value, index);
} else {
printf("%d not found\n", value);
}
}

void search(int value) {


int index = value % SIZE;
if (arr[index] == value) {
printf("%d found at arr[%d]\n", value, index);
} else {
printf("%d not found\n", value);
}
}
void display() {
for (int i = 0; i < SIZE; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
}

int main() {
int choice, value;
init();
while (1) {
printf("\n1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Enter value to insert: "); scanf("%d", &value); insert(value); break;
case 2: printf("Enter value to delete: "); scanf("%d", &value); delete(value); break;
case 3: printf("Enter value to search: "); scanf("%d", &value); search(value); break;
case 4: display(); break;
case 5: return 0;
default: printf("Invalid choice!\n");
}
}
return 0;
}

You might also like