DSA File
DSA File
1: Write a menu driven program for insertion, deletion and traversal in a one-
dimensional Array.
#include <stdio.h>
#define MAX 5 // Maximum size of the array
void displayMenu() {
printf("\nMenu:\n");
printf("1. Insert Element\n");
printf("2. Delete Element\n");
printf("3. Traverse (Display) Array\n");
printf("4. Exit\n");
}
// Function to insert an element at a specified position
void insertElement(int arr[], int *size) {
if (*size == MAX) {
printf("Array is full, cannot insert more elements.\n");
return;
}
int element, position,i;
printf("Enter the element to insert: ");
scanf("%d", &element);
printf("Enter the position (0 to %d): ", *size);
scanf("%d", &position);
while (1) {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertElement(arr, &size);
break;
case 2:
deleteElement(arr, &size);
break;
case 3:
traverseArray(arr, size);
break;
case 4:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice! Please choose a valid option.\n");
} }
return 0;
}
Output:
Program.2: Write a menu driven program for insertion, deletion and traversal in a 2-
dimensional Array:
#include <stdio.h>
void displayMenu() {
printf("\nMenu:\n");
printf("1. Insert Element\n");
printf("2. Delete Element\n");
printf("3. Traverse (Display) Array\n");
printf("4. Exit\n");
}
if (row < 0 || row >= *rows || col < 0 || col >= *cols) {
printf("Invalid position!\n");
} else {
printf("Enter the element to insert: ");
scanf("%d", &element);
arr[row][col] = element;
printf("Element %d inserted at position (%d, %d).\n", element, row, col);
}
}
if (*rows == 0 || *cols == 0) {
printf("Array is empty, nothing to delete!\n");
return;
}
if (row < 0 || row >= *rows || col < 0 || col >= *cols) {
printf("Invalid position!\n");
} else {
printf("Element %d at position (%d, %d) deleted.\n", arr[row][col], row, col);
arr[row][col] = 0; // Set the deleted element to 0 (or a sentinel value)
}
}
int main() {
printf("Name: Gautam Garg \n Roll No. : 23049561\n");
int arr[MAX_ROWS][MAX_COLS] = {0}; // Initialize array elements to 0
int rows, cols;
int choice;
while (1) {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertElement(arr, &rows, &cols);
break;
case 2:
deleteElement(arr, &rows, &cols);
break;
case 3:
traverseArray(arr, rows, cols);
break;
case 4:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice! Please choose a valid option.\n");
}
}
return 0;
}
Output:
Program 3 : Write a menu driven program for insertion, deletion and traversal in a names
(character) Array.
#include <stdio.h>
#include <string.h>
// Function prototypes
void insert(char names[MAX][50], int *n);
void delete(char names[MAX][50], int *n);
void traverse(char names[MAX][50], int n);
int main() {
char names[MAX][50]; // Array to store names
int n = 0; // Current number of names
int choice;
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
while (1) {
// Display the menu
printf("\nMenu:\n");
printf("1. Insert a name\n");
printf("2. Delete a name\n");
printf("3. Traverse names\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert(names, &n);
break;
case 2:
delete(names, &n);
break;
case 3:
traverse(names, n);
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
// Function to insert a name
void insert(char names[MAX][50], int *n) {
if (*n >= MAX) {
printf("Array is full, cannot insert more names.\n");
return;
}
printf("Enter the name to insert: ");
scanf("%s", names[*n]);
(*n)++;
printf("Name inserted successfully.\n");
}
// Function to delete a name
void delete(char names[MAX][50], int *n) {
if (*n == 0) {
printf("Array is empty, nothing to delete.\n");
return;
}
char delName[50];
printf("Enter the name to delete: ");
scanf("%s", delName);
int i, found = 0;
for (i = 0; i < *n; i++) {
if (strcmp(names[i], delName) == 0) {
found = 1;
break;
}
}
if (found) {
int j;
for ( j = i; j < *n - 1; j++) {
strcpy(names[j], names[j + 1]);
}
(*n)--;
printf("Name deleted successfully.\n");
} else {
printf("Name not found.\n");
}
}
// Function prototypes
void findMaxMin1D();
void findMaxMin2D();
int main() {
int choice;
while (1) {
printf("\nMenu:\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
findMaxMin1D();
break;
case 2:
findMaxMin2D();
break;
case 3:
printf("Exiting...\n");
return 0;
default:
return 0;
void findMaxMin1D() {
int n;
scanf("%d", &n);
int arr[n];
int i;
scanf("%d", &arr[i]);
max = arr[i];
if (arr[i] < min)
min = arr[i];
void findMaxMin2D() {
int arr[rows][cols];
int i,j;
scanf("%d", &arr[i][j]);
max = arr[i][j];
}
Output:
Program 5: Matrix Multiplication, addition and subtraction
#include <stdio.h>
// Function prototypes
void addMatrices(int rows, int cols, int matrix1[10][10], int matrix2[10][10], int result[10][10]);
void subtractMatrices(int rows, int cols, int matrix1[10][10], int matrix2[10][10], int result[10][10]);
void multiplyMatrices(int rows1, int cols1, int matrix1[10][10], int rows2, int cols2, int matrix2[10][10], int
result[10][10]);
int main() {
int choice, rows1, cols1, rows2, cols2;
int matrix1[10][10], matrix2[10][10], result[10][10];
int i,j;
while (1) {
// Display the menu
printf("\nMenu:\n");
printf("1. Matrix Addition\n");
printf("2. Matrix Subtraction\n");
printf("3. Matrix Multiplication\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Matrix addition
printf("Enter the number of rows and columns for both matrices: ");
scanf("%d %d", &rows1, &cols1);
case 2:
// Matrix subtraction
printf("Enter the number of rows and columns for both matrices: ");
scanf("%d %d", &rows1, &cols1);
case 3:
// Matrix multiplication
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication is not possible with these dimensions.\n");
break;
}
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
int stack[MAX];
int top = -1;
// Function prototypes
void push();
void pop();
void display();
int main() {
int choice;
while (1) {
// Display the menu
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
// Function to push an element onto the stack
void push() {
int value;
if (top == MAX - 1) {
printf("Stack Overflow! Cannot push any more elements.\n");
} else {
printf("Enter the value to push: ");
scanf("%d", &value);
top++;
stack[top] = value;
printf("Value pushed onto the stack.\n");
}
}
#include <stdlib.h>
struct Node {
int data;
};
// Function prototypes
int main() {
struct Node* top = NULL; // Initialize the stack as an empty linked list
int choice;
while (1) {
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
switch (choice) {
case 1:
push(&top);
break;
case 2:
pop(&top);
break;
case 3:
display(top);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
return 0;
int value;
if (newNode == NULL) {
return;
newNode->data = value;
newNode->next = *top;
*top = newNode;
if (*top == NULL) {
} else {
*top = (*top)->next;
free(temp);
if (top == NULL) {
printf("Stack is empty.\n");
} else {
printf("Stack elements:\n");
printf("%d\n", temp->data);
temp = temp->next;
}
Output:
Program 8: WAP to implement infix to post fix conversion.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
struct Stack {
int top;
char arr[MAX];
};
// Function prototypes
int main() {
gets(infix);
infixToPostfix(infix, postfix);
return 0;
if (stack->top == MAX - 1) {
printf("Stack Overflow!\n");
return;
stack->arr[++(stack->top)] = op;
if (isEmpty(stack)) {
printf("Stack Underflow!\n");
return -1;
return stack->arr[(stack->top)--];
return stack->arr[stack->top];
}
// Function to check if the stack is empty
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
return symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' || symbol == '^';
int i = 0, j = 0;
while (infix[i] != '\0') {
if (isalnum(symbol)) {
postfix[j++] = symbol;
push(&stack, symbol);
postfix[j++] = pop(&stack);
else if (isOperator(symbol)) {
postfix[j++] = pop(&stack);
push(&stack, symbol);
i++;
}
// Pop the remaining operators from the stack
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
Output:
Program 9: WAP to implement post fix expression evaluation.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
struct Stack {
int top;
int arr[MAX];
};
// Function prototypes
int main() {
char postfix[MAX];
gets(postfix);
return 0;
if (stack->top == MAX - 1) {
printf("Stack Overflow!\n");
return;
stack->arr[++(stack->top)] = value;
if (stack->top == -1) {
printf("Stack Underflow!\n");
exit(1);
return stack->arr[(stack->top)--];
if (isdigit(symbol)) {
// If the character is an operator, pop two operands and apply the operator
else {
switch (symbol) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
if (operand2 == 0) {
exit(1);
case '^':
break;
default:
exit(1);
i++;
// The final result will be the only value left in the stack
return pop(&stack);
}
Output:
Program 10: WAP for recursive Tower of Hanoi.
#include <stdio.h>
if (n == 1) {
return;
int main() {
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B'); // A, B, and C are the names of the rods
return 0;
}
Output:
Program 11: WAP for recursive factorial.
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
int main() {
int num;
scanf("%d", &num);
return 0;
}
Output:
Program 12: WAP for recursive Fibonacci series
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
int main() {
int n;
scanf("%d", &n);
printf("\n");
return 0;
}
Output:
Program 13: WAP to implement search in array: find 1st occurrence, only 2nd
occurrence, first two occurrence and last occurrence of the number.
#include <stdio.h>
// Function prototypes
int findFirstOccurrence(int arr[], int size, int num);
int findSecondOccurrence(int arr[], int size, int num);
void findFirstTwoOccurrences(int arr[], int size, int num);
int findLastOccurrence(int arr[], int size, int num);
int main() {
int arr[MAX], size, num, choice;
while (1) {
// Display the menu
printf("\nMenu:\n");
printf("1. Find first occurrence\n");
printf("2. Find second occurrence\n");
printf("3. Find first two occurrences\n");
printf("4. Find last occurrence\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
int index = findFirstOccurrence(arr, size, num);
if (index != -1) {
printf("First occurrence of %d is at index %d\n", num, index);
} else {
printf("Number %d not found in the array.\n", num);
}
break;
}
case 2: {
int index = findSecondOccurrence(arr, size, num);
if (index != -1) {
printf("Second occurrence of %d is at index %d\n", num, index);
} else {
printf("Second occurrence of %d not found in the array.\n", num);
}
break;
}
case 3: {
findFirstTwoOccurrences(arr, size, num);
break;
}
case 4: {
int index = findLastOccurrence(arr, size, num);
if (index != -1) {
printf("Last occurrence of %d is at index %d\n", num, index);
} else {
printf("Number %d not found in the array.\n", num);
}
break;
}
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
// Function to find the first occurrence of a number in the array
int findFirstOccurrence(int arr[], int size, int num) {
for (int i = 0; i < size; i++) {
if (arr[i] == num) {
return i; // Return the index of the first occurrence
}
}
return -1; // Return -1 if the number is not found
}
// Function prototypes
void insertAtEnd(struct Node** head, int data);
int findFirstOccurrence(struct Node* head, int num);
int findSecondOccurrence(struct Node* head, int num);
void findFirstTwoOccurrences(struct Node* head, int num);
int main() {
struct Node* head = NULL;
int choice, num, data;
// Custom print statement
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
// Menu-driven interface
while (1) {
printf("\nMenu:\n");
printf("1. Insert element in linked list\n");
printf("2. Find first occurrence\n");
printf("3. Find second occurrence\n");
printf("4. Find first two occurrences\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number to insert: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 2:
printf("Enter the number to search for: ");
scanf("%d", &num);
{
int index = findFirstOccurrence(head, num);
if (index != -1) {
printf("First occurrence of %d is at position %d\n", num, index);
} else {
printf("Number %d not found in the linked list.\n", num);
}
}
break;
case 3:
printf("Enter the number to search for: ");
scanf("%d", &num);
{
int index = findSecondOccurrence(head, num);
if (index != -1) {
printf("Second occurrence of %d is at position %d\n", num, index);
} else {
printf("Second occurrence of %d not found in the linked list.\n", num);
}
}
break;
case 4:
printf("Enter the number to search for: ");
scanf("%d", &num);
findFirstTwoOccurrences(head, num);
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
// Function to insert a node at the end of the linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;
new_node->data = data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
}
// Function to find the first occurrence of a number in the linked list
int findFirstOccurrence(struct Node* head, int num) {
int pos = 1;
struct Node* current = head;
while (current != NULL) {
if (current->data == num) {
return pos;
}
current = current->next;
pos++;
}
return -1; // Return -1 if the number is not found
}
// Function to find the second occurrence of a number in the linked list
int findSecondOccurrence(struct Node* head, int num) {
int pos = 1, count = 0;
struct Node* current = head;
if (count == 0) {
printf("Number not found.");
}
printf("\n");
}
Output:
Program 15: WAP to show insertion and deletion in a linked list:
a) at beginning b) at end c) in middle d) in sorted order
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
void insertAtBeginning(struct Node** head, int data);
void insertAtEnd(struct Node** head, int data);
void insertInMiddle(struct Node** head, int data, int position);
void insertInSortedOrder(struct Node** head, int data);
void deleteNode(struct Node** head, int key);
void displayList(struct Node* node);
int main() {
struct Node* head = NULL;
int choice, data, position;
// Menu-driven interface
while (1) {
printf("\nMenu:\n");
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Insert in middle\n");
printf("4. Insert in sorted order\n");
printf("5. Delete a node\n");
printf("6. Display the linked list\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number to insert at beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter the number to insert at end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter the number to insert in middle: ");
scanf("%d", &data);
printf("Enter the position (1-based index): ");
scanf("%d", &position);
insertInMiddle(&head, data, position);
break;
case 4:
printf("Enter the number to insert in sorted order: ");
scanf("%d", &data);
insertInSortedOrder(&head, data);
break;
case 5:
printf("Enter the number to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 6:
displayList(head);
break;
case 7:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
new_node->data = data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
last->next = new_node;
}
if (position == 1) {
new_node->next = *head;
*head = new_node;
return;
}
if (current == NULL) {
printf("The previous node is null. Inserting at the end.\n");
free(new_node);
insertAtEnd(head, data);
return;
}
new_node->next = current->next;
current->next = new_node;
}
// Function to insert a node in sorted order in the linked list
void insertInSortedOrder(struct Node** head, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
int main() {
struct CircularNode* circularHead = NULL;
struct HeaderNode* headerHead = createHeaderNode(0); // Create header node
struct DoublyNode* doublyHead = NULL;
return 0;
}
if (*head == NULL) {
*head = new_node;
new_node->next = *head; // Point to itself
} else {
struct CircularNode* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = new_node;
new_node->next = *head;
}
}
void deleteNodeCircular(struct CircularNode** head, int key) {
if (*head == NULL) return;
if (*head == NULL) {
new_node->prev = NULL;
*head = new_node;
return;
}
temp->next = new_node;
new_node->prev = temp;
}
free(temp);
}
return result;
}
int main() {
int degree;
double x;
// Input the value of x for evaluation
printf("Enter the value of x: ");
scanf("%lf", &x);
return 0;
}
Output:
Program 18: WAP to show working of simple queue using array and Linked list.
#include <stdio.h>
#include <stdlib.h>
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
// Array implementation
struct QueueArray qArray;
initializeQueueArray(&qArray);
return 0;
}
if (*front == NULL) {
// If the queue becomes empty, reset rear
return value;
}
free(temp);
return value;
}
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
// Array implementation
struct CircularQueueArray cqArray;
initializeCircularQueueArray(&cqArray);
return 0;
}
if (*front == (*front)->next) {
// If there is only one node
*front = NULL;
} else {
struct Node* rear = *front;
while (rear->next != *front) {
rear = rear->next;
}
rear->next = (*front)->next; // Update rear to point to the next node
*front = (*front)->next; // Move front to the next node
}
free(temp);
return value;
}
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
// Array implementation
struct PriorityQueueArray pqArray;
initializePriorityQueueArray(&pqArray);
return 0;
}
void enqueueLinkedList(struct Node** front, struct Node** rear, int value, int priority) {
struct Node* newNode = createNode(value, priority);
if (*front == NULL) {
*front = *rear = newNode; // If the queue is empty
printf("Enqueued: %d with priority %d\n", value, priority);
return;
}
if (newNode->next == NULL) {
*rear = newNode; // Update rear if it's the new last node
}
}
printf("Enqueued: %d with priority %d\n", value, priority);
}
// Main function
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
// Perform traversals
printf("In-order traversal: ");
inorderTraversal(root);
printf("\n");
return 0;
}
Output:
Program 22: WAP to show insertion, deletion and searching in a binary search tree.
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
// Delete a node
printf("Deleting node 20 from the BST...\n");
root = deleteNode(root, 20);
printf("In-order traversal after deletion: ");
inorderTraversal(root);
printf("\n");
return 0;
}
Output:
Program 23: WAP to show working of depth first traversal of a graph.
#include <stdio.h>
#include <stdlib.h>
return graph;
}
// Since the graph is undirected, add an edge from dest to src as well
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// DFS recursive function
void DFS(struct Graph* graph, int vertex, int* visited) {
visited[vertex] = 1; // Mark the current vertex as visited
printf("%d ", vertex); // Print the visited vertex
// Main function
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
return 0;
}
Output:
Program 24: WAP to show working of breadth first traversal of a graph.
#include <stdio.h>
#include <stdlib.h>
return graph;
}
// Since the graph is undirected, add an edge from dest to src as well
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// Main function
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
return 0;
}
Output:
Program 25: Write a Program to show the following sorting algorithms:
a) Bubble Sort b) Insertion Sort c) Quick Sort d) Merge Sort e) Selection Sort
#include <stdio.h>
i = 0;
j = 0;
k = left;
// Main function
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
return 0;
}
Output:
Program: WAP for Linear Search
#include <stdio.h>
// Main function
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
Output:
Program 27: WAP for Binary Search.
#include <stdio.h>
// Main function
int main() {
// Display Name and Roll Number
printf("Name: Gautam Garg \nRoll No. : 23049561\n");
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
Output: