SnehaDSAassignment
SnehaDSAassignment
01
Set - A
Q.a) Implement a Binary search tree (BST) library (btree.h) with operations create, search,
insert, inorder, preorder and postorder. Write a menu driven program that performs the
above operations.
Ans:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* createTree() {
return NULL;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
Node* search(Node* root, int data) {
if (root == NULL || root->data == data) {
return root;
}
if (data < root->data) {
return search(root->left, data);
}
return search(root->right, data);
}
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
void displayMenu() {
printf("\nBinary Search Tree Operations:\n");
printf("1. Create Tree\n");
printf("2. Insert Element\n");
printf("3. Search Element\n");
printf("4. Inorder Traversal\n");
printf("5. Preorder Traversal\n");
printf("6. Postorder Traversal\n");
printf("7. Exit\n");
printf("Enter your choice: ");
}
int main() {
Node* root = NULL;
int choice, value;
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
root = createTree();
printf("Tree created successfully.\n");
for (int i = 0; i < 5; i++) {
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
printf("Element %d inserted.\n", value);
}
break;
case 2:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
printf("Element inserted.\n");
break;
case 3:
printf("Enter value to search: ");
scanf("%d", &value);
Node* result = search(root, value);
if (result != NULL) {
printf("Element found: %d\n", result->data);
} else {
printf("Element not found.\n");
}
break;
case 4:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 5:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 6:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 7:
printf("Exiting program.\n");
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Output :
Q.b) Write a program which uses binary search tree library and counts the total nodes and
total leaf nodes in the tree.
int count(T)-returns the total number of nodes from BST
int countLeaf(T)-returns the total number of leaf nodes from BST.
Ans:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node* createNode(int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
int count(Node* root) {
if (root == NULL) {
return 0;
}
return 1 + count(root->left) + count(root->right);
}
int countLeaf(Node* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaf(root->left) + countLeaf(root->right);
}
void inOrderTraversal(Node* root) {
if (root == NULL) {
return;
}
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
int main() {
Node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
int totalNodes = count(root);
int leafNodes = countLeaf(root);
printf("Total nodes in the BST: %d\n", totalNodes);
printf("Total leaf nodes in the BST: %d\n", leafNodes);
printf("Values in the BST: ");
inOrderTraversal(root);
printf("\n");
return 0;
}
Output :
Set - B
Q.a) Write a C program which uses Binary search tree library and implements following
function with recursion:
Tcopy(T) create another BST which is exact copy of BST which is passed as parameter int
compare(T1, T2)-compares two hinary search trees and returns 1 if they are equal and 0
otherwise.
Ans :
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
Node* Tcopy(Node* root) {
if (root == NULL) {
return NULL;
}
Node* newNode = createNode(root->data);
newNode->left = Tcopy(root->left);
newNode->right = Tcopy(root->right);
return newNode;
}
int compare(Node* root1, Node* root2) {
if (root1 == NULL && root2 == NULL) {
return 1;
}
if (root1 == NULL || root2 == NULL) {
return 0;
return (root1->data == root2->data) && compare(root1->left, root2->left) && compare(root1-
>right, root2->right);
}
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
int main() {
Node* root1 = NULL;
root1 = insert(root1, 50);
root1 = insert(root1, 30);
root1 = insert(root1, 20);
root1 = insert(root1, 40);
root1 = insert(root1, 70);
root1 = insert(root1, 60);
root1 = insert(root1, 80);
Node* root2 = Tcopy(root1);
printf("Inorder traversal of T1: ");
inorder(root1);
printf("\n");
printf("Inorder traversal of T2 (copy of T1): ");
inorder(root2);
printf("\n");
if (compare(root1, root2)) {
printf("Both trees are equal.\n");
} else {
printf("Both trees are not equal.\n");
}
return 0;
}
Output :
Assignment No. 02
Set - A
Q.1) Write a C program which uses Binary search tree library and displays nodes at each
level,count of node at each level and total levels in the tree.
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
void levelOrder(struct Node* root) {
if (root == NULL) return;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
int level = 0;
while (front < rear) {
int nodeCount = rear - front;
printf("Level %d: ", level);
while (nodeCount > 0) {
struct Node* currentNode = queue[front++];
printf("%d ", currentNode->data);
if (currentNode->left != NULL) {
queue[rear++] = currentNode->left;
}
if (currentNode->right != NULL) {
queue[rear++] = currentNode->right;
}
nodeCount--;
}
printf("\n");
level++;
}
}
int countLevels(struct Node* root) {
if (root == NULL) {
return 0;
}
int leftHeight = countLevels(root->left);
int rightHeight = countLevels(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
void countNodesAtEachLevel(struct Node* root) {
if (root == NULL) return;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
int level = 0;
while (front < rear) {
int nodeCount = rear - front;
printf("Number of nodes at Level %d: %d\n", level, nodeCount);
while (nodeCount > 0) {
struct Node* currentNode = queue[front++];
if (currentNode->left != NULL) {
queue[rear++] = currentNode->left;
}
if (currentNode->right != NULL) {
queue[rear++] = currentNode->right;
}
nodeCount--;
}
level++;
}
}
int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
levelOrder(root);
countNodesAtEachLevel(root);
int totalLevels = countLevels(root);
printf("Total number of levels in the tree: %d\n", totalLevels);
return 0;
}
OUTPUT :
Set - B
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
int main() {
int n;
srand(time(0));
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
arr[i] = rand() % 100;
}
printf("Before sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
heapSort(arr, n);
printf("After sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT :
Assignment No. 03
Set - A
a) Write a C program that accepts the vertices and edges of a graph and stores it as an
adjacency matrix. Display the adjacency matrix.
Ans :
#include <stdio.h>
int main() {
int vertices, edges, i, j, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int adjMatrix[vertices][vertices];
printf("Adjacency Matrix:\n");
for(i = 0; i < vertices; i++) {
for(j = 0; j < vertices; j++) {
printf("%d ", adjMatrix[i][j]);
}
printf("\n");
}
return 0;
}
Output :
b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement functions to print indegree, outdegree and total degree of all
vertices of graph.
Ans :
#include <stdio.h>
void printIndegree(int adjMatrix[][10], int vertices) {
int i, j;
printf("Indegree of vertices:\n");
for(i = 0; i < vertices; i++) {
int indegree = 0;
for(j = 0; j < vertices; j++) {
indegree += adjMatrix[j][i];
}
printf("Vertex %d: %d\n", i, indegree);
}
}
int main() {
int vertices, edges, i, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int adjMatrix[vertices][vertices];
printIndegree(adjMatrix, vertices);
printOutdegree(adjMatrix, vertices);
printTotalDegree(adjMatrix, vertices);
return 0;
}
Output :
Set-B
a) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement function to traverse the graph using Breadth First Search (BFS)
traversal.
Ans :
#include <stdio.h>
#include <stdlib.h>
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int adjMatrix[vertices][vertices];
return 0;
}
Output :
b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement function to traverse the graph using Depth First Search (BFS)
traversal.
Ans:
#include <stdio.h>
void DFS(int adjMatrix[][10], int vertices, int start, int visited[]) {
printf("%d ", start);
visited[start] = 1;
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int adjMatrix[vertices][vertices];
int visited[vertices];
for(int i = 0; i < vertices; i++) {
visited[i] = 0;
}
return 0;
}
Output :
Assignment No. 04
Set-A
a) Write a C program that accepts the vertices and edges of a graph. Create adjacency
list and display the adjacency list.
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
printAdjList(adjList, vertices);
return 0;
}
Output :
b) Write a C program that accepts the vertices and edges of a graph. Create adjacency list.
Implement functions to print indegree, outdegree and total degree of all vertex of graph.
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
void addEdge(struct Node* adjList[], int u, int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = u;
newNode->next = adjList[v];
adjList[v] = newNode;
}
void printIndegree(struct Node* adjList[], int vertices) {
int indegree[vertices];
for (int i = 0; i < vertices; i++) indegree[i] = 0;
for (int i = 0; i < vertices; i++) {
struct Node* temp = adjList[i];
while (temp != NULL) {
indegree[temp->vertex]++;
temp = temp->next;
}
}
printf("Indegree of vertices:\n");
for (int i = 0; i < vertices; i++) {
printf("Vertex %d: %d\n", i, indegree[i]);
}
}
void printOutdegree(struct Node* adjList[], int vertices) {
int outdegree[vertices];
for (int i = 0; i < vertices; i++) outdegree[i] = 0;
for (int i = 0; i < vertices; i++) {
struct Node* temp = adjList[i];
while (temp != NULL) {
outdegree[i]++;
temp = temp->next;
}
}
printf("Outdegree of vertices:\n");
for (int i = 0; i < vertices; i++) {
printf("Vertex %d: %d\n", i, outdegree[i]);
}
}
void printTotalDegree(struct Node* adjList[], int vertices) {
int totalDegree[vertices];
for (int i = 0; i < vertices; i++) totalDegree[i] = 0;
for (int i = 0; i < vertices; i++) {
struct Node* temp = adjList[i];
while (temp != NULL) {
totalDegree[i]++;
totalDegree[temp->vertex]++;
temp = temp->next;
}
}
printf("Total degree of vertices:\n");
for (int i = 0; i < vertices; i++) {
printf("Vertex %d: %d\n", i, totalDegree[i]);
}
}
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
struct Node* adjList[vertices];
for (int i = 0; i < vertices; i++) adjList[i] = NULL;
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (int i = 0; i < edges; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}
printIndegree(adjList, vertices);
printOutdegree(adjList, vertices);
printTotalDegree(adjList, vertices);
return 0;
}
Output :
Set-B
a) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency list. Implement function to traverse the graph using Breadth First Search (BFS)
traversal.
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
void addEdge(struct Node* adjList[], int u, int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = u;
newNode->next = adjList[v];
adjList[v] = newNode;
}
void BFS(struct Node* adjList[], int vertices, int start) {
int visited[vertices];
for (int i = 0; i < vertices; i++) visited[i] = 0;
int queue[vertices], front = 0, rear = 0;
queue[rear++] = start;
visited[start] = 1;
while (front < rear) {
int current = queue[front++];
printf("%d ", current);
struct Node* temp = adjList[current];
while (temp != NULL) {
if (!visited[temp->vertex]) {
queue[rear++] = temp->vertex;
visited[temp->vertex] = 1;
}
temp = temp->next;
}
}
printf("\n");
}
int main()
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
struct Node* adjList[vertices];
for (int i = 0; i < vertices; i++) adjList[i] = NULL
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (int i = 0; i < edges; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}
printf("BFS Traversal starting from vertex 0:\n");
BFS(adjList, vertices, 0);
return 0;
}
Output :
b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency list. Implement function to traverse the graph using Depth First Search (BFS)
traversal.
Ans:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
void addEdge(struct Node* adjList[], int u, int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = u;
newNode->next = adjList[v];
adjList[v] = newNode;
}
void DFS(struct Node* adjList[], int vertices, int start, int visited[]) {
visited[start] = 1;
printf("%d ", start);
struct Node* temp = adjList[start];
while (temp != NULL) {
if (!visited[temp->vertex]) {
DFS(adjList, vertices, temp->vertex, visited);
}
temp = temp->next;
}
}
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
struct Node* adjList[vertices];
for (int i = 0; i < vertices; i++) adjList[i] = NULL;
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (int i = 0; i < edges; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}
int visited[vertices];
for (int i = 0; i < vertices; i++) visited[i] = 0;
printf("DFS Traversal starting from vertex 0:\n");
DFS(adjList, vertices, 0, visited);
printf("\n");
return 0;
}
Output :
Assignment No. 05
Set-A
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
void DFS(struct Node* adjList[], int vertex, int visited[], int stack[], int* top) {
visited[vertex] = 1;
stack[++(*top)] = vertex;
}
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
topologicalSort(adjList, vertices);
return 0;
}
Output :
b)Write a C program for the Implementation of Prim's Minimum spanning tree algorithm.
Ans :
#include <stdio.h>
#include <limits.h>
#define MAX 10
int minKey(int key[], int visited[], int vertices) {
int min = INT_MAX, minIndex;
for (int i = 0; i < vertices; i++) {
if (visited[i] == 0 && key[i] < min) {
min = key[i];
minIndex = i;
}
}
return minIndex;
}
key[0] = 0;
parent[0] = -1;
printf("Edge Weight\n");
for (int i = 1; i < vertices; i++) {
printf("%d - %d %d\n", parent[i], i, graph[i][parent[i]]);
}
}
int main() {
int vertices, graph[MAX][MAX];
Output :
Set-B
#include <stdio.h>
#include <stdlib.h>
struct Edge {
int u, v, weight;
};
printf("Edge Weight\n");
for (int i = 0; i < edgesCount; i++) {
int u = edges[i].u;
int v = edges[i].v;
int setU = find(parent, u);
int setV = find(parent, v);
if (setU != setV) {
printf("%d - %d %d\n", u, v, edges[i].weight);
unionSet(parent, setU, setV);
}
}
}
int main() {
int vertices, edgesCount;
printf("Enter number of vertices: ");
scanf("%d", &vertices);
return 0;
}
Output :
Assignment No. 06
Set - A
a)Write a C program for the implementation of Dijkstra's shortest path algorithm for finding
shortest path from a given source vertex using adjacency cost matrix.
Ans :
#include <stdio.h>
#include <limits.h>
#define MAX 10
int minDistance(int dist[], int sptSet[], int vertices) {
int min = INT_MAX, minIndex;
for (int i = 0; i < vertices; i++) {
if (sptSet[i] == 0 && dist[i] < min) {
min = dist[i];
minIndex = i;
}
}
return minIndex;
}
void dijkstra(int graph[MAX][MAX], int vertices, int src) {
int dist[vertices], sptSet[vertices];
for (int i = 0; i < vertices; i++) {
dist[i] = INT_MAX;
sptSet[i] = 0;
}
dist[src] = 0;
for (int count = 0; count < vertices - 1; count++) {
int u = minDistance(dist, sptSet, vertices);
sptSet[u] = 1;
return 0;
}
Output :
Set - B
a)Write a C program for the implementation of Floyd Warshall's algorithm for finding all
pairs shortest path using adjacency cost matrix.
Ans :
#include <stdio.h>
#include <limits.h>
#define MAX 10
a) Write a program to implement various types of hash functions which are used to place the
data in a hash table
a. Division Method
b. Mid Square Method
c. Digit Folding Method
Accept n values from the user and display appropriate message in case of collision for each of
the above functions.
Ans :
#include <stdio.h>
#include <math.h>
#define SIZE 10
void insert(int hashTable[], int key, int (*hashFunction)(int), const char *method) {
int index = hashFunction(key);
if (hashTable[index] != -1) {
printf("Collision occurred at index %d for key %d using %s method\n", index, key, method);
} else {
hashTable[index] = key;
printf("Key %d inserted at index %d using %s method\n", key, index, method);
}
}
int main() {
int hashTable[SIZE];
for (int i = 0; i < SIZE; i++) {
hashTable[i] = -1;
}
int n;
printf("Enter number of values to insert: ");
scanf("%d", &n);
if (strcmp(method, "division") == 0) {
insert(hashTable, key, divisionMethod, "division");
} else if (strcmp(method, "mid_square") == 0) {
insert(hashTable, key, midSquareMethod, "mid_square");
} else if (strcmp(method, "digit_folding") == 0) {
insert(hashTable, key, digitFoldingMethod, "digit_folding");
} else {
printf("Invalid method selected\n");
}
}
return 0;
}
Output :
Set - B
a) Write a menu driven program to implement hash table using array (insert, search, delete,
display). Use any of the above-mentioned hash functions. In case of collision apply linear
probing.
Ans :
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
void display() {
printf("Hash Table: ");
for (int i = 0; i < SIZE; i++) {
if (hashTable[i] != -1)
printf("[%d] ", hashTable[i]);
else
printf("[ ] ");
}
printf("\n");
}
int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = -1;
}
int choice, key;
do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\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(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;
}
Output :
b)Write a menu driven program to implement hash table using array (insert, search, delete,
display). Use any of the above-mentioned hash functions. In case of collision apply quadratic
probing.
Ans :
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
void display() {
printf("Hash Table: ");
for (int i = 0; i < SIZE; i++) {
if (hashTable[i] != -1)
printf("[%d] ", hashTable[i]);
else
printf("[ ] ");
}
printf("\n");
}
int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = -1;
}
int choice, key;
do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\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(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;
}
Output:
Assignment No. 08
Set-A
a)Implement hash table using singly linked lists. Write a menu driven program to perform
operations on the hash table (insert, search, delete, display). Select appropriate hashing
function. In case of collision, use separate chaining.
Ans :
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct Node {
int key;
struct Node* next;
};
switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;
}
Output :
set_B
a)Implement hash table using doubly linked lists. Write a menu driven program to perform
operations on the hash table (insert, search, delete, display). Select appropriate hashing
function. In case of collision, use separate chaining.
Ans :
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct Node {
int key;
struct Node* prev;
struct Node* next;
};
void display() {
for (int i = 0; i < SIZE; i++) {
printf("Index %d: ", i);
struct Node* temp = hashTable[i];
while (temp != NULL) {
printf("%d <-> ", temp->key);
temp = temp->next;
}
printf("NULL\n");
}
}
int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = NULL;
}
do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\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(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;
}
Output :