0% found this document useful (0 votes)
3 views

DS Program List-2

This document is a practical file for a BCA course on Data Structures and Algorithms, containing various programming assignments. It includes programs for array operations, matrix manipulations, searching algorithms, sorting algorithms, and linked lists. Each section provides code implementations along with explanations for tasks such as insertion, deletion, traversal, and matrix operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DS Program List-2

This document is a practical file for a BCA course on Data Structures and Algorithms, containing various programming assignments. It includes programs for array operations, matrix manipulations, searching algorithms, sorting algorithms, and linked lists. Each section provides code implementations along with explanations for tasks such as insertion, deletion, traversal, and matrix operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 69

A Practical File

on

Data Structures and Algorithms


BCA-106 P
Bachelor of Computer Applications (BCA)
Guru Gobind Singh Indraprastha University, Delhi

Submitted To: Submitted By:

Ms.Aditi Aggarwal Student’s Name: Suhani Dixit

Ms. Leena Gupta Enrl No.: 04413702024

Batch: BCA-II(M1)

Institute of Information Technology & Management,


New Delhi – 110058
Batch (2024-2028)
INDEX

S.No Program Date


1. Write a program to implement one-dimensional
array
1. Insertion
2. Deletion
3. Traversal
4. Reverse
5. Merge
2. Write a Program to perform operations on matrix
like addition, subtraction, and multiplication.
3. Write a program to create sparse matrix.

4. Write a program to perform linear search in an


array.

5. Write a program to perform binary search.

6. Write a program to sort an array using menu


driven:
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Merge Sort
7. WAP to implement a Singly Linked List.

8. WAP to implement a Circular Linked Lists.

9. WAP to implement Doubly Linked Lists.

10. Write a menu driven program to implement


(i) Static Stack
(ii) Dynamic Stack.

11. WAP to implement postfix expressions using


stack.
12. WAP to implement :
(i) Static Linear Queue
(ii) Dynamic Linear Queue

13. WAP to implement :


(iii) Static Circular Queue
(iv) Dynamic Circular Queue

14. WAP to implement an expression tree. (For


example: (a + b / (c * d) – e) ).
15. WAP to implement recursive algorithms for
BST traversal- Inorder, Preorder, Postorder.
Question1. Write a program to implement one-dimensional array:
1. Insertion
2. Deletion
3. Traversal
4. Reverse
5. Merge
Solution:
Code:
#include <stdio.h>
void traverse(int arr[], int size) {
printf("Array elements: ");
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int insert(int arr[], int size, int element, int position, int capacity) {
if(size >= capacity || position < 0 || position > size) {
printf("Insertion not possible.\n");
return size;
}
for(int i = size; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
return size + 1;
}
int delete(int arr[], int size, int position) {
if(position < 0 || position >= size) {
printf("Deletion not possible.\n");
return size;
}
for(int i = position; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
return size - 1;
}
void reverse(int arr[], int size) {
int temp;
for(int i = 0; i < size / 2; i++) {
temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
}
int merge(int arr1[], int size1, int arr2[], int size2, int merged[]) {
for(int i = 0; i < size1; i++) {
merged[i] = arr1[i];
}
for(int i = 0; i < size2; i++) {
merged[size1 + i] = arr2[i];
}
return size1 + size2;
}
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int capacity = 10;
int arr[10], size, choice, element, position;
int arr2[10], size2, merged[20], mergedSize = 0;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Enter number of elements in array: ");
scanf("%d", &size);
if(size > capacity) {
printf("Size exceeds array capacity.\n");
return 1;
}
printf("Enter elements:\n");
for(int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
do {
printf("\nMenu:\n");
printf("1. Traverse\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Reverse\n");
printf("5. Merge with another array\n");
printf("6. Print array\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
traverse(arr, size);
break;
case 2:
printf("Enter element to insert: ");
scanf("%d", &element);
printf("Enter position (0 to %d): ", size);
scanf("%d", &position);
size = insert(arr, size, element, position, capacity);
break;
case 3:
printf("Enter position to delete (0 to %d): ", size - 1);
scanf("%d", &position);
size = delete(arr, size, position);
break;
case 4:
reverse(arr, size);
printf("Array reversed.\n");
break;
case 5:
printf("Enter number of elements in second array: ");
scanf("%d", &size2);
if(size2 > 10) {
printf("Second array too large.\n");
break;
}
printf("Enter elements:\n");
for(int i = 0; i < size2; i++) {
scanf("%d", &arr2[i]);
}
mergedSize = merge(arr, size, arr2, size2, merged);
printf("Merged array: ");
printArray(merged, mergedSize);
break;
case 6:
printf("Array: ");
printArray(arr, size);
break;
case 7:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while(choice != 7);
return 0;
}
Output:
Question2. Write a Program to perform operations on matrix like addition, subtraction, and
multiplication.
Solution:
Code:
#include <stdio.h>
void inputMatrix(int mat[10][10], int rows, int cols, char name) {
printf("Enter elements of Matrix %c (%dx%d):\n", name, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%c[%d][%d]: ", name, i, j);
scanf("%d", &mat[i][j]);
}
}
}
void printMatrix(int mat[10][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void addMatrix(int A[10][10], int B[10][10], int result[10][10], int rows, int cols) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
result[i][j] = A[i][j] + B[i][j];
}
void subtractMatrix(int A[10][10], int B[10][10], int result[10][10], int rows, int cols) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
result[i][j] = A[i][j] - B[i][j];
}
void multiplyMatrix(int A[10][10], int B[10][10], int result[10][10], int r1, int c1, int c2) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++)
result[i][j] += A[i][k] * B[k][j];
}
}
}
int main() {
int A[10][10], B[10][10], result[10][10];
int r1, c1, r2, c2, choice;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Enter rows and columns for Matrix A: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns for Matrix B: ");
scanf("%d %d", &r2, &c2);
inputMatrix(A, r1, c1, 'A');
inputMatrix(B, r2, c2, 'B');
do {
printf("\nMatrix Operations Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
if (r1 == r2 && c1 == c2) {
addMatrix(A, B, result, r1, c1);
printf("Result of Addition:\n");
printMatrix(result, r1, c1);
} else {
printf("Addition not possible. Matrix sizes must be equal.\n");
}
break;
case 2:
if (r1 == r2 && c1 == c2) {
subtractMatrix(A, B, result, r1, c1);
printf("Result of Subtraction:\n");
printMatrix(result, r1, c1);
} else {
printf("Subtraction not possible. Matrix sizes must be equal.\n");
}
break;
case 3:
if (c1 == r2) {
multiplyMatrix(A, B, result, r1, c1, c2);
printf("Result of Multiplication:\n");
printMatrix(result, r1, c2);
} else {
printf("Multiplication not possible. Columns of A must equal rows of B.\n");
}
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 4);
return 0;
}

Output:
Question3. Write a program to create sparse matrix.
Solution:
Code:
#include <stdio.h>
void displayMatrix(int rows, int cols, int matrix[rows][cols]) {
printf("\nOriginal Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows, cols;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Enter number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
int sparse[rows * cols][3];
int count = 0, k = 1;
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] != 0) {
sparse[k][0] = i;
sparse[k][1] = j;
sparse[k][2] = matrix[i][j];
k++;
count++;
}
}
}
displayMatrix(rows, cols, matrix);
sparse[0][0] = rows;
sparse[0][1] = cols;
sparse[0][2] = count;
printf("\nSparse Matrix (Triplet Representation):\n");
printf("Row Col Value\n");
for (int i = 0; i <= count; i++) {
printf(" %d %d %d\n", sparse[i][0], sparse[i][1], sparse[i][2]);
}
return 0;
}
Output:

Question4. Write a program to perform linear search in an array.


Solution:
Code:
#include <stdio.h>
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int main() {
int size, key;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search for: ");
scanf("%d", &key);
int result = linearSearch(arr, size, key);
if (result != -1) {
printf("Element %d found at position %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}

Output:
Question5. Write a program to perform binary search.
Solution:
Code:
#include <stdio.h>
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int size, key;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Enter number of elements in the array (sorted): ");
scanf("%d", &size);
int arr[size];
printf("Enter %d sorted elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, size, key);
if (result != -1) {
printf("Element %d found at position %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
Output:
Question6. Write a program to sort an array using menu driven:
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Merge Sort
Solution:
Code:
#include <stdio.h>
// Function to print the array
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Bubble Sort
void bubbleSort(int arr[], int size) {
for(int i = 0; i < size - 1; i++) {
for(int j = 0; j < size - 1 - i; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Selection Sort
void selectionSort(int arr[], int size) {
for(int i = 0; i < size - 1; i++) {
int minIdx = i;
for(int j = i + 1; j < size; j++) {
if(arr[j] < arr[minIdx]) {
minIdx = j;
}
}
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
// Insertion Sort
void insertionSort(int arr[], int size) {
for(int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
while(j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// Merge function for Merge Sort
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for(i = 0; i < n1; i++)
L[i] = arr[left + i];
for(j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while(i < n1 && j < n2) {
if(L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while(i < n1)
arr[k++] = L[i++];
while(j < n2)
arr[k++] = R[j++];
}
// Merge Sort
void mergeSort(int arr[], int left, int right) {
if(left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
int arr[100], size, choice;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for(int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
do {
printf("\nMenu:\n");
printf("1. Bubble Sort\n");
printf("2. Selection Sort\n");
printf("3. Insertion Sort\n");
printf("4. Merge Sort\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
int tempArr[100];
for(int i = 0; i < size; i++)
tempArr[i] = arr[i];
switch(choice) {
case 1:
bubbleSort(tempArr, size);
printf("Array after Bubble Sort: ");
printArray(tempArr, size);
break;
case 2:
selectionSort(tempArr, size);
printf("Array after Selection Sort: ");
printArray(tempArr, size);
break;
case 3:
insertionSort(tempArr, size);
printf("Array after Insertion Sort: ");
printArray(tempArr, size);
break;
case 4:
mergeSort(tempArr, 0, size - 1);
printf("Array after Merge Sort: ");
printArray(tempArr, size);
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while(choice != 5);
return 0;
}
Output:
Question7. WAP to implement a Singly Linked List.

Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertAtEnd(int value) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

printf("Node inserted.\n");
}
void deleteNode(int value) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
struct Node* prev = NULL;
if (temp->data == value) {
head = temp->next;
free(temp);
printf("Node deleted.\n");
return;
}
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found in list.\n");
return;
}
prev->next = temp->next;
free(temp);
printf("Node deleted.\n");
}
void displayList() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int choice, value;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
do {
printf("\nMenu:\n");
printf("1. Insert at End\n");
printf("2. Delete a Node\n");
printf("3. Display List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(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;
}

Output:
Question8. WAP to implement a Circular Linked Lists.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertAtEnd(int value) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
printf("Node inserted.\n");
}
void deleteNode(int value) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* current = head;
struct Node* previous = NULL;
if (head->data == value) {
struct Node* temp = head;
if (head->next == head) {
head = NULL;
} else {
while (temp->next != head) {
temp = temp->next;
}
temp->next = head->next;
head = head->next;
}
free(current);
printf("Node deleted.\n");
return;
}
do {
previous = current;
current = current->next;
if (current->data == value) {
previous->next = current->next;
free(current);
printf("Node deleted.\n");
return;
}
} while (current != head);
printf("Value not found in the list.\n");
}
void displayList() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
printf("Circular Linked List: ");
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to head)\n");
}
int main() {
int choice, value;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
do {
printf("\nMenu:\n");
printf("1. Insert at End\n");
printf("2. Delete a Node\n");
printf("3. Display List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(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;
}

Output:
Question9. WAP to implement Doubly Linked Lists.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head = NULL;
// Insert at end
void insertAtEnd(int value) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
newNode->prev = temp;
temp->next = newNode;
}
printf("Node inserted.\n");
}
// Delete a node with a specific value
void deleteNode(int value) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
// If the node to be deleted is the head
if (temp->data == value) {
head = temp->next;
if (head != NULL)
head->prev = NULL;
free(temp);
printf("Node deleted.\n");
return;
}
// For other nodes
while (temp != NULL && temp->data != value) {
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found.\n");
return;
}
if (temp->next != NULL)
temp->next->prev = temp->prev;
if (temp->prev != NULL)
temp->prev->next = temp->next;
free(temp);
printf("Node deleted.\n");
}
// Display list from start to end
void displayForward() {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
printf("Doubly Linked List (forward): ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Display list from end to start
void displayBackward() {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
printf("Doubly Linked List (backward): ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}
int main() {
int choice, value;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
do {
printf("\nMenu:\n");
printf("1. Insert at End\n");
printf("2. Delete a Node\n");
printf("3. Display Forward\n");
printf("4. Display Backward\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(value);
break;
case 3:
displayForward();
break;
case 4:
displayBackward();
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while(choice != 5);
return 0;
}
Output:
Question10. Write a menu driven program to implement
(i) Static Stack
(ii) Dynamic Stack.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
// Static Stack structure
struct StaticStack {
int* arr;
int top;
int capacity;
};
// Dynamic Stack structure
struct DynamicStack {
int* arr;
int top;
int capacity;
};
// Function prototypes for Static Stack
void initStaticStack(struct StaticStack* stack, int capacity);
int isFullStatic(struct StaticStack* stack);
int isEmptyStatic(struct StaticStack* stack);
void pushStatic(struct StaticStack* stack, int value);
int popStatic(struct StaticStack* stack);
void displayStatic(struct StaticStack* stack);
// Function prototypes for Dynamic Stack
void initDynamicStack(struct DynamicStack* stack, int capacity);
int isFullDynamic(struct DynamicStack* stack);
int isEmptyDynamic(struct DynamicStack* stack);
void pushDynamic(struct DynamicStack* stack, int value);
int popDynamic(struct DynamicStack* stack);
void displayDynamic(struct DynamicStack* stack);
int main() {
int choice, value, option, staticCapacity, dynamicCapacity;
struct StaticStack staticStack;
struct DynamicStack dynamicStack;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
do {
printf("\nMenu:\n");
printf("1. Static Stack\n");
printf("2. Dynamic Stack\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\nStatic Stack Operations:\n");
printf("Enter the capacity of Static Stack: ");
scanf("%d", &staticCapacity);
initStaticStack(&staticStack, staticCapacity);
do {
printf("\nStatic Stack Menu:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Back\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch(option) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
pushStatic(&staticStack, value);
break;
case 2:
value = popStatic(&staticStack);
if (value != -1)
printf("Popped value: %d\n", value);
break;
case 3:
displayStatic(&staticStack);
break;
case 4:
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while(option != 4);
break;
case 2:
printf("\nDynamic Stack Operations:\n");
printf("Enter the capacity of Dynamic Stack: ");
scanf("%d", &dynamicCapacity);
initDynamicStack(&dynamicStack, dynamicCapacity);
do {
printf("\nDynamic Stack Menu:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Back\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch(option) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
pushDynamic(&dynamicStack, value);
break;
case 2:
value = popDynamic(&dynamicStack);
if (value != -1)
printf("Popped value: %d\n", value);
break;
case 3:
displayDynamic(&dynamicStack);
break;
case 4:
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while(option != 4);
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while(choice != 3);
return 0;
}
// Static Stack functions
void initStaticStack(struct StaticStack* stack, int capacity) {
stack->capacity = capacity;
stack->top = -1;
stack->arr = (int*) malloc(capacity * sizeof(int));
}
int isFullStatic(struct StaticStack* stack) {
return stack->top == stack->capacity - 1;
}
int isEmptyStatic(struct StaticStack* stack) {
return stack->top == -1;
}
void pushStatic(struct StaticStack* stack, int value) {
if (isFullStatic(stack)) {
printf("Static stack overflow. Cannot push %d\n", value);
} else {
stack->arr[++stack->top] = value;
printf("Pushed %d onto Static Stack\n", value);
}
}
int popStatic(struct StaticStack* stack) {
if (isEmptyStatic(stack)) {
printf("Static stack underflow. Cannot pop\n");
return -1;
} else {
return stack->arr[stack->top--];
}
}
void displayStatic(struct StaticStack* stack) {
if (isEmptyStatic(stack)) {
printf("Static stack is empty.\n");
} else {
printf("Static Stack: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}
}
// Dynamic Stack functions
void initDynamicStack(struct DynamicStack* stack, int capacity) {
stack->capacity = capacity;
stack->top = -1;
stack->arr = (int*) malloc(capacity * sizeof(int));
}
int isFullDynamic(struct DynamicStack* stack) {
return stack->top == stack->capacity - 1;
}
int isEmptyDynamic(struct DynamicStack* stack) {
return stack->top == -1;
}
void pushDynamic(struct DynamicStack* stack, int value) {
if (isFullDynamic(stack)) {
printf("Dynamic stack overflow. Expanding stack size...\n");
stack->capacity *= 2;
stack->arr = realloc(stack->arr, stack->capacity * sizeof(int));
printf("Stack size expanded to %d.\n", stack->capacity);
}
stack->arr[++stack->top] = value;
printf("Pushed %d onto Dynamic Stack\n", value);
}
int popDynamic(struct DynamicStack* stack) {
if (isEmptyDynamic(stack)) {
printf("Dynamic stack underflow. Cannot pop\n");
return -1;
} else {
return stack->arr[stack->top--];
}
}
void displayDynamic(struct DynamicStack* stack) {
if (isEmptyDynamic(stack)) {
printf("Dynamic stack is empty.\n");
} else {
printf("Dynamic Stack: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}
}

Output:
Question11. WAP to implement postfix expressions using stack.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct Stack {
int* arr;
int top;
int capacity;
};
void initStack(struct Stack* stack, int capacity) {
stack->arr = (int*)malloc(capacity * sizeof(int));
stack->top = -1;
stack->capacity = capacity;
}
int isFull(struct Stack* stack) {
return stack->top == stack->capacity - 1;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow\n");
} else {
stack->arr[++stack->top] = value;
}
}
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1; // Error code
} else {
return stack->arr[stack->top--];
}
}
int evaluate(int op1, int op2, char operator) {
switch (operator) {
case '+':
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
default:
return 0;
}
}
int evaluatePostfix(char* expression, int capacity) {
struct Stack stack;
initStack(&stack, capacity);
int i = 0;
while (expression[i] != '\0') {
if (isdigit(expression[i])) {
push(&stack, expression[i] - '0');
} else {
int op2 = pop(&stack);
int op1 = pop(&stack);
int result = evaluate(op1, op2, expression[i]);
push(&stack, result);
} i++;
}
return pop(&stack);
}
int main() {
int capacity;
char expression[100];
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Enter stack capacity: ");
scanf("%d", &capacity);
printf("Enter a postfix expression: ");
scanf("%s", expression);
int result = evaluatePostfix(expression, capacity);
printf("Result of postfix evaluation: %d\n", result);
return 0;
}
Output:
Question12. WAP to implement an expression tree. (For example: (a + b / (c * d) – e) ).
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct Node {
char data;
struct Node* left;
struct Node* right;
} Node;
// Function to create a new tree node
Node* createNode(char data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Function to check if a character is an operator
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
// Function to construct an expression tree from a postfix expression
Node* constructExpressionTree(char* postfix) {
Node* stack[50]; // Stack to hold tree nodes
int top = -1;
// Loop through each character in postfix expression
for (int i = 0; postfix[i] != '\0'; i++) {
char c = postfix[i];
if (isOperator(c)) {
// Create a new node for the operator
Node* operatorNode = createNode(c);
// Pop two operands from stack
operatorNode->right = stack[top--];
operatorNode->left = stack[top--];
// Push the operator node back to the stack
stack[++top] = operatorNode;
} else {
// Create a new node for operand (variable)
Node* operandNode = createNode(c);
// Push the operand node to the stack
stack[++top] = operandNode;
}
}
return stack[top];
}
// Function to print the tree in inorder traversal
void inorderTraversal(Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%c ", root->data);
inorderTraversal(root->right);
}
}
// Function to print the tree in preorder traversal
void preorderTraversal(Node* root) {
if (root != NULL) {
printf("%c ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// Function to print the tree in postorder traversal
void postorderTraversal(Node* root) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%c ", root->data);
}
}
int main() {
char postfix[] = "abcd*+/e-"; // Postfix expression: (a + b / (c * d) - e)
Node* root = constructExpressionTree(postfix);
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
printf("Inorder Traversal of Expression Tree:\n");
inorderTraversal(root);
printf("\n");
printf("Preorder Traversal of Expression Tree:\n");
preorderTraversal(root);
printf("\n");
printf("Postorder Traversal of Expression Tree:\n");
postorderTraversal(root);
printf("\n");
return 0;
}
Output:
Question13. WAP to implement:
(i) Static Linear Queue
(ii) Dynamic Linear Queue
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
// Static Linear Queue Implementation (Array-based)
typedef struct {
int front, rear;
int* arr;
int capacity;
} StaticQueue;
void initStaticQueue(StaticQueue* q, int capacity) {
q->capacity = capacity;
q->arr = (int*)malloc(capacity * sizeof(int));
q->front = -1;
q->rear = -1;
}
int isFullStatic(StaticQueue* q) {
return q->rear == q->capacity - 1;
}
int isEmptyStatic(StaticQueue* q) {
return q->front == -1 || q->front > q->rear;
}
void enqueueStatic(StaticQueue* q, int value) {
if (isFullStatic(q)) {
printf("Static Queue is full\n");
return;
}
if (q->front == -1) {
q->front = 0; // first element
}
q->rear++;
q->arr[q->rear] = value;
printf("Enqueued %d to Static Queue\n", value);
}
int dequeueStatic(StaticQueue* q) {
if (isEmptyStatic(q)) {
printf("Static Queue is empty\n");
return -1;
}
int value = q->arr[q->front];
q->front++;
if (q->front > q->rear) { // Reset queue after last dequeue
q->front = q->rear = -1;
}
printf("Dequeued %d from Static Queue\n", value);
return value;
}
void displayStatic(StaticQueue* q) {
if (isEmptyStatic(q)) {
printf("Static Queue is empty\n");
return;
}
printf("Static Queue: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->arr[i]);
}
printf("\n");
}
// Dynamic Linear Queue Implementation (Linked List-based)
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct {
Node* front;
Node* rear;
} DynamicQueue;
void initDynamicQueue(DynamicQueue* q) {
q->front = q->rear = NULL;
}
int isEmptyDynamic(DynamicQueue* q) {
return q->front == NULL;
}
void enqueueDynamic(DynamicQueue* q, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
printf("Enqueued %d to Dynamic Queue\n", value);
}
int dequeueDynamic(DynamicQueue* q) {
if (isEmptyDynamic(q)) {
printf("Dynamic Queue is empty\n");
return -1;
}
Node* temp = q->front;
int value = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL; // Queue is empty now
}
free(temp);
printf("Dequeued %d from Dynamic Queue\n", value);
return value;
}
void displayDynamic(DynamicQueue* q) {
if (isEmptyDynamic(q)) {
printf("Dynamic Queue is empty\n");
return;
}
Node* temp = q->front;
printf("Dynamic Queue: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int staticCapacity, choice, value;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
// Static Queue
printf("Enter capacity for Static Queue: ");
scanf("%d", &staticCapacity);
StaticQueue staticQueue;
initStaticQueue(&staticQueue, staticCapacity);
// Dynamic Queue
DynamicQueue dynamicQueue;
initDynamicQueue(&dynamicQueue);
do {
printf("\nMenu:\n");
printf("1. Static Queue Enqueue\n");
printf("2. Static Queue Dequeue\n");
printf("3. Static Queue Display\n");
printf("4. Dynamic Queue Enqueue\n");
printf("5. Dynamic Queue Dequeue\n");
printf("6. Dynamic Queue Display\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to enqueue in Static Queue: ");
scanf("%d", &value);
enqueueStatic(&staticQueue, value);
break;
case 2:
dequeueStatic(&staticQueue);
break;
case 3:
displayStatic(&staticQueue);
break;
case 4:
printf("Enter value to enqueue in Dynamic Queue: ");
scanf("%d", &value);
enqueueDynamic(&dynamicQueue, value);
break;
case 5:
dequeueDynamic(&dynamicQueue);
break;
case 6:
displayDynamic(&dynamicQueue);
break;
case 7:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while(choice != 7);
free(staticQueue.arr);
return 0;
}
Output:
Question14. WAP to implement:
(i) Static Circular Queue
(ii) Dynamic Circular Queue
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
// Static Circular Queue Implementation (Array-based)
typedef struct {
int front, rear;
int* arr;
int capacity;
} StaticCircularQueue;
void initStaticCircularQueue(StaticCircularQueue* q, int capacity) {
q->capacity = capacity;
q->arr = (int*)malloc(capacity * sizeof(int));
q->front = q->rear = -1;
}
int isFullStatic(StaticCircularQueue* q) {
return (q->rear + 1) % q->capacity == q->front;
}
int isEmptyStatic(StaticCircularQueue* q) {
return q->front == -1;
}
void enqueueStatic(StaticCircularQueue* q, int value) {
if (isFullStatic(q)) {
printf("Static Circular Queue is full\n");
return;
}
if (q->front == -1) {
q->front = 0;
}
q->rear = (q->rear + 1) % q->capacity;
q->arr[q->rear] = value;
printf("Enqueued %d to Static Circular Queue\n", value);
}
int dequeueStatic(StaticCircularQueue* q) {
if (isEmptyStatic(q)) {
printf("Static Circular Queue is empty\n");
return -1;
}
int value = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front = (q->front + 1) % q->capacity;
}
printf("Dequeued %d from Static Circular Queue\n", value);
return value;
}
void displayStatic(StaticCircularQueue* q) {
if (isEmptyStatic(q)) {
printf("Static Circular Queue is empty\n");
return;
}
int i = q->front;
printf("Static Circular Queue: ");
while (i != q->rear) {
printf("%d ", q->arr[i]);
i = (i + 1) % q->capacity;
}
printf("%d\n", q->arr[q->rear]);
}
// Dynamic Circular Queue Implementation (Linked List-based)
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct {
Node* front;
Node* rear;
} DynamicCircularQueue;
void initDynamicCircularQueue(DynamicCircularQueue* q) {
q->front = q->rear = NULL;
}
int isEmptyDynamic(DynamicCircularQueue* q) {
return q->front == NULL;
}
void enqueueDynamic(DynamicCircularQueue* q, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (isEmptyDynamic(q)) {
q->front = q->rear = newNode;
q->rear->next = q->front;
} else {
q->rear->next = newNode;
q->rear = newNode;
q->rear->next = q->front;
}
printf("Enqueued %d to Dynamic Circular Queue\n", value);
}
int dequeueDynamic(DynamicCircularQueue* q) {
if (isEmptyDynamic(q)) {
printf("Dynamic Circular Queue is empty\n");
return -1;
}
Node* temp = q->front;
int value = temp->data;
if (q->front == q->rear) {
q->front = q->rear = NULL;
} else {
q->front = q->front->next;
q->rear->next = q->front;
}
free(temp);
printf("Dequeued %d from Dynamic Circular Queue\n", value);
return value;
}
void displayDynamic(DynamicCircularQueue* q) {
if (isEmptyDynamic(q)) {
printf("Dynamic Circular Queue is empty\n");
return;
}
Node* temp = q->front;
printf("Dynamic Circular Queue: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != q->front);
printf("\n");
}
int main() {
int staticCapacity, choice, value;
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
// Static Circular Queue
printf("Enter capacity for Static Circular Queue: ");
scanf("%d", &staticCapacity);
StaticCircularQueue staticQueue;
initStaticCircularQueue(&staticQueue, staticCapacity);
// Dynamic Circular Queue
DynamicCircularQueue dynamicQueue;
initDynamicCircularQueue(&dynamicQueue);
do {
printf("\nMenu:\n");
printf("1. Static Circular Queue Enqueue\n");
printf("2. Static Circular Queue Dequeue\n");
printf("3. Static Circular Queue Display\n");
printf("4. Dynamic Circular Queue Enqueue\n");
printf("5. Dynamic Circular Queue Dequeue\n");
printf("6. Dynamic Circular Queue Display\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to enqueue in Static Circular Queue: ");
scanf("%d", &value);
enqueueStatic(&staticQueue, value);
break;
case 2:
dequeueStatic(&staticQueue);
break;
case 3:
displayStatic(&staticQueue);
break;
case 4:
printf("Enter value to enqueue in Dynamic Circular Queue: ");
scanf("%d", &value);
enqueueDynamic(&dynamicQueue, value);
break;
case 5:
dequeueDynamic(&dynamicQueue);
break;
case 6:
displayDynamic(&dynamicQueue);
break;
case 7:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while(choice != 7);
// Free dynamically allocated memory for static queue
free(staticQueue.arr);
return 0;
}
Output:
Question15. WAP to implement recursive algorithms for BST traversal- Inorder, Preorder,
Postorder.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* 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 root is NULL, create a new node and return
}
if (data < root->data) {
root->left = insert(root->left, data); // Recur on the left subtree
} else {
root->right = insert(root->right, data); // Recur on the right subtree
}
return root; // Return the (unchanged) node pointer
}
// Inorder Traversal (Left, Root, Right)
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
// Preorder Traversal (Root, Left, Right)
void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
// Postorder Traversal (Left, Right, Root)
void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
printf("Name: Suhani Dixit\n");
printf("Enrolment No.: 04413702024\n");
// Perform Inorder Traversal
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
// Perform Preorder Traversal
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
// Perform Postorder Traversal
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
return 0;
}
Output:

You might also like