Semester II Lab Programs DS 2025
Semester II Lab Programs DS 2025
#include <stdio.h>
int a[20], n, val, i, pos;
void display();
void insert();
void del();
int main() {
int choice;
printf("\nEnter the size of the array elements: ");
scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
do {
printf("\n\n--------Menu-----------\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Exit\n");
printf("-----------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
del();
break;
case 3:
break;
default:
printf("\nInvalid choice!\n");
}
} while (choice != 3);
return 0;
}
void display() {
printf("\nThe array elements are:\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
void insert() {
printf("\nEnter the position for the new element: ");
scanf("%d", &pos);
printf("\nEnter the element to be inserted: ");
scanf("%d", &val);
for (i = n-1; i >= pos-1; i--) {
a[i+1] = a[i];
}
a[pos - 1] = val;
n = n + 1;
display();
}
void del() {
printf("\nEnter the position of the element to be deleted: ");
scanf("%d", &pos);
int main() {
int arr[20], n, target, result;
return 0;
}
3. Write a program to search for an element in an array using Binary Search
#include <stdio.h>
if (arr[mid] == number) {
return mid; // Target found, return index
}
int main() {
int arr[20], n, number, result;
return 0;
}
4. Write a program to sort an array using Bubble Sort
#include <stdio.h>
int main() {
int arr[20], n;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
5. Write a program to sort an array using Selection Sort
#include <stdio.h>
int main() {
int arr[20], n;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
6. Write a program to sort an array using insertion Sort
#include <stdio.h>
int main() {
int arr[20], n,i,j;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
7. Write a program to merge two arrays
#include <stdio.h>
int main() {
int ar1[10],ar2[10],n1,n2,i,arr[50];
printf("Enter the number of elements in the array: ");
scanf("%d", &n1);
printf("\nelements of the first array are : \n");
for (i = 0; i < n1; i++)
{
scanf("%d", &ar1[i]);
}
printf("\n\nEnter the number of elements for second array: ");
scanf("%d", &n2);
for (i = 0; i < n2; i++)
{
printf("Enter element %d:\n", i + 1);
scanf("%d", &ar2[i]);
}
return 0;
}
8. Write a program to add two matrix
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
// add the matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
// print the result
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
// add the matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
// print the result
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int a[r1][c1],b[r2][c2],c[r1][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
c[i][j]=0;
#include <stdio.h>
#define MAX 15
int stack[MAX], top = -1, item;
void push();
void pop();
void display();
int main() {
printf("\n\t\t\t*** STACK OPERATION ***\n");
int ch;
do {
printf("\nMenu : ");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("\nInvalid Entry");
}
} while(ch != 4);
return 0;
}
void push() {
if(top == MAX - 1) {
printf("\nOVERFLOW");
} else {
top++;
printf("\nEnter an item: ");
scanf("%d", &item);
stack[top] = item;
printf("\nInserted successfully");
}
}
void pop() {
if(top == -1) {
printf("\nUNDERFLOW");
} else {
item = stack[top];
top--;
printf("\n%dDeleted successfully",item);
}
}
void display() {
if(top == -1) {
printf("\nStack is empty");
} else {
for(int i = top; i >= 0; i--){
printf("%d\n", stack[i]);
}
}
}
12. QUEUE OPERATIONS
#include <stdio.h>
void enqueue();
void dequeue();
void display();
int main() {
int ch;
printf("\nEnter size of queue: ");
scanf("%d", &n);
printf("\n\t\t\t***QUEUE OPERATIONS***\n");
do {
printf("\nMenu:\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("\nExiting program...\n");
break;
default:
printf("\nInvalid Entry\n");
}
} while (ch != 4);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define size 5
int main()
{
int arr[size],R=-1,F=0,count=0,ch,n,i,x;
printf("\n1. Add");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(count==size)
{
printf("Queue is full");
}
else
{
printf("Enter a number ");
scanf("%d",&n);
R=(R+1)%size;
arr[R]=n;
count=count+1;
}
break;
case 2:
if(count==0)
{
printf("Queue is empty");
}
else
{
printf("Number Deleted = %d",arr[F]);
F=(F+1)%size;
count=count-1;
}
break;
case 3:
if(count==0)
{
printf("Queue is empty");
}
else
{
x=F;
for(i=1; i<=count; i++)
{
printf("%d ",arr[x]);
x=(x+1)%size;
}
}
break;
case 4:
exit(0);
break;
default:
printf("Wrong Choice");
}
}
return 0;
}
if (head == NULL) {
head = newNode;
} else {
struct Node *temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("Inserted %d at the end.\n", value);
}
do {
printf("\nMenu:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Specified Position\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value and position to insert: ");
scanf("%d %d", &value, &position);
insertAtPosition(value, position);
break;
case 4:
displayList();
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
15. Program to Delete a Node in Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
do {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Delete from Beginning\n");
printf("3. Delete from End\n");
printf("4. Delete from Specified Position\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
deleteFromBeginning();
break;
case 3:
deleteFromEnd();
break;
case 4:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteFromPosition(position);
break;
case 5:
displayList();
break;
case 6:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);
return 0;
}
16. Searching a Value in the Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
do {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &value);
search(value);
break;
case 3:
displayList();
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
17. a program to perform the following operations in a Doubly Linked List: (a) Create
(b) Search for an element
#include <stdio.h>
#include <stdlib.h>
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
printf("Inserted %d into the list.\n", value);
}
return 0;
}
18. Write a program to perform the following operations in a Circular Linked List: (a)
Create (b) Search an element
#include <stdio.h>
#include <stdlib.h>
if (head == NULL) {
head = newNode;
head->next = head;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
printf("Inserted %d into the list.\n", value);
}
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
create(value);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &value);
search(value);
break;
case 3:
display();
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
19. Write a program to perform the following operations on a binary search tree. (a)
Preorder Traversal (b) Inorder Traversal (c) Postorder Traversal
#include <stdio.h>
#include <stdlib.h>
struct Node {
int info;
struct Node* left;
struct Node* right;
};
int main() {
int ch, value;
do {
printf("\n1. Add Node");
printf("\n2. Inorder Traversal");
printf("\n3. Preorder Traversal");
printf("\n4. Postorder Traversal");
printf("\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the value: ");
scanf("%d", &value);
addNode(value);
break;
case 2:
if (root == NULL)
printf("Tree is empty\n");
else {
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
}
break;
case 3:
if (root == NULL)
printf("Tree is empty\n");
else {
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
}
break;
case 4:
if (root == NULL)
printf("Tree is empty\n");
else {
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
}
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (ch != 5);
return 0;
}
20. Write a program to perform the following using recursion: (a) Find the factorial of
a number (b)Generate Fibonacci Series
#include <stdio.h>
int main() {
int choice, num, i;
do {
printf("\nMenu:\n");
printf("1. Find Factorial\n");
printf("2. Generate Fibonacci Series\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a number to find factorial: ");
scanf("%d", &num);
printf("Factorial of %d is: %d\n", num, factorial(num));
break;
case 2:
printf("Enter the number of terms for Fibonacci series: ");
scanf("%d", &num);
printf("Fibonacci Series: ");
for (i = 0; i < num; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3);
return 0;
}