End Sem Exam (DS Code)
End Sem Exam (DS Code)
DFS:
#include <stdio.h>
#include <stdlib.h>
#define Max 10
struct Graph {
int numVertices;
int adjMatrix[Max][Max];
int Visited[Max];
};
int main() {
int numVertices, numEdges, src, dest, startVertex;
BFS:
#include <stdio.h>
#include <stdlib.h>
#define Max 10
struct Graph{
int numVertices;
int adjMatric[Max][Max];
int Visited[Max];
};
graph->numVertices=numVertices;
for(int i=0;i<numVertices;i++){
for (int j=0; j<numVertices;j++){
graph->adjMatric[i][j]=0;
}
graph->Visited[i]=0;
}
return graph;
}
void addEdges(struct Graph* graph,src,dest){
graph->adjMatric[src][dest]=1;
graph->adjMatric[dest][src]=1;
}
while(front<rear){
int vertex=queue[front++];
printf("%d",vertex + 1);
for(int i=0;i<graph->numVertices;i++){
if(graph->adjMatric[vertex][i]==1 && !graph->Visited){
queue[rear++]= i;
Visited[i]=1;
}
}
}
}
int main()
{
int numVertices,numEdges, src, dest, startVertex;
for(int i=0;i<numEdges;i++){
addEdges(graph,src -1,dest-1);
BFS(graph,startVertex-1);
free(graph);
return 0;
}
Quadratic Probing
#include <stdio.h>
#define TABLE_SIZE 10
int hashTable[TABLE_SIZE];
// Function to initialize the hash table
void initializeTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable[i] = -1; // Use -1 to represent empty slots
}
}
// Function to calculate the hash
int hash(int key) {
return key % TABLE_SIZE;
}
// Function to insert a key using quadratic probing
void insert(int key) {
int index = hash(key);
int i = 0;
int main() {
initializeTable();
int choice, key;
while (1) {
printf("\n1. Insert\n2. Display\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
display();
break;
case 3:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Please enter 1, 2, or 3.\n");
}
}
return 0;
}
Stack Using Linked List
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
void push(int value);
void pop();
void peek();
void isEmpty();
void display();
// Function prototypes
void enqueue(int value);
void dequeue();
void peek();
void isEmpty();
void display();
// Function prototypes
void push(int value);
void pop();
void peek();
void isEmpty();
void display();
// Function prototypes
void enqueue(int value);
void dequeue();
void peek();
void isEmpty();
void display();
struct Node {
int data;
struct Node* left;
struct Node* right;
};
do {
printf("\n-----------Main Menu------------\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Inorder Traversal\n");
printf("4. Preorder Traversal\n");
printf("5. Postorder Traversal\n");
printf("6. Delete\n");
printf("7. Exit\n");
printf("Enter your Choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the element to be Inserted: ");
scanf("%d", &value);
root = insert(root, value);
printf("%d is inserted into BST.\n", value);
break;
case 2:
printf("Enter value to be searched: ");
scanf("%d", &key);
if(search(root, key)) {
printf("%d found in the BST.\n", key);
} else {
printf("%d not found in the BST.\n", key);
}
break;
case 3:
printf("Inorder Traversal of the BST: ");
inorder(root);
printf("\n");
break;
case 4:
printf("Preorder Traversal of the BST: ");
preorder(root);
printf("\n");
break;
case 5:
printf("Postorder Traversal of the BST: ");
postorder(root);
printf("\n");
break;
case 6:
printf("Enter value to be deleted: ");
scanf("%d", &key);
root = deleteNode(root, key);
printf("%d is deleted from the BST.\n", key);
break;
case 7:
printf("Thank you! Exiting....\n");
break;
default:
printf("Invalid Input!\n");
break;
}
} while(choice != 7);
return 0;
}
struct Node* createNode(int value) {
struct Node* ptr = (struct Node*)malloc(sizeof(struct Node));
ptr->data = value;
ptr->left = ptr->right = NULL;
return ptr;
}
// If the key to be deleted is smaller than the root's data, go to the left subtree
if (key < root->data) {
root->left = deleteNode(root->left, key);
}
// If the key to be deleted is larger than the root's data, go to the right subtree
else if (key > root->data) {
root->right = deleteNode(root->right, key);
}
// If the key is the same as the root's data, this is the node to be deleted
else {
// Node has one or no child
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp; // Return the right child (or NULL if no child)
}
else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp; // Return the left child (or NULL if no child)
}
// Node has two children, get the inorder successor (smallest in the right subtree)
struct Node* temp = findMin(root->right);
return root;
}