DSA Practical File (Nizamuddin Sameer)
DSA Practical File (Nizamuddin Sameer)
UNIVERSITY
INSTITUTE OF INNOVATION IN TECHNOLOGY & MANAGEMENT
DATA STRUCTURE AND ALGORITHM USING C
BCA-106P
IITM
Submitted To:
Prof. TANIYA DABAS
Professor
Submitted By:
Name: Nizamuddin Sameer
Class: BCA II (E1)
SEMESTER:2
Enrolment no:05824402024
S.No. Program Description Date Signature
1 WAP to implement
operations on one-
dimensional array:
(i) Insertion
(ii) Deletion
(iii) Traversal
(iv) Reverse
(v) Merge
2 WAP to sort an array
using menu-driven
approach:
(i) Bubble Sort
(ii) Merge Sort
(iii) Insertion Sort
(iv) Selection Sort
3 WAP to implement
Singly Linked List.
4 WAP to implement
Circular Linked List.
5 WAP to implement
Doubly Linked List.
6 Menu-driven program to
implement:
(i) Static Stack
(ii) Dynamic Stack
7 WAP to implement:
(i) Static Circular Queue
(ii) Dynamic Circular
Queue
8 WAP to implement:
(i) Static De-Queue
(ii) Dynamic De-Queue
9 Recursive algorithms for
Binary Search Tree:
(i) Insertion
(ii) Searching
10 Recursive BST traversal:
(i) Inorder
(ii) Preorder
(iii) Postorder
11 WAP to search and
display the location of an
element in an array:
(i) Linear Search
(ii) Binary Search
12 WAP to accept a matrix,
check if it is sparse, and
convert it into a triplex
matrix.
13 Program to evaluate:
(i) Prefix Expression
(ii) Postfix Expression
14 Program to evaluate
Prefix and Postfix
expressions using stack.
15 WAP to implement an
Expression Tree.
Question 1
: WAP to implement following operation on one
dimensional array (i) Insertion (ii) Deletion
(iii) Traversal (iv) Reverse (v) Merge.
Code:
#include <stdio.h>
#define MAX 100
void merge(int arr1[], int size1, int arr2[], int size2, int result[]) {
int i = 0, j = 0, k = 0;
while (i < size1 && j < size2) {
if (arr1[i] <= arr2[j])
result[k++] = arr1[i++];
else
result[k++] = arr2[j++];
}
while (i < size1)
result[k++] = arr1[i++];
while (j < size2)
result[k++] = arr2[j++];
}
int main() {
int arr[MAX] = {1, 2, 3, 4}, size = 4;
int arr2[] = {5, 6, 7}, size2 = 3;
int result[MAX];
reverse(arr, size);
printf("After reverse: ");
traverse(arr, size);
return 0;
}
Output:
int main() {
int arr[MAX], n, choice;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
do {
printf("\nMenu:\n1. Bubble Sort\n2. Merge Sort\n3. Insertion Sort\n4.
Selection Sort\n5. Exit\nChoice: ");
scanf("%d", &choice);
int temp[MAX];
for (int i = 0; i < n; i++) temp[i] = arr[i];
switch (choice) {
case 1: bubbleSort(temp, n); break;
case 2: mergeSort(temp, 0, n-1); break;
case 3: insertionSort(temp, n); break;
case 4: selectionSort(temp, n); break;
case 5: return 0;
}
printf("Sorted array: ");
printArray(temp, n);
} while (1);
}
Output:
Question 3: WAP to implement Singly Linked
List.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Head pointer
struct Node* head = NULL;
// Function prototypes
void insertAtBeginning(int data);
void insertAtEnd(int data);
void insertAtPosition(int data, int position);
void deleteFromBeginning();
void deleteFromEnd();
void deleteFromPosition(int position);
void display();
int main() {
int choice, data, position;
while (1) {
printf("\n--- Singly Linked List Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete from Position\n");
printf("7. Display List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter position (starting from 1): ");
scanf("%d", &position);
insertAtPosition(data, position);
break;
case 4:
deleteFromBeginning();
break;
case 5:
deleteFromEnd();
break;
case 6:
printf("Enter position to delete (starting from 1): ");
scanf("%d", &position);
deleteFromPosition(position);
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
// Insert at beginning
void insertAtBeginning(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
head = newNode;
printf("Node inserted at beginning.\n");
}
// Insert at end
void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
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 at end.\n");
}
if (position == 1) {
insertAtBeginning(data);
return;
}
if (temp == NULL) {
printf("Position out of range.\n");
free(newNode);
} else {
newNode->next = temp->next;
temp->next = newNode;
printf("Node inserted at position %d.\n", position);
}
}
if (head->next == NULL) {
free(head);
head = NULL;
} else {
struct Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
printf("Node deleted from end.\n");
}
if (position < 1) {
printf("Invalid position.\n");
return;
}
if (position == 1) {
deleteFromBeginning();
return;
}
Output:
Question 4: WAP to implement a circular linked list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Function prototypes
void insertAtBeginning(int data);
void insertAtEnd(int data);
void insertAtPosition(int data, int position);
void deleteFromBeginning();
void deleteFromEnd();
void deleteFromPosition(int position);
void display();
int main() {
int choice, data, position;
while (1) {
printf("\n--- Circular Linked List Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete from Position\n");
printf("7. Display List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
printf("Enter data: ");
scanf("%d", &data);
printf("Enter position (starting from 1): ");
scanf("%d", &position);
insertAtPosition(data, position);
break;
case 4:
deleteFromBeginning();
break;
case 5:
deleteFromEnd();
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteFromPosition(position);
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
// Insert at beginning
void insertAtBeginning(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (head == NULL) {
newNode->next = newNode;
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != head)
temp = temp->next;
newNode->next = head;
temp->next = newNode;
head = newNode;
}
printf("Node inserted at beginning.\n");
}
// Insert at end
void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (head == NULL) {
newNode->next = newNode;
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
printf("Node inserted at end.\n");
}
// Insert at a specific position
void insertAtPosition(int data, int position) {
if (position < 1) {
printf("Invalid position.\n");
return;
}
if (position == 1) {
insertAtBeginning(data);
return;
}
head = head->next;
last->next = head;
}
free(temp);
printf("Node deleted from beginning.\n");
}
if (position < 1) {
printf("Invalid position.\n");
return;
}
if (position == 1) {
deleteFromBeginning();
return;
}
// Node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function prototypes
void insertAtBeginning(int data);
void insertAtEnd(int data);
void insertAtPosition(int data, int position);
void deleteFromBeginning();
void deleteFromEnd();
void deleteFromPosition(int position);
void display();
int main() {
int choice, data, position;
while (1) {
printf("\n--- Doubly Linked List Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete from Position\n");
printf("7. Display List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
printf("Enter data: ");
scanf("%d", &data);
printf("Enter position (starting from 1): ");
scanf("%d", &position);
insertAtPosition(data, position);
break;
case 4:
deleteFromBeginning();
break;
case 5:
deleteFromEnd();
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteFromPosition(position);
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
// Insert at beginning
void insertAtBeginning(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;
if (head != NULL)
head->prev = newNode;
head = newNode;
printf("Node inserted at beginning.\n");
}
// Insert at end
void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
printf("Node inserted at end.\n");
}
if (position == 1) {
insertAtBeginning(data);
return;
}
if (temp == NULL) {
printf("Position out of range.\n");
free(newNode);
return;
}
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
printf("Node inserted at position %d.\n", position);
}
if (head != NULL)
head->prev = NULL;
free(temp);
printf("Node deleted from beginning.\n");
}
temp->prev->next = NULL;
}
free(temp);
printf("Node deleted from end.\n");
}
if (position == 1) {
deleteFromBeginning();
return;
}
if (temp == NULL) {
printf("Position out of range.\n");
return;
}
if (temp->prev != NULL)
temp->prev->next = temp->next;
if (temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
printf("Node deleted from position %d.\n", position);
}
// Static Stack
int staticStack[MAX], top = -1;
int popStatic() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return staticStack[top--];
}
// Dynamic Stack
struct Node {
int data;
struct Node* next;
};
int popDynamic() {
if (topDynamic == NULL) {
printf("Stack Underflow\n");
return -1;
}
struct Node* temp = topDynamic;
int data = temp->data;
topDynamic = topDynamic->next;
free(temp);
return data;
}
int main() {
int choice, data, type;
do {
printf("\n1. Static Stack\n2. Dynamic Stack\n3. Exit\nChoose stack type:
");
scanf("%d", &type);
if (type == 3) break;
printf("1. Push\n2. Pop\n3. Exit\nChoice: ");
scanf("%d", &choice);
if (choice == 3) break;
if (choice == 1) {
printf("Enter data: ");
scanf("%d", &data);
if (type == 1) pushStatic(data);
else pushDynamic(data);
} else if (choice == 2) {
int result = (type == 1) ? popStatic() : popDynamic();
if (result != -1) printf("Popped: %d\n", result);
}
} while (1);
return 0;
}
Output:
Question 7: WAP to implement a (i) Static (ii)
Dynamic Circular queue.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int dequeueStatic() {
if (front == -1) {
printf("Queue Empty\n");
return -1;
}
int data = staticQueue[front];
if (front == rear) front = rear = -1;
else front = (front + 1) % MAX;
return data;
}
int dequeueDynamic() {
if (frontDynamic == NULL) {
printf("Queue Empty\n");
return -1;
}
struct Node* temp = frontDynamic;
int data = temp->data;
if (frontDynamic == rearDynamic) {
frontDynamic = rearDynamic = NULL;
} else {
frontDynamic = frontDynamic->next;
rearDynamic->next = frontDynamic;
}
free(temp);
return data;
}
int main() {
enqueueStatic(1);
enqueueStatic(2);
printf("Static Dequeue: %d\n", dequeueStatic());
enqueueDynamic(3);
enqueueDynamic(4);
printf("Dynamic Dequeue: %d\n", dequeueDynamic());
return 0;
}
Output:
Question 8: WAP to implement (i) Static (ii)
Dynamic De-Queue.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Static Deque
int staticDeque[MAX], front = -1, rear = -1;
int deleteFrontStatic() {
if (front == -1) {
printf("Deque Empty\n");
return -1;
}
int data = staticDeque[front];
if (front == rear) front = rear = -1;
else if (front == MAX-1) front = 0;
else front++;
return data;
}
// Dynamic Deque
struct Node {
int data;
struct Node *prev, *next;
};
int deleteFrontDynamic() {
if (frontDynamic == NULL) {
printf("Deque Empty\n");
return -1;
}
struct Node* temp = frontDynamic;
int data = temp->data;
frontDynamic = frontDynamic->next;
if (frontDynamic == NULL) rearDynamic = NULL;
else frontDynamic->prev = NULL;
free(temp);
return data;
}
int main() {
insertFrontStatic(1);
insertRearStatic(2);
printf("Static Deque Delete Front: %d\n", deleteFrontStatic());
insertFrontDynamic(3);
printf("Dynamic Deque Delete Front: %d\n", deleteFrontDynamic());
return 0;
}
Output:
Question 9: Implement recursive algorithms for
the following operations on Binary Search Tree.
(i) Insertion (ii) Searching
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
printf("Insert 50, 30, 70\n");
if (search(root, 30))
printf("30 found in BST\n");
else
printf("30 not found\n");
return 0;
}
Output:
Question 10: Implement recursive algorithms for
BST traversal- Inorder, Preorder, Postorder.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
printf("Inorder: ");
inorder(root);
printf("\nPreorder: ");
preorder(root);
printf("\nPostorder: ");
postorder(root);
printf("\n");
return 0;
}
Output:
Question 11: WAP to search and display the
location of an element specified by the user,
in an array using (i) Linear Search (ii) Binary
Search technique.
Code:
#include <stdio.h>
int main() {
// Random predefined array
int arr[] = {23, 5, 78, 12, 9, 56, 34, 1, 99, 45};
int n = sizeof(arr) / sizeof(arr[0]);
int choice, target;
printf("Array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
while (1) {
printf("\n--- Search Menu ---\n");
printf("1. Linear Search\n");
printf("2. Binary Search\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 3)
break;
switch (choice) {
case 1:
linearSearch(arr, n, target);
break;
case 2:
sortArray(arr, n); // Binary search needs sorted array
binarySearch(arr, n, target);
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}
// Linear Search
void linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
printf("Element %d found at position %d (index %d).\n", target,
i + 1, i);
return;
}
}
printf("Element %d not found in the array.\n", target);
}
// Binary Search
void binarySearch(int arr[], int size, int target) {
int low = 0, high = size - 1, mid;
if (arr[mid] == target) {
printf("Element %d found at position %d (index %d).\n", target,
mid + 1, mid);
return;
} else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
int main() {
int matrix[MAX][MAX], rows, cols, count = 0;
printf("Enter rows and columns: ");
scanf("%d %d", &rows, &cols);
printf("Enter matrix elements:\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) count++;
}
printf("Triplex Matrix:\n");
for (int i = 0; i <= count; i++)
printf("%d %d %d\n", triplex[i][0], triplex[i][1], triplex[i][2]);
return 0;
}
Output:
Question 13: Write a program to evaluate Prefix Expression
Postfix Expression
Code:
#include <stdio.h>
#include <ctype.h> // for isdigit()
#include <string.h> // for strlen()
// Stack Utility
int stack[MAX];
int top = -1;
int pop() {
if (top < 0)
printf("Stack Underflow\n");
else
return stack[top--];
}
// Main Function
int main() {
char postfix[] = "231*+9-";
char prefix[] = "-+*2319";
return 0;
}
Output:
Question 14: Write a program to evaluate
Prefix Expression Postfix Expression
using stack.
Code:
#include <stdio.h>
#include <string.h>
int pop() {
return stack[top--];
}
int main() {
char prefix[] = "*+23-51";
char postfix[] = "231*+9-";
printf("Prefix Expression: %s\n", prefix);
printf("Result: %d\n", evaluatePrefix(prefix));
printf("Postfix Expression: %s\n", postfix);
printf("Result: %d\n", evaluatePostfix(postfix));
return 0;
}
Output:
struct Node {
char data;
struct Node *left, *right;
};
int main() {
struct Node* root = createNode('+');
root->left = createNode('*');
root->right = createNode('3');
root->left->left = createNode('2');
root->left->right = createNode('4');
printf("Inorder traversal of expression tree: ");
inorder(root);
printf("\n");
return 0;
}
Output: