1 DSA Slip Answer
1 DSA Slip Answer
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations - create,
insert, inorder. Write a menu driven program that performs the above operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
if (!newNode) {
return NULL;
newNode->data = data;
return newNode;
return createNode(data);
return root;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
int main() {
while (1) {
printf("1. Insert\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
inorder(root);
printf("\n");
break;
case 3:
freeTree(root);
printf("Exiting program...\n");
return 0;
default:
return 0;
Q.2) Write a C program that accepts the vertices and edges of a graph and stores it as an
adjacency matrix. Display the adjacency matrix. Implement functions to print indegree
of all vertices of graph.
->
#include <stdio.h>
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createAdjMatrix(adj, vertices, edges);
printf("\nAdjacency Matrix:\n");
displayAdjMatrix(adj, vertices);
printIndegree(adj, vertices);
return 0;
int i, j;
printf("\n");
}
if (adj[i][j] == 1) {
indegree[j]++;
}
Slip 2
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations - create,
search, preorder. Write a menu driven program that performs the above operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
scanf("%d", &value);
if (search(root, value))
else
break;
case 3:
preorder(root);
printf("\n");
break;
case 4:
printf("Exiting program...\n");
break;
default:
return 0;
}
void menu() {
printf("1. Insert\n");
printf("2. Search\n");
printf("4. Exit\n");
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
Q.2) Implementation of Dijkstra's shortest path algorithm for finding Shortest Path from
a given source vertex using adjacency cost matrix.
->
#include <stdio.h>
#include <limits.h>
int main() {
int vertices, i, j;
int graph[MAX][MAX];
scanf("%d", &vertices);
scanf("%d", &graph[i][j]);
if (graph[i][j] == 99999) {
graph[i][j] = INF;
int src;
scanf("%d", &src);
return 0;
dist[i] = INF;
visited[i] = 0;
dist[src] = 0;
visited[u] = 1;
if (!visited[v] && graph[u][v] != INF && dist[u] != INF && dist[u] + graph[u][v] <
dist[v]) {
if (dist[i] == INF)
else
}
int minDistance(int dist[], int visited[], int vertices) {
return min_index;
}
Slip 3
Q1) Implementation of static hash table with Linear Probing.
->
#include <stdio.h>
#define SIZE 10
#define EMPTY -1
#define DELETED -2
int hashTable[SIZE];
void initHashTable() {
hashTable[i] = EMPTY;
if (index == originalIndex) {
hashTable[index] = key;
if (hashTable[index] == key) {
return index;
if (index == originalIndex) {
break;
return -1;
void display() {
printf("\nHash Table:\n");
if (hashTable[i] == EMPTY) {
} else {
int main() {
initHashTable();
do {
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
if (index != -1)
else
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
return 0;
Q.2) C program to implement graph traversal method using depth first search
->
#include <stdio.h>
int adj[MAX][MAX];
int visited[MAX];
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
scanf("%d", &startVertex);
visited[i] = 0;
DFS(startVertex, vertices);
printf("\n");
return 0;
}
adj[i][j] = 0;
adj[src][dest] = 1;
visited[vertex] = 1;
DFS(i, vertices);
}
}
Slip 4
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations-create,
search, preorder. Write a menu driven program that performs the above operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
scanf("%d", &value);
if (search(root, value))
else
break;
case 3:
preorder(root);
printf("\n");
break;
case 4:
printf("Exiting program...\n");
break;
default:
return 0;
}
void menu() {
printf("1. Insert\n");
printf("2. Search\n");
printf("4. Exit\n");
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
Q.2) Write C Program that accept the vertices and edges of a graph and store it is an
adjacency Matrix
->
#include <stdio.h>
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
displayGraph(adj, vertices);
return 0;
adj[src][dest] = 1;
printf("\nAdjacency Matrix:\n");
printf("\n");
}
Slip 5
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations-create,
insert, postorder, Write a menu driven program that performs the above operations
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
postorder(root);
printf("\n");
break;
case 3:
printf("Exiting program...\n");
break;
default:
return 0;
void menu() {
printf("1. Insert\n");
printf("3. Exit\n");
}
// Function to create and insert a node into BST
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
if (root) {
postorder(root->left);
postorder(root->right);
Q.2) 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.
->
#include <stdio.h>
#include <stdlib.h>
int adj[MAX][MAX];
int visited[MAX];
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
scanf("%d", &startVertex);
printf("\n");
return 0;
adj[i][j] = 0;
adj[src][dest] = 1;
printf("\nAdjacency Matrix:\n");
printf("\n");
visited[i] = 0;
visited[startVertex] = 1;
queue[rear++] = startVertex;
queue[rear++] = i;
visited[i] = 1;
}
Slip 6
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations create,
preorder and postorder. Write a menu driven program that performs the above
operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
postorder(root);
printf("\n");
break;
case 4:
printf("Exiting program...\n");
break;
default:
return 0;
}
void menu() {
printf("1. Insert\n");
printf("4. Exit\n");
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
if (root) {
postorder(root->left);
postorder(root->right);
Output
1. Insert
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Preorder Traversal: 50 30 20 40 70 60 80
Postorder Traversal: 20 40 30 60 80 70 50
Exiting program...
Q.2) Write a C program that accepts the vertices and edges of a graph. Create
adjacency list and display the adjacency list.
->
#include <stdio.h>
#include <stdlib.h>
// Structure for an adjacency list node
int vertex;
} Node;
int numVertices;
Node** adjLists;
} Graph;
// Function prototypes
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
printf("Enter edges (source destination):\n");
displayGraph(graph);
return 0;
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
return graph;
newNode->next = NULL;
return newNode;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
printf("\nAdjacency List:\n");
while (temp) {
temp = temp->next;
printf("\n");
}
}
Output
01
04
12
13
14
34
Adjacency List:
Vertex 2: -> 1
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
scanf("%d", &key);
else
break;
case 3:
preorder(root);
printf("\n");
break;
case 4:
printf("Exiting program...\n");
break;
default:
void menu() {
printf("1. Insert\n");
printf("2. Search\n");
printf("4. Exit\n");
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
Q.2) 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
(DFS) traversal. [15]
->
#include <stdio.h>
#include <stdlib.h>
int adj[MAX][MAX];
int visited[MAX];
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
scanf("%d", &startVertex);
visited[i] = 0;
DFS(startVertex, vertices);
printf("\n");
return 0;
}
void createGraph(int vertices, int edges) {
adj[i][j] = 0;
adj[src][dest] = 1;
printf("\nAdjacency Matrix:\n");
printf("\n");
visited[vertex] = 1;
DFS(i, vertices);
}
Slip 8
Q.1) C program to implement graph as adjacency matrix.
->
#include <stdio.h>
#include <stdlib.h>
int adj[MAX][MAX];
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
return 0;
adj[i][j] = 0;
adj[src][dest] = 1;
printf("\nAdjacency Matrix:\n");
printf("\n");
->
#include <stdio.h>
#include <stdlib.h>
int hashTable[TABLE_SIZE];
// Function prototypes
void initializeTable();
void display();
int main() {
do {
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &key);
insert(key);
break;
case 2:
scanf("%d", &key);
result = search(key);
if (result != -1)
else
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
return 0;
}
// Function to initialize the hash table
void initializeTable() {
return;
hashTable[index] = key;
}
// Function to search for a key in the hash table
if (hashTable[index] == key)
break;
void display() {
printf("\nHash Table:\n");
if (hashTable[i] == -1)
else
}
Slip 9
Q.1) C program to implement graph traversal method using depth first search.
->
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
printf("Enter the starting vertex for DFS: ");
scanf("%d", &startVertex);
DFS(startVertex, vertices);
printf("\n");
return 0;
adj[i][j] = 0;
adj[src][dest] = 1;
adj[dest][src] = 1; // For undirected graph
printf("\nAdjacency Matrix:\n");
printf("\n");
visited[vertex] = 1;
DFS(i, vertices);
}
Q.2) C program to implement BST to perform following operations on BST-
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
break;
case 3:
printf("Exiting program...\n");
break;
default:
return 0;
void menu() {
printf("1. Insert\n");
printf("3. Exit\n");
newNode->data = data;
return newNode;
return root;
if (root == NULL)
return 0;
return 1;
}
Slip 10
Q.1) Write a C program to implement graph traversal method using depth first search.
->
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
printf("Enter the starting vertex for DFS: ");
scanf("%d", &startVertex);
DFS(startVertex, vertices);
printf("\n");
return 0;
adj[i][j] = 0;
adj[src][dest] = 1;
adj[dest][src] = 1; // For undirected graph
printf("\nAdjacency Matrix:\n");
printf("\n");
visited[vertex] = 1;
DFS(i, vertices);
}
Q.2) 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 of all vertices of graph.
->
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
printIndegree(vertices);
return 0;
adj[i][j] = 0;
printf("\nAdjacency Matrix:\n");
printf("\n");
indegree[i]++;
printf("\nIn-degree of vertices:\n");
}
Slip 11
*Q.1)* 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
(DFS) traversal.
->
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
scanf("%d", &startVertex);
DFS(startVertex, vertices);
printf("\n");
return 0;
adj[i][j] = 0;
adj[src][dest] = 1;
printf("\nAdjacency Matrix:\n");
printf("\n");
visited[vertex] = 1;
DFS(i, vertices);
}
}
*Q.2)* Implement a Binary search tree (BST) library (btree.h) with operations – create,
preorder. Write a menu-driven program that performs the above operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
printf("Exiting program...\n");
break;
default:
return 0;
void menu() {
printf("1. Insert\n");
printf("3. Exit\n");
}
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
}
Slip 12
Q.1)* Write a C program which uses Binary search tree library and implements the
following function with recursion:
int compare(T1, T2) – compares two binary search trees and returns 1 if they are equal
and 0 otherwise
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
switch (choice) {
case 1:
scanf("%d", &treeChoice);
scanf("%d", &value);
if (treeChoice == 1)
T1 = insert(T1, value);
else if (treeChoice == 2)
T2 = insert(T2, value);
else
break;
case 2:
preorder(T1);
printf("\n");
preorder(T2);
printf("\n");
break;
case 3:
if (compare(T1, T2))
printf("The trees are equal.\n");
else
break;
case 4:
printf("Exiting program...\n");
break;
default:
return 0;
void menu() {
printf("4. Exit\n");
newNode->data = data;
return newNode;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
return 1;
return 0;
return (T1->data == T2->data) &&
compare(T1->right, T2->right);
}
Slip 12
*Q.1)* 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 (DFS) traversal.
->
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
scanf("%d", &startVertex);
DFS(startVertex, vertices);
printf("\n");
return 0;
adj[i][j] = 0;
printf("\nAdjacency Matrix:\n");
printf("\n");
visited[vertex] = 1;
DFS(i, vertices);
}
Q.2)* Implement a Binary search tree (BST) library (btree.h) with operations – create,
preorder. Write a menu-driven program that performs the above operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function Prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
printf("Exiting program...\n");
break;
default:
return 0;
void menu() {
printf("1. Insert\n");
printf("3. Exit\n");
}
// Function to create and insert a node into BST
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
}
Slip 13
Q.1)* Write a C program which uses Binary search tree library and implements the
following function with recursion:
int compare(T1, T2) – compares two binary search trees and returns 1 if they are equal
and 0 otherwise
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function Prototypes
void menu();
int main() {
do {
menu();
switch (choice) {
case 1:
scanf("%d", &value);
T1 = insert(T1, value);
break;
case 2:
scanf("%d", &value);
T2 = insert(T2, value);
break;
case 3:
preorder(T1);
printf("\n");
break;
case 4:
preorder(T2);
printf("\n");
break;
case 5:
if (compare(T1, T2))
else
case 6:
printf("Exiting program...\n");
break;
default:
return 0;
void menu() {
printf("6. Exit\n");
if (root == NULL) {
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
return root;
if (root) {
preorder(root->left);
preorder(root->right);
compare(T1->right, T2->right);
Q.2)* Write a C program that accepts the vertices and edges of a graph and stores it as
an adjacency matrix. Display the adjacency matri
->
#include <stdio.h>
// Function prototypes
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
displayGraph(vertices);
return 0;
adjMatrix[i][j] = 0;
}
// Function to display the adjacency matrix
printf("\nAdjacency Matrix:\n");
printf("\n");
}
Slip 14
Q.1)* Write a C program that accepts the vertices and edges of a graph. Create
adjacency list and display the adjacency list
->
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
int numVertices;
Node** adjLists;
} Graph;
// Function prototypes
int main() {
scanf("%d", &vertices);
// Create graph
scanf("%d", &edges);
displayGraph(graph);
return 0;
graph->numVertices = vertices;
graph->adjLists = (Node**)malloc(vertices * sizeof(Node*));
graph->adjLists[i] = NULL;
return graph;
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
printf("\nAdjacency List:\n");
while (temp) {
temp = temp->next;
printf("\n");
Q.2)* Write a program which uses binary search tree library and counts the total nodes
in the tree. int count(T) – returns the total number of nodes from BST
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
case 4:
printf("Exiting program...\n");
break;
default:
return 0;
void menu() {
printf("1. Insert\n");
printf("4. Exit\n");
if (root == NULL) {
newNode->data = data;
return newNode;
}
if (data < root->data)
return root;
if (root) {
preorder(root->left);
preorder(root->right);
if (root == NULL)
return 0;
}
Slip 15
Q.1)* Implementation of static hash table with Linear Probing.
->
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
void initialize() {
hashTable[i] = -1;
// Hash function
int i = 0;
i++;
}
if (i < SIZE) {
} else {
// Search key
int i = 0;
i++;
else
void display() {
printf("\nHash Table:\n");
else
// Menu
void menu() {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
int main() {
initialize();
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
insert(key);
break;
case 2:
scanf("%d", &key);
search(key);
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
return 0;
Q.2)* Write a program which uses binary search tree library and counts the total nodes
in the tree. int count(T) – returns the total number of nodes from BST.
->
#include <stdio.h>
#include <stdlib.h>
// Structure for a BST node
int data;
} Node;
// Function prototypes
void menu();
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
break;
case 4:
printf("Exiting program...\n");
break;
default:
return 0;
// Menu display
void menu() {
printf("1. Insert\n");
printf("4. Exit\n");
}
// Insert function for BST
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
// Preorder traversal
if (root) {
preorder(root->left);
preorder(root->right);
if (root == NULL)
return 0;
return 1 + count(root->left) + count(root->right);
}
Slip 16
*Q.1)* C program to implement graph traversal method using Breadth First Search
->
#include <stdio.h>
#include <stdlib.h>
int adj[MAX][MAX];
int visited[MAX];
// Function Prototypes
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
printf("Enter the starting vertex for BFS: ");
scanf("%d", &startVertex);
BFS(startVertex, vertices);
printf("\n");
return 0;
adj[i][j] = 0;
// Input edges
adj[src][dest] = 1;
}
// Function to display adjacency matrix
printf("\nAdjacency Matrix:\n");
printf("\n");
visited[i] = 0;
visited[startVertex] = 1;
queue[rear++] = startVertex;
visited[i] = 1;
Q.2)* Implement a Binary Search Tree (BST) library (btree.h) with operations – create,
preorder. Write a menu-driven program that performs the above operations
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
if (root == NULL) {
return createNode(data);
return root;
// Preorder Traversal
if (root != NULL) {
preorder(root->left);
preorder(root->right);
do {
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
printf("Exiting program.\n");
break;
default:
return 0;
}
Slip 17
*Q.1)* Write a program which uses binary search tree library and counts the total
leaf nodes in the tree. int countLeaf(T) – returns the total number of leaf nodes
from BST
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = data;
return newNode;
if (root == NULL)
return createNode(data);
if (data < root->data)
return root;
if (root == NULL)
return 0;
return 1;
if (root != NULL) {
preorder(root->left);
preorder(root->right);
}
int main() {
do {
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
break;
case 4:
printf("Exiting program.\n");
break;
default:
return 0;
*Q.2)* Write a C program that accepts the vertices and edges of a graph and
stores it as an adjacency matrix. Display the adjacency matrix.
->
#include <stdio.h>
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayGraph(vertices);
return 0;
// Initialize matrix to 0
adj[i][j] = 0;
}
printf("Enter edges (source destination):\n");
adj[src][dest] = 1;
printf("\nAdjacency Matrix:\n");
printf("\n");
*Q.3)* Write a C program to traverse the graph using Depth First Search (DFS).
->
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayAdjMatrix(vertices);
visited[i] = 0;
DFS(startVertex, vertices);
printf("\n");
return 0;
// Initialize matrix to 0
adj[i][j] = 0;
adj[src][dest] = 1;
}
}
visited[vertex] = 1;
DFS(i, vertices);
printf("\nAdjacency Matrix:\n");
printf("\n");
}
Slip 18
Q.1)* Write a program which uses binary search tree library and counts the total
leaf nodes in the tree. int countLeaf(T) - returns the total number of leaf nodes
from BST
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
// Function Prototypes
void menu();
int main() {
do {
menu();
switch(choice) {
case 1:
scanf("%d", &value);
break;
case 2:
inorder(root);
printf("\n");
break;
case 3:
break;
case 4:
printf("Exiting program.\n");
break;
default:
} while(choice != 4);
return 0;
}
// Menu Function
void menu() {
printf("1. Insert\n");
printf("4. Exit\n");
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
}
// Function to perform inorder traversal
if (root != NULL) {
inorder(root->left);
inorder(root->right);
if (root == NULL)
return 0;
return 1;
Q.2)* Write a C program that accepts the vertices and edges of a graph and
stores it as an adjacency matrix. Implement functions to print the indegree of all
vertices of the graph.
->
#include <stdio.h>
// Initialize matrix to 0
adj[i][j] = 0;
printf("\nAdjacency Matrix:\n");
printf("\n");
int indegree = 0;
if (adj[row][col] == 1)
indegree++;
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayMatrix(vertices);
computeIndegree(vertices);
return 0;
}
Slip 19
Q.1)* Write a C program that accepts the vertices and edges of a graph. Create
adjacency list and display the adjacency list
->
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
int numVertices;
} Graph;
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
// Function to create a graph with a given number of vertices
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
printf("\nAdjacency List:\n");
while (temp) {
temp = temp->next;
printf("\n");
// Main function
int main() {
scanf("%d", &vertices);
displayGraph(graph);
return 0;
*Q.2)* Implement a Binary search tree (BST) library (btree.h) with operations –
create, preorder. Write a menu-driven program that performs the above
operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
newNode->data = value;
return newNode;
if (root == NULL) {
return createNode(value);
return root;
preorder(root->left);
preorder(root->right);
void showMenu() {
printf("3. Exit\n");
// Main function
int main() {
do {
showMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
printf("Exiting program...\n");
break;
default:
return 0;
}
Slip 20
Q.1)* Implement a Binary search tree (BST) library (btree.h) with operations –
create, insert, inorder. Write a menu-driven program that performs the above
operations
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = value;
return newNode;
if (root == NULL) {
return createNode(value);
}
return root;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
void showMenu() {
printf("3. Exit\n");
// Main program
int main() {
do {
showMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
inorder(root);
printf("\n");
break;
case 3:
printf("Exiting program...\n");
break;
default:
return 0;
Q.2)* Write a C program that accepts the vertices and edges of a graph and
stores it as an adjacency matrix. Display the adjacency matrix. Implement
functions to print indegree of all vertices of the graph
->
#include <stdio.h>
// Initialize matrix to 0
for (i = 0; i < vertices; i++) {
adj[i][j] = 0;
printf("\nAdjacency Matrix:\n");
printf("\n");
indegree[i] = 0; // Initialize
if (adj[j][i] == 1) {
indegree[i]++;
printf("\nIndegree of vertices:\n");
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayMatrix(vertices);
calculateIndegree(vertices);
return 0;
}
Slip 21
Q.1)* C program to implement graph traversal method using depth first search.
->
#include <stdio.h>
#include <stdlib.h>
adj[i][j] = 0;
visited[v] = 1;
DFS(i, vertices);
printf("\nAdjacency Matrix:\n");
printf("\n");
}
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
createGraph(vertices, edges);
displayMatrix(vertices);
visited[i] = 0;
scanf("%d", &start);
DFS(start, vertices);
printf("\n");
return 0;
}
Q.2)* Implementation of Dijkstra’s shortest path algorithm for finding the shortest
path from a given source vertex using an adjacency cost matrix.
->
#include <stdio.h>
#include <limits.h>
// Initialize distances
distance[i] = graph[start][i];
visited[start] = 1;
distance[start] = 0;
min = INF;
min = distance[i];
nextNode = i;
}
visited[nextNode] = 1;
int main() {
scanf("%d", &n);
scanf("%d", &graph[i][j]);
scanf("%d", &source);
dijkstra(graph, n, source);
return 0;
}
Slip 22
Q.1)* Implement a Binary Search Tree (BST) library (btree.h) with operations –
create, insert, postorder. Write a menu-driven program that performs the above
operations.
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = data;
return newNode;
// Insert function
if (root == NULL) {
return createNode(data);
}
if (data < root->data)
return root;
// Postorder Traversal
if (root != NULL) {
postorder(root->left);
postorder(root->right);
// Menu
void menu() {
printf("3. Exit\n");
// Main Function
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
postorder(root);
printf("\n");
break;
case 3:
printf("Exiting program.\n");
break;
default:
->
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
void initialize() {
hashTable[i] = -1;
// Hash function
if (index == startIndex) {
return;
hashTable[index] = key;
void display() {
printf("\nHash Table:\n");
if (hashTable[i] != -1)
else
// Menu
void menu() {
printf("\n=== Hash Table Menu ===\n");
printf("1. Insert\n");
printf("2. Display\n");
printf("3. Exit\n");
// Main function
int main() {
initialize();
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &key);
insert(key);
break;
case 2:
display();
break;
case 3:
printf("Exiting program.\n");
break;
default:
return 0;
}
Slip 23
Q.1)* C program to implement graph as adjacency List.
->
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
int numVertices;
Node** adjLists;
} Graph;
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
// Function to create a graph
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// Function to display the adjacency list
while (temp) {
temp = temp->next;
printf("\n");
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
displayGraph(graph);
return 0;
a) Create
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = value;
return newNode;
if (root == NULL) {
return createNode(value);
return root;
if (root == NULL)
return 0;
}
// Function to display inorder traversal
if (root != NULL) {
inorder(root->left);
inorder(root->right);
void menu() {
printf("4. Exit\n");
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
inorder(root);
printf("\n");
break;
case 3:
break;
case 4:
printf("Exiting program.\n");
break;
default:
return 0;
}
Slip 24
Q.1)* Implement a Binary Search Tree (BST) library (btree.h) with operations –
create, insert, postorder. Write a menu-driven program that performs the above
operations
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = value;
return newNode;
if (root == NULL)
return createNode(value);
if (value < root->data)
return root;
if (root != NULL) {
postorder(root->left);
postorder(root->right);
void menu() {
printf("1. Insert\n");
printf("3. Exit\n");
int main() {
Node* root = NULL;
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
postorder(root);
printf("\n");
break;
case 3:
printf("Exiting program.\n");
break;
default:
}
} while (choice != 3);
return 0;
Q.2)* Write a C program that accepts the vertices and edges of a graph and store
it as an adjacency matrix. Implement a function to traverse the graph using
Breadth First Search (BFS) traversal.
->
#include <stdio.h>
#include <stdlib.h>
int adjMatrix[MAX][MAX];
int visited[MAX];
int queue[MAX];
int numVertices;
adjMatrix[start][end] = 1;
// Queue operations
void enqueue(int vertex) {
if (rear == MAX - 1) {
printf("Queue overflow\n");
return;
if (front == -1)
front = 0;
queue[++rear] = vertex;
int dequeue() {
return -1;
return queue[front++];
// BFS traversal
visited[i] = 0;
enqueue(startVertex);
visited[startVertex] = 1;
enqueue(i);
visited[i] = 1;
printf("\n");
int main() {
scanf("%d", &numVertices);
adjMatrix[i][j] = 0;
addEdge(src, dest);
scanf("%d", &startVertex);
bfs(startVertex);
return 0;
}
Slip 25
*Q.1* Implement a Binary Search Tree (BST) library (btree.h) with operations –
create, preorder, and postorder. Write a menu-driven program that performs the
above operations
->
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = value;
return newNode;
if (root == NULL)
return createNode(value);
return root;
if (root) {
preorder(root->left);
preorder(root->right);
if (root) {
postorder(root->left);
postorder(root->right);
}
// Menu
void menu() {
printf("4. Exit\n");
printf("------------------------------------\n");
int main() {
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
preorder(root);
printf("\n");
break;
case 3:
postorder(root);
printf("\n");
break;
case 4:
printf("Exiting program...\n");
break;
default:
return 0;
->
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
void initHashTable() {
hashTable[i] = -1;
// Hash function
if (index == startIndex) {
return;
}
hashTable[index] = key;
// Search key
if (hashTable[index] == key)
return index;
if (index == startIndex)
break;
return -1;
void display() {
printf("\nHash Table:\n");
if (hashTable[i] != -1)
printf("Index %d: %d\n", i, hashTable[i]);
else
int main() {
initHashTable();
do {
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
if (result != -1)
else
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
return 0;