0% found this document useful (0 votes)
25 views28 pages

End Sem Exam (DS Code)

Uploaded by

stavankalkumbe
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)
25 views28 pages

End Sem Exam (DS Code)

Uploaded by

stavankalkumbe
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/ 28

DS Practical C

DFS:
#include <stdio.h>
#include <stdlib.h>
#define Max 10

struct Graph {
int numVertices;
int adjMatrix[Max][Max];
int Visited[Max];
};

struct Graph *createGraph(int numVertices) {


struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = numVertices;

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


for(int j = 0; j < numVertices; j++) {
graph->adjMatrix[i][j] = 0;
}
graph->Visited[i] = 0;
}
return graph;
}

void addEdge(struct Graph *graph, int src, int dest) {


graph->adjMatrix[src][dest] = 1;
graph->adjMatrix[dest][src] = 1;
}

void DFS(struct Graph *graph, int vertex) {


graph->Visited[vertex] = 1;
printf("%d ", vertex + 1); // Adjust for 1-indexed output

for(int i = 0; i < graph->numVertices; i++) {


if(graph->adjMatrix[vertex][i] == 1 && !graph->Visited[i]) {
DFS(graph, i);
}
}
}

int main() {
int numVertices, numEdges, src, dest, startVertex;

printf("Enter the number of Vertices: ");


scanf("%d", &numVertices);

struct Graph *graph = createGraph(numVertices);

printf("Enter the Number of Edges: ");


scanf("%d", &numEdges);

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


printf("Enter Edge %d (source, Destination): ", i + 1);
scanf("%d %d", &src, &dest);
addEdge(graph, src - 1, dest - 1); // Adjust for 0-indexing
}

printf("Enter starting Vertex for DFS: ");


scanf("%d", &startVertex);

printf("DFS Traversal starting from vertex %d: ", startVertex);


DFS(graph, startVertex - 1); // Adjust for 0-indexing
printf("\n");
free(graph);
return 0;
}

BFS:
#include <stdio.h>
#include <stdlib.h>
#define Max 10

struct Graph{
int numVertices;
int adjMatric[Max][Max];
int Visited[Max];
};

struct Graph* createGraph(numVertices){

struct Graph* graph=(struct Graph*)malloc(sizeof(struct Graph));

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;
}

void BFS(struct Graph* graph, startVertex){


int queue[Max],front=0,rear=0;
queue[rear++]=startVertex;
graph->Visited[startVertex]=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;

printf("Enter the number of Vertices: ");


scanf("%d",&numVertices);
createGraph(numVertices);

printf("Enter the number of Edges: ");


scanf("%d",&numEdges);

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

printf("Enter Edge %d (Source, Destination): ",i+1);


scanf("%d%d",&src,&dest);

addEdges(graph,src -1,dest-1);

printf("Enter starting vertex for BFS Traversal: ");


scanf("%d",&startVertex);

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;

while (hashTable[(index + i * i) % TABLE_SIZE] != -1 && i < TABLE_SIZE) {


i++;
}
if (i < TABLE_SIZE) {
hashTable[(index + i * i) % TABLE_SIZE] = key;
printf("Key %d inserted at index %d\n", key, (index + i * i) % TABLE_SIZE);
} else {
printf("Hash table is full! Unable to insert key %d\n", key);
}
}
// Function to display the hash table
void display() {
printf("Hash Table:\n");
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i] == -1) {
printf("Index %d: EMPTY\n", i);
} else {
printf("Index %d: %d\n", i, hashTable[i]);
}
}
}

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>

// Node structure for linked list


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

// Pointer to the top of the stack


struct Node* top = NULL;

// Function prototypes
void push(int value);
void pop();
void peek();
void isEmpty();
void display();

// Main function with menu-driven interface


int main() {
int choice, value;
do {
printf("\nStack Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Check if Empty\n");
printf("5. Display Stack\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Push
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2: // Pop
pop();
break;
case 3: // Peek
peek();
break;
case 4: // Check if Empty
isEmpty();
break;
case 5: // Display Stack
display();
break;
case 6: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 6);
return 0;
}

// Function to push an element onto the stack


void push(int value) {
struct Node* ptr = (struct Node*)malloc(sizeof(struct Node));
if (ptr == NULL) {
printf("Stack Overflow\n");
} else {
ptr->data = value; // Set the data
ptr->next = top; // Link new node to the current top
top = ptr; // Update top pointer
printf("%d pushed to stack\n", value);
}
}

// Function to pop an element from the stack


void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
} else {
int value = top->data; // Get the value from the top
struct Node* ptr = top; // Temporary pointer to the top node
top = top->next; // Update top to the next node
free(ptr); // Free the old top node
printf("%d popped from stack\n", value);
}
}

// Function to peek (see the top element)


void peek() {
if (top == NULL) {
printf("Stack is empty\n");
} else {
printf("Top element is %d\n", top->data);
}
}

// Function to check if the stack is empty


void isEmpty() {
if (top == NULL) {
printf("Stack is empty\n");
} else {
printf("Stack is not empty\n");
}
}

// Function to display all elements in the stack


void display() {
if (top == NULL) {
printf("Stack is empty\n");
} else {
struct Node* ptr = top;
printf("Stack elements:\n");
while (ptr != NULL) {
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
}

Queue Using Linked List


#include <stdio.h>
#include <stdlib.h>

// Node structure for linked list


struct Node {
int data;
struct Node* next;
};
// Pointers to the front and rear of the queue
struct Node* front = NULL;
struct Node* rear = NULL;

// Function prototypes
void enqueue(int value);
void dequeue();
void peek();
void isEmpty();
void display();

// Main function with menu-driven interface


int main() {
int choice, value;
do {
printf("\nQueue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Check if Empty\n");
printf("5. Display Queue\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Enqueue
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2: // Dequeue
dequeue();
break;
case 3: // Peek
peek();
break;
case 4: // Check if Empty
isEmpty();
break;
case 5: // Display Queue
display();
break;
case 6: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 6);
return 0;
}

// Function to enqueue an element to the queue


void enqueue(int value) {
struct Node* ptr = (struct Node*)malloc(sizeof(struct Node));
if (ptr == NULL) {
printf("Queue Overflow\n");
} else {
ptr->data = value; // Set the data
ptr->next = NULL; // New node will be the last node
if (rear == NULL) {
front = rear = ptr; // If queue is empty, front and rear are the same
} else {
rear->next = ptr; // Link the new node to the last node
rear = ptr; // Update rear to point to the new last node
}
printf("%d enqueued to queue\n", value);
}
}

// Function to dequeue an element from the queue


void dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
} else {
struct Node* ptr = front; // Temporary pointer to the front node
front = front->next; // Update front to the next node
if (front == NULL) {
rear = NULL; // If queue becomes empty, set rear to NULL
}
printf("%d dequeued from queue\n", ptr->data);
free(ptr); // Free the old front node
}
}

// Function to peek (see the front element)


void peek() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
printf("Front element is %d\n", front->data);
}
}

// Function to check if the queue is empty


void isEmpty() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
printf("Queue is not empty\n");
}
}

// Function to display all elements in the queue


void display() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
struct Node* ptr = front;
printf("Queue elements:\n");
while (ptr != NULL) {
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
}

Stack Using Array


#include <stdio.h>
#define MAX 100 // Maximum size of the stack

int stack[MAX]; // Array to hold stack elements


int top = -1; // Index of the top element (-1 indicates an empty stack)

// Function prototypes
void push(int value);
void pop();
void peek();
void isEmpty();
void display();

// Main function with menu-driven interface


int main() {
int choice, value;
do {
printf("\nStack Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Check if Empty\n");
printf("5. Display Stack\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Push
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2: // Pop
pop();
break;
case 3: // Peek
peek();
break;
case 4: // Check if Empty
isEmpty();
break;
case 5: // Display Stack
display();
break;
case 6: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 6);
return 0;
}

// Function to push an element onto the stack


void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = value; // Increment top and add the value
printf("%d pushed to stack\n", value);
}
}

// Function to pop an element from the stack


void pop() {
if (top == -1) {
printf("Stack Underflow\n");
} else {
printf("%d popped from stack\n", stack[top--]); // Remove top element and decrement
top
}
}

// Function to peek (see the top element)


void peek() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Top element is %d\n", stack[top]);
}
}

// Function to check if the stack is empty


void isEmpty() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack is not empty\n");
}
}

// Function to display all elements in the stack


void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
#include <stdio.h>
#define MAX 100 // Maximum size of the queue

int queue[MAX]; // Array to hold queue elements


int front = -1; // Index of the front element (-1 indicates an empty queue)
int rear = -1; // Index of the rear element

// Function prototypes
void enqueue(int value);
void dequeue();
void peek();
void isEmpty();
void display();

// Main function with menu-driven interface


int main() {
int choice, value;
do {
printf("\nQueue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Check if Empty\n");
printf("5. Display Queue\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Enqueue
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2: // Dequeue
dequeue();
break;
case 3: // Peek
peek();
break;
case 4: // Check if Empty
isEmpty();
break;
case 5: // Display Queue
display();
break;
case 6: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 6);
return 0;
}

// Function to enqueue an element to the queue


void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) {
front = 0; // Initialize front if queue was empty
}
queue[++rear] = value; // Increment rear and add the value
printf("%d enqueued to queue\n", value);
}
}

// Function to dequeue an element from the queue


void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
front = rear = -1; // Reset queue if empty
} else {
printf("%d dequeued from queue\n", queue[front++]); // Remove front element and
increment front
}
}

// Function to peek (see the front element)


void peek() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Front element is %d\n", queue[front]);
}
}

// Function to check if the queue is empty


void isEmpty() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Queue is not empty\n");
}
}
// Function to display all elements in the queue
void display() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Queue elements:\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

Binary Search Tree(BST):


#include<stdio.h>
#include<stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* createNode(int value);


struct Node* insert(struct Node* root, int value);
struct Node* search(struct Node* root, int key);
void inorder(struct Node* root);
void preorder(struct Node* root);
void postorder(struct Node* root);
struct Node* deleteNode(struct Node* root, int key);
struct Node* findMin(struct Node* root);
int main() {
struct Node* root = NULL;
int choice, value, key;

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;
}

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 if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}

struct Node* search(struct Node* root, int key) {


if (root == NULL || root->data == key) {
return root;
}
if (key < root->data) {
return search(root->left, key);
} else {
return search(root->right, key);
}
}

void inorder(struct Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

void preorder(struct Node* root) {


if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct Node* root) {


if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

// Function to delete a node in BST


struct Node* deleteNode(struct Node* root, int key) {
if (root == NULL) {
return root; // If the tree is empty or key is not found
}

// 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);

// Copy the inorder successor's content to this node


root->data = temp->data;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->data);
}

return root;
}

// Function to find the minimum value node in a BST (inorder successor)


struct Node* findMin(struct Node* root) {
while (root->left != NULL) {
root = root->left;
}
return root;
}

You might also like