1.
a. Write a program for insertion operation in an array.
Ans: #include <stdio.h>
int main() {
int arr[100], size, i;
int val, pos;
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for(i = 0; i < size; i++) {
scanf("%d", &arr[i]);
printf("\nOriginal array:\n");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
printf("\nEnter the value to insert: ");
scanf("%d", &val);
printf("Enter the position (0 to %d) to insert at: ", size);
scanf("%d", &pos);
if(pos < 0 || pos > size) {
printf("Invalid position for insertion.\n");
} else {
for(i = size; i > pos; i--) {
arr[i] = arr[i - 1];
arr[pos] = val;
size++;
printf("Array after insertion:\n");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
Output
Enter the number of elements in the array: 5
Enter 5 elements:
12345
Original array:
12345
Enter the value to insert: 7
Enter the position (0 to 5) to insert at: 4
Array after insertion:
1 23475
b. Write a program for deletion operation in an array.
Ans: #include <stdio.h>
int main() {
int arr[100], size, i;
int val, pos;
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for(i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("\nOriginal array:\n");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
printf("\nEnter the position (0 to %d) to delete from: ", size - 1);
scanf("%d", &pos);
if(pos < 0 || pos >= size) {
printf("Invalid position for deletion.\n");
} else {
for(i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
size--;
printf("Array after deletion:\n");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
Output
Enter the number of elements in the array: 5
Enter 5 elements:
12345
Original array:
12345
Enter the position (0 to 4) to delete from: 3
Array after deletion:
1 235
2.
a. Write a program in c to search for an element in an array using Linear Search.
Ans: #include <stdio.h>
int main() {
int arr[100], n, i, search, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &search);
for(i = 0; i < n; i++) {
if(arr[i] == search) {
found = 1;
printf("Element found at index %d\n", i);
break;
if(!found) {
printf("Element not found in the array.\n");
return 0;
Output
Enter the number of elements: 5
Enter 5 elements:
12345
Enter the element to search: 4
Element found at index 3
b. Write a program in c to search for an element in an array using Binary Search.
Ans: #include <stdio.h>
int main() {
int arr[100], n, search, i;
int low, high, mid;
int found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements in sorted order:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &search);
low = 0;
high = n - 1;
while(low <= high) {
mid = (low + high) / 2;
if(arr[mid] == search) {
found = 1;
printf("Element found at index %d\n", mid);
break;
} else if(arr[mid] < search) {
low = mid + 1;
} else {
high = mid - 1;
}
if(!found) {
printf("Element not found in the array.\n");
return 0;
Output
Enter the number of elements: 5
Enter 5 elements in sorted order:
12345
Enter the element to search: 4
Element found at index 3
3. Write a program to sort an array using
a. Bubble Sort
Ans: #include <stdio.h>
int main() {
int arr[100], n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
printf("\nArray after Bubble Sort:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
Output
Enter the number of elements: 5
Enter 5 elements:
24531
Array after Bubble Sort:
12345
b. Selection sort
Ans: #include <stdio.h>
int main() {
int arr[100], n, i, j, min_idx, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n - 1; i++) {
min_idx = i;
for(j = i + 1; j < n; j++) {
if(arr[j] < arr[min_idx]) {
min_idx = j;
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
printf("\nArray after Selection Sort:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
Output
Enter the number of elements: 5
Enter 5 elements:
45213
Array after Selection Sort:
12345
c. Insertion sort
Ans: #include <stdio.h>
int main() {
int arr[100], n, i, j, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
for(i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
printf("\nArray after Insertion Sort:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
Output
Enter the number of elements: 5
Enter 5 elements:
43512
Array after Insertion Sort:
12345
4. Write a program to merge two arrays.
Ans: #include <stdio.h>
int main() {
int arr1[50], arr2[50], merged[100];
int n1, n2, i, j;
printf("Enter the number of elements in the first array: ");
scanf("%d", &n1);
printf("Enter %d elements for the first array:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
printf("Enter the number of elements in the second array: ");
scanf("%d", &n2);
printf("Enter %d elements for the second array:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
for(i = 0; i < n1; i++) {
merged[i] = arr1[i];
for(j = 0; j < n2; j++) {
merged[i + j] = arr2[j];
printf("\nMerged array:\n");
for(i = 0; i < n1 + n2; i++) {
printf("%d ", merged[i]);
printf("\n");
return 0;
}
Output
Enter the number of elements in the first array: 5
Enter 5 elements for the first array:
12456
Enter the number of elements in the second array: 5
Enter 5 elements for the second array:
76893
Merged array:
1245676893
5. Write a program to add and subtract two matrices.
Ans: #include <stdio.h>
int main() {
int mat1[10][10], mat2[10][10], sum[10][10], diff[10][10];
int rows, cols, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &rows, &cols);
printf("Enter elements of the first matrix (%d x %d):\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &mat1[i][j]);
printf("Enter elements of the second matrix (%d x %d):\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &mat2[i][j]);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
diff[i][j] = mat1[i][j] - mat2[i][j];
printf("\nSum of the matrices:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
printf("\n");
printf("\nDifference of the matrices (Matrix 1 - Matrix 2):\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d ", diff[i][j]);
printf("\n");
return 0;
Output
Enter the number of rows and columns of the matrices: 2 2
Enter elements of the first matrix (2 x 2):
4689
Enter elements of the second matrix (2 x 2):
2346
Sum of the matrices:
69
12 15
Difference of the matrices (Matrix 1 - Matrix 2):
23
43
6. Write a program to multiply two matrices.
Ans: #include <stdio.h>
int main() {
int mat1[10][10], mat2[10][10], result[10][10];
int r1, c1, r2, c2;
int i, j, k;
printf("Enter rows and columns for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2) {
printf("Matrix multiplication not possible. Columns of first matrix must equal rows of second
matrix.\n");
return 1;
printf("Enter elements of the first matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
scanf("%d", &mat1[i][j]);
printf("Enter elements of the second matrix:\n");
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
scanf("%d", &mat2[i][j]);
}
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
result[i][j] = 0;
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
for(k = 0; k < c1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
printf("\nProduct of the two matrices:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d ", result[i][j]);
printf("\n");
return 0;
Output
Enter rows and columns for the first matrix: 2 2
Enter rows and columns for the second matrix: 2 2
Enter elements of the first matrix:
4563
Enter elements of the second matrix:
1326
Product of the two matrices:
14 42
12 36
7. Write a program to insert an element into a Singly Linked List:
a. At the beginning
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* newNode;
int value, choice;
do {
printf("Enter the value to insert at the beginning: ");
scanf("%d", &value);
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Current Linked List: ");
struct Node* temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
printf("Do you want to insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while(choice == 1);
return 0;
Output
Enter the value to insert at the beginning: 10
Current Linked List: 10 -> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 1
Enter the value to insert at the beginning: 20
Current Linked List: 20 -> 10 -> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 1
Enter the value to insert at the beginning: 30
Current Linked List: 30 -> 20 -> 10 -> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 0
b. At the end
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* newNode, *temp;
int value, choice;
do {
newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter the value to insert at the end: ");
scanf("%d", &value);
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode; // Add at the end
printf("Current Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Do you want to insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
}
Output
Enter the value to insert at the end: 10
Current Linked List: 10 -> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 1
Enter the value to insert at the end: 20
Current Linked List: 10 -> 20 -> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 1
Enter the value to insert at the end: 30
Current Linked List: 10 -> 20 -> 30 -> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 0
c. At a specified position
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* newNode, *temp;
int value, position, i, choice;
do {
newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter the value to insert: ");
scanf("%d", &value);
printf("Enter the position to insert at (starting from 1): ");
scanf("%d", &position);
newNode->data = value;
newNode->next = NULL;
if (position == 1) {
newNode->next = head;
head = newNode;
else {
temp = head;
for (i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
if (temp == NULL) {
printf("Invalid position.\n");
free(newNode);
} else {
newNode->next = temp->next;
temp->next = newNode;
printf("Current Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Do you want to insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
Output
Enter the value to insert: 10
Enter the position to insert at (starting from 1): 1
Current Linked List: 10 -> NULL
Enter the value to insert: 20
Enter the position to insert at (starting from 1): 2
Current Linked List: 10 -> 20 -> NULL
Enter the value to insert: 15
Enter the position to insert at (starting from 1): 2
Current Linked List: 10 -> 15 -> 20 -> NULL
8. Write a program to delete an element from a Singly Linked List:
a. At the beginning
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* temp;
int value, choice;
do {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Current Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
do {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
break;
temp = head;
head = head->next;
printf("Deleted element: %d\n", temp->data);
free(temp);
printf("Updated Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Delete another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
Output
Enter value to insert at beginning: 10
Current Linked List: 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert at beginning: 20
Current Linked List: 20 -> 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 0
Deleted element: 20
Updated Linked List: 10 -> NULL
Delete another node? (1 = Yes, 0 = No): 1
Deleted element: 10
Updated Linked List: NULL
Delete another node? (1 = Yes, 0 = No): 0
b. At the end
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* temp, *prev;
int value, choice;
do {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Current Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
do {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
break;
if (head->next == NULL) {
printf("Deleted element: %d\n", head->data);
free(head);
head = NULL;
} else {
temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
printf("Deleted element: %d\n", temp->next->data);
free(temp->next);
temp->next = NULL;
printf("Updated Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Delete another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
Output
Enter value to insert at beginning: 10
Current Linked List: 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert at beginning: 20
Current Linked List: 20 -> 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert at beginning: 30
Current Linked List: 30 -> 20 -> 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 0
Deleted element: 10
Updated Linked List: 30 -> 20 -> NULL
Delete another node? (1 = Yes, 0 = No): 1
Deleted element: 20
Updated Linked List: 30 -> NULL
Delete another node? (1 = Yes, 0 = No): 0
c. A specific element
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* temp, *prev;
int value, choice, position, i;
do {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Current Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
do {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
break;
printf("Enter position to delete (starting from 1): ");
scanf("%d", &position);
if (position == 1) {
temp = head;
head = head->next;
printf("Deleted element: %d\n", temp->data);
free(temp);
} else {
temp = head;
for (i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
if (temp == NULL || temp->next == NULL) {
printf("Invalid position.\n");
} else {
struct Node* delNode = temp->next;
temp->next = delNode->next;
printf("Deleted element: %d\n", delNode->data);
free(delNode);
printf("Updated Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Delete another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
Output
Enter value to insert at beginning: 10
Current Linked List: 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert at beginning: 20
Current Linked List: 20 -> 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert at beginning: 30
Current Linked List: 30 -> 20 -> 10 -> NULL
Insert another node? (1 = Yes, 0 = No): 0
Enter position to delete (starting from 1): 2
Deleted element: 20
Updated Linked List: 30 -> 10 -> NULL
Delete another node? (1 = Yes, 0 = No): 1
Enter position to delete (starting from 1): 1
Deleted element: 30
Updated Linked List: 10 -> NULL
Delete another node? (1 = Yes, 0 = No): 0
9. Write a program to perform the following operations in a Doubly Linked List:
a. Create
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* tail = NULL;
struct Node* newNode;
int value, choice;
printf("Creating Doubly Linked List:\n");
do {
newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter value to insert: ");
scanf("%d", &value);
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
printf("Current Doubly Linked List: ");
struct Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Do you want to insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
Output
Creating Doubly Linked List:
Enter value to insert: 10
Current Doubly Linked List: 10 <-> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 20
Current Doubly Linked List: 10 <-> 20 <-> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 30
Current Doubly Linked List: 10 <-> 20 <-> 30 <-> NULL
Do you want to insert another node? (1 = Yes, 0 = No): 0
b. Search for an element
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* tail = NULL;
struct Node* newNode, *temp;
int value, choice, searchValue, found = 0, position = 1;
printf("Creating Doubly Linked List:\n");
do {
newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter value to insert: ");
scanf("%d", &value);
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
printf("Insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
printf("Doubly Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
printf("NULL\n");
printf("Enter element to search: ");
scanf("%d", &searchValue);
temp = head;
while (temp != NULL) {
if (temp->data == searchValue) {
found = 1;
break;
temp = temp->next;
position++;
if (found)
printf("Element %d found at position %d.\n", searchValue, position);
else
printf("Element %d not found in the list.\n", searchValue);
return 0;
Output
Creating Doubly Linked List:
Enter value to insert: 10
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 20
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 30
Insert another node? (1 = Yes, 0 = No): 0
Doubly Linked List: 10 <-> 20 <-> 30 <-> NULL
Enter element to search: 20
Element 20 found at position 2.
10. Write a program to perform the following operations in a Circular Linked List:
a. Create
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* tail = NULL;
struct Node* newNode;
int value, choice;
printf("Creating Circular Linked List:\n");
do {
newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter value to insert: ");
scanf("%d", &value);
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
tail->next = head; // Make it circular
} else {
tail->next = newNode;
newNode->next = head;
tail = newNode;
printf("Current Circular Linked List: ");
struct Node* temp = head;
if (head != NULL) {
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to head)\n");
printf("Insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
Output
Creating Circular Linked List:
Enter value to insert: 10
Current Circular Linked List: 10 -> (back to head)
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 20
Current Circular Linked List: 10 -> 20 -> (back to head)
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 30
Current Circular Linked List: 10 -> 20 -> 30 -> (back to head)
Insert another node? (1 = Yes, 0 = No): 0
b. Delete an element from the end
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* tail = NULL;
struct Node* newNode, *temp;
int value, choice;
printf("Creating Circular Linked List:\n");
do {
newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("Enter value to insert: ");
scanf("%d", &value);
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
tail->next = head;
} else {
tail->next = newNode;
newNode->next = head;
tail = newNode;
printf("Insert another node? (1 = Yes, 0 = No): ");
scanf("%d", &choice);
} while (choice == 1);
printf("\nDeleting node from the end...\n");
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
} else if (head == tail) {
printf("Deleted element: %d\n", head->data);
free(head);
head = tail = NULL;
} else {
temp = head;
while (temp->next != tail) {
temp = temp->next;
printf("Deleted element: %d\n", tail->data);
free(tail);
tail = temp;
tail->next = head;
printf("Updated Circular Linked List: ");
if (head == NULL) {
printf("List is empty.\n");
} else {
temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to head)\n");
return 0;
Output
Creating Circular Linked List:
Enter value to insert: 10
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 20
Insert another node? (1 = Yes, 0 = No): 1
Enter value to insert: 30
Insert another node? (1 = Yes, 0 = No): 0
Deleting node from the end...
Deleted element: 30
Updated Circular Linked List: 10 -> 20 ->
11. Write a program to implement stack operations using an array.
Ans: #include <stdio.h>
#define SIZE 100
int main() {
int stack[SIZE];
int top = -1;
int choice, value, i;
do {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1: // Push
if (top == SIZE - 1) {
printf("Stack Overflow! Cannot push.\n");
} else {
printf("Enter value to push: ");
scanf("%d", &value);
top++;
stack[top] = value;
printf("%d pushed onto stack.\n", value);
break;
case 2: // Pop
if (top == -1) {
printf("Stack Underflow! Cannot pop.\n");
} else {
printf("Popped element: %d\n", stack[top]);
top--;
break;
case 3: // Peek
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Top element: %d\n", stack[top]);
break;
case 4: // Display
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Stack elements (top to bottom): ");
for (i = top; i >= 0; i--) {
printf("%d ", stack[i]);
printf("\n");
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 5);
return 0;
Output
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice (1-5): 1
Enter value to push: 10
10 pushed onto stack.
Enter your choice (1-5): 1
Enter value to push: 20
20 pushed onto stack.
Enter your choice (1-5): 4
Stack elements (top to bottom): 20 10
Enter your choice (1-5): 2
Popped element: 20
Enter your choice (1-5): 3
Top element: 10
Enter your choice (1-5): 5
Exiting program.
12. Write a program to implement stack operations using a linked list.
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* top = NULL; // Top of the stack
struct Node* temp;
int choice, value;
do {
printf("\nStack Operations (Linked List):\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1: // Push
printf("Enter value to push: ");
scanf("%d", &value);
temp = (struct Node*)malloc(sizeof(struct Node));
if (temp == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
temp->data = value;
temp->next = top;
top = temp;
printf("%d pushed onto stack.\n", value);
break;
case 2: // Pop
if (top == NULL) {
printf("Stack Underflow! Cannot pop.\n");
} else {
temp = top;
printf("Popped element: %d\n", top->data);
top = top->next;
free(temp);
break;
case 3: // Peek
if (top == NULL) {
printf("Stack is empty.\n");
} else {
printf("Top element: %d\n", top->data);
break;
case 4: // Display
if (top == NULL) {
printf("Stack is empty.\n");
} else {
printf("Stack elements (top to bottom): ");
temp = top;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
printf("\n");
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
} while (choice != 5);
return 0;
Output
Stack Operations (Linked List):
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice (1-5): 1
Enter value to push: 10
10 pushed onto stack.
Enter your choice (1-5): 1
Enter value to push: 20
20 pushed onto stack.
Enter your choice (1-5): 4
Stack elements (top to bottom): 20 10
Enter your choice (1-5): 2
Popped element: 20
Enter your choice (1-5): 3
Top element: 10
Enter your choice (1-5): 5
Exiting program.
13. Write a program to add two polynomials using a linked list.
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int coeff;
int expo;
struct Node* next;
};
struct Node* createNode(int coeff, int expo) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coeff = coeff;
newNode->expo = expo;
newNode->next = NULL;
return newNode;
void insertTerm(struct Node** poly, int coeff, int expo) {
struct Node* newNode = createNode(coeff, expo);
if (*poly == NULL) {
*poly = newNode;
} else {
struct Node* temp = *poly;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
void displayPoly(struct Node* poly) {
while (poly != NULL) {
printf("%dx^%d", poly->coeff, poly->expo);
if (poly->next != NULL)
printf(" + ");
poly = poly->next;
printf("\n");
struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {
struct Node* result = NULL;
while (poly1 != NULL && poly2 != NULL) {
if (poly1->expo == poly2->expo) {
insertTerm(&result, poly1->coeff + poly2->coeff, poly1->expo);
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->expo > poly2->expo) {
insertTerm(&result, poly1->coeff, poly1->expo);
poly1 = poly1->next;
} else {
insertTerm(&result, poly2->coeff, poly2->expo);
poly2 = poly2->next;
while (poly1 != NULL) {
insertTerm(&result, poly1->coeff, poly1->expo);
poly1 = poly1->next;
while (poly2 != NULL) {
insertTerm(&result, poly2->coeff, poly2->expo);
poly2 = poly2->next;
return result;
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* sum = NULL;
int n, coeff, expo, i;
printf("Enter number of terms in Polynomial 1: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter coefficient and exponent for term %d: ", i + 1);
scanf("%d %d", &coeff, &expo);
insertTerm(&poly1, coeff, expo);
printf("Enter number of terms in Polynomial 2: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter coefficient and exponent for term %d: ", i + 1);
scanf("%d %d", &coeff, &expo);
insertTerm(&poly2, coeff, expo);
printf("\nPolynomial 1: ");
displayPoly(poly1);
printf("Polynomial 2: ");
displayPoly(poly2);
sum = addPolynomials(poly1, poly2);
printf("\nSum of polynomials: ");
displayPoly(sum);
return 0;
Output
Enter number of terms in Polynomial 1: 3
Enter coefficient and exponent for term 1: 5 2
Enter coefficient and exponent for term 2: 4 1
Enter coefficient and exponent for term 3: 2 0
Enter number of terms in Polynomial 2: 3
Enter coefficient and exponent for term 1: 3 2
Enter coefficient and exponent for term 2: 1 1
Enter coefficient and exponent for term 3: 6 0
Polynomial 1: 5x^2 + 4x^1 + 2x^0
Polynomial 2: 3x^2 + 1x^1 + 6x^0
Sum of polynomials: 8x^2 + 5x^1 + 8x^0
14. Write a program to evaluate a postfix expression using a stack
Ans: #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define SIZE 100
int main() {
char postfix[SIZE];
int stack[SIZE];
int top = -1;
int i;
char ch;
int op1, op2, result;
printf("Enter a postfix expression (single-digit operands, e.g., 53+62-*): ");
scanf("%s", postfix);
for (i = 0; postfix[i] != '\0'; i++) {
ch = postfix[i];
if (isdigit(ch)) {
stack[++top] = ch - '0';
} else {
if (top < 1) {
printf("Invalid expression\n");
return 1;
op2 = stack[top--];
op1 = stack[top--];
switch (ch) {
case '+': result = op1 + op2; break;
case '-': result = op1 - op2; break;
case '*': result = op1 * op2; break;
case '/': result = op1 / op2; break;
case '^': result = pow(op1, op2); break;
default:
printf("Unknown operator: %c\n", ch);
return 1;
stack[++top] = result;
if (top == 0) {
printf("Result = %d\n", stack[top]);
} else {
printf("Invalid postfix expression\n");
return 0;
Output
Enter a postfix expression (single-digit operands, e.g., 53+62-*): 53+62-*
Result = 32
15. Write a program to perform the following using recursion:
a. Find the factorial of a number
Ans: #include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0)
printf("Factorial is not defined for negative numbers.\n");
else
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
Output
Enter a non-negative integer: 5
Factorial of 5 = 120
b. Find the GCD of two numbers
Ans: #include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
int main() {
int num1, num2;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
if (num1 <= 0 || num2 <= 0) {
printf("Please enter positive integers only.\n");
} else {
printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
return 0;
}
Output
Enter two positive integers: 48 18
GCD of 48 and 18 is 6
c. Solve Towers of Hanoi problem
Ans: #include <stdio.h>
void towersOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", from_rod, to_rod);
return;
towersOfHanoi(n - 1, from_rod, aux_rod, to_rod;
printf("Move disk %d from %c to %c\n", n, from_rod, to_rod);
towersOfHanoi(n - 1, aux_rod, to_rod, from_rod);
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
printf("The sequence of moves to solve Towers of Hanoi with %d disks:\n", n);
towersOfHanoi(n, 'A', 'C', 'B');
return 0;
Output
Enter the number of disks: 3
The sequence of moves to solve Towers of Hanoi with 3 disks:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
16. Write a program to implement simple queue operations using an array.
Ans: #include <stdio.h>
#define SIZE 5
int main() {
int queue[SIZE];
int front = -1, rear = -1;
int choice, value;
do {
printf("\nQueue Operations:\n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Remove)\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch (choice) {
case 1: // Enqueue
if (rear == SIZE - 1) {
printf("Queue Overflow! Cannot insert %d\n", value);
} else {
printf("Enter value to enqueue: ");
scanf("%d", &value);
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("%d enqueued.\n", value);
break;
case 2: // Dequeue
if (front == -1 || front > rear) {
printf("Queue Underflow! Queue is empty.\n");
} else {
printf("Dequeued element: %d\n", queue[front]);
front++;
if (front > rear) {
front = rear = -1;
break;
case 3: // Display
if (front == -1) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
printf("\n");
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter 1-4.\n");
} while (choice != 4);
return 0;
Output
Queue Operations:
1. Enqueue (Insert)
2. Dequeue (Remove)
3. Display Queue
4. Exit
Enter your choice (1-4): 1
Enter value to enqueue: 10
10 enqueued.
Enter your choice (1-4): 1
Enter value to enqueue: 20
20 enqueued.
Enter your choice (1-4): 3
Queue elements: 10 20
Enter your choice (1-4): 2
Dequeued element: 10
Enter your choice (1-4): 3
Queue elements: 20
Enter your choice (1-4): 4
Exiting program.
17. Write a program to implement circular queue operations using an array
Ans: #include <stdio.h>
#define SIZE 5
int main() {
int queue[SIZE];
int front = -1, rear = -1;
int choice, value;
int isFull() {
return ((rear + 1) % SIZE == front);
int isEmpty() {
return (front == -1);
void enqueue(int val) {
if (isFull()) {
printf("Queue Overflow! Cannot insert %d\n", val);
return;
if (isEmpty()) {
front = 0;
rear = 0;
} else {
rear = (rear + 1) % SIZE;
queue[rear] = val;
printf("%d enqueued.\n", val);
}
void dequeue() {
if (isEmpty()) {
printf("Queue Underflow! Queue is empty.\n");
return;
printf("Dequeued element: %d\n", queue[front]);
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
void display() {
if (isEmpty()) {
printf("Queue is empty.\n");
return;
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear)
break;
i = (i + 1) % SIZE;
printf("\n");
}
do {
printf("\nCircular Queue Operations:\n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Remove)\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter 1-4.\n");
} while (choice != 4);
return 0;
}
Output
Circular Queue Operations:
1. Enqueue (Insert)
2. Dequeue (Remove)
3. Display Queue
4. Exit
Enter your choice (1-4): 1
Enter value to enqueue: 10
10 enqueued.
Enter your choice (1-4): 1
Enter value to enqueue: 20
20 enqueued.
Enter your choice (1-4): 3
Queue elements: 10 20
Enter your choice (1-4): 2
Dequeued element: 10
Enter your choice (1-4): 3
Queue elements: 20
Enter your choice (1-4): 4
Exiting program.
18. Write a program to implement circular queue operations using a linked list.
Ans: #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* rear = NULL
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return;
newNode->data = value;
if (rear == NULL) {
newNode->next = newNode;
rear = newNode;
} else {
newNode->next = rear->next;
rear->next = newNode;
rear = newNode;
printf("%d enqueued.\n", value);
void dequeue() {
if (rear == NULL) {
printf("Queue Underflow! Queue is empty.\n");
return;
struct Node* front = rear->next;
int value = front->data;
if (rear == front) {
free(front);
rear = NULL;
} else {
rear->next = front->next;
free(front);
printf("Dequeued element: %d\n", value);
void display() {
if (rear == NULL) {
printf("Queue is empty.\n");
return;
struct Node* temp = rear->next;
printf("Queue elements: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != rear->next);
printf("\n");
int main() {
int choice, value;
do {
printf("\nCircular Queue Operations (Linked List):\n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Remove)\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter 1-4.\n");
} while (choice != 4);
return 0;
Output
Circular Queue Operations (Linked List):
1. Enqueue (Insert)
2. Dequeue (Remove)
3. Display Queue
4. Exit
Enter your choice (1-4): 1
Enter value to enqueue: 10
10 enqueued.
Enter your choice (1-4): 1
Enter value to enqueue: 20
20 enqueued.
Enter your choice (1-4): 3
Queue elements: 10 20
Enter your choice (1-4): 2
Dequeued element: 10
Enter your choice (1-4): 3
Queue elements: 20
Enter your choice (1-4): 4
Exiting program.
19. Write a program to perform the following operations on a binary search tree.
a. Preorder Traversal
Ans: #include <stdio.h>
#include <stdlib.h>
int main() {
struct Node {
int data;
struct Node *left, *right;
};
struct Node* newNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
struct Node* insert(struct Node* root, int data) {
if (root == NULL) return newNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else
root->right = insert(root->right, data);
return root;
void preorder(struct Node* root) {
if (root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
return 0;
Output
Preorder Traversal: 50 30 20 40 70 60 80
b. Inorder Traversal
Ans: #include <stdio.h>
#include <stdlib.h>
int main() {
struct Node {
int data;
struct Node *left, *right;
};
struct Node* newNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
struct Node* insert(struct Node* root, int data) {
if (root == NULL) return newNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else
root->right = insert(root->right, data);
return root;
void inorder(struct Node* root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
return 0;
Output
Inorder Traversal: 20 30 40 50 60 70 80
c. Postorder Traversal
Ans: #include <stdio.h>
#include <stdlib.h>
int main() {
struct Node {
int data;
struct Node *left, *right;
};
struct Node* newNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
struct Node* insert(struct Node* root, int data) {
if (root == NULL) return newNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else
root->right = insert(root->right, data);
return root;
void postorder(struct Node* root) {
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
return 0;
Output
Postorder Traversal: 20 40 30 60 80 70 50
20. Write a program to perform insertion operation in a binary search tree.
Ans: #include <stdio.h>
#include <stdlib.h>
int main() {
struct Node {
int data;
struct Node *left, *right;
};
struct Node* newNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return newNode(data);
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
void inorder(struct Node* root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
struct Node* root = NULL;
int n, value;
printf("How many values do you want to insert? ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter value %d: ", i + 1);
scanf("%d", &value);
root = insert(root, value);
printf("BST inorder traversal (sorted): ");
inorder(root);
printf("\n");
return 0;
Output
How many values do you want to insert? 3
Enter value 1: 50
Enter value 2: 30
Enter value 3: 70
BST inorder traversal (sorted): 30 50 70