0% found this document useful (0 votes)
11 views40 pages

DSA Practical File (Nizamuddin Sameer)

The document outlines a series of programming assignments for a Data Structure and Algorithm course at Guru Gobind Singh Indraprastha University. It includes various tasks such as implementing operations on arrays, sorting algorithms, and linked lists using C programming. The document provides code examples and instructions for each assignment, demonstrating fundamental data structure concepts.

Uploaded by

mdjamaluddin9123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views40 pages

DSA Practical File (Nizamuddin Sameer)

The document outlines a series of programming assignments for a Data Structure and Algorithm course at Guru Gobind Singh Indraprastha University. It includes various tasks such as implementing operations on arrays, sorting algorithms, and linked lists using C programming. The document provides code examples and instructions for each assignment, demonstrating fundamental data structure concepts.

Uploaded by

mdjamaluddin9123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

GURU GOBIND SINGH INDRAPRASTHA

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 insert(int arr[], int *size, int pos, int value) {


if (*size >= MAX) {
printf("Array full!\n");
return;
}
for (int i = *size; i >= pos; i--)
arr[i] = arr[i-1];
arr[pos-1] = value;
(*size)++;
}

void delete(int arr[], int *size, int pos) {


if (*size == 0) {
printf("Array empty!\n");
return;
}
for (int i = pos-1; i < *size-1; i++)
arr[i] = arr[i+1];
(*size)--;
}

void traverse(int arr[], int size) {


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

void reverse(int arr[], int size) {


for (int i = 0; i < size/2; i++) {
int temp = arr[i];
arr[i] = arr[size-1-i];
arr[size-1-i] = temp;
}
}

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];

printf("Original array: ");


traverse(arr, size);

insert(arr, &size, 2, 10);


printf("After insertion: ");
traverse(arr, size);

delete(arr, &size, 3);


printf("After deletion: ");
traverse(arr, size);

reverse(arr, size);
printf("After reverse: ");
traverse(arr, size);

merge(arr, size, arr2, size2, result);


printf("Merged array: ");
traverse(result, size + size2);

return 0;
}

Output:

Question 2: WAP to sort an array using menu


driven: (i) Bubble Sort (ii) Merge Sort (iii)
Insertion Sort (iv) Selection Sort .
Code:
#include <stdio.h>
#define MAX 100

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int i = 0; i < n2; i++) R[i] = arr[m + 1 + i];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; 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;
}
}

void selectionSort(int arr[], int n) {


for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

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");
}

// Insert at specific position


void insertAtPosition(int data, int position) {
if (position < 1) {
printf("Invalid position.\n");
return;
}

if (position == 1) {
insertAtBeginning(data);
return;
}

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = data;

struct Node* temp = head;


for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}

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);
}
}

// Delete from beginning


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


head = head->next;
free(temp);
printf("Node deleted from beginning.\n");
}

// Delete from end


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

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");
}

// Delete from specific position


void deleteFromPosition(int position) {
if (head == NULL) {
printf("List is empty.\n");
return;
}

if (position < 1) {
printf("Invalid position.\n");
return;
}

if (position == 1) {
deleteFromBeginning();
return;
}

struct Node* temp = head;


for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}

if (temp == NULL || temp->next == NULL) {


printf("Position out of range.\n");
} else {
struct Node* toDelete = temp->next;
temp->next = toDelete->next;
free(toDelete);
printf("Node deleted from position %d.\n", position);
}
}

// Display the list


void display() {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}

printf("Linked List: ");


while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

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;
};

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--- 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;
}

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = data;

struct Node* temp = head;


for (int i = 1; i < position - 1 && temp->next != head; i++) {
temp = temp->next;
}

if (temp == NULL || temp->next == 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);
}
}

// Delete from beginning


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


if (head->next == head) {
head = NULL;
} else {
struct Node* last = head;
while (last->next != head)
last = last->next;

head = head->next;
last->next = head;
}
free(temp);
printf("Node deleted from beginning.\n");
}

// Delete from end


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


if (head->next == head) {
free(head);
head = NULL;
} else {
struct Node* prev = NULL;
while (temp->next != head) {
prev = temp;
temp = temp->next;
}
prev->next = head;
free(temp);
}
printf("Node deleted from end.\n");
}

// Delete from a specific position


void deleteFromPosition(int position) {
if (head == NULL) {
printf("List is empty.\n");
return;
}

if (position < 1) {
printf("Invalid position.\n");
return;
}

if (position == 1) {
deleteFromBeginning();
return;
}

struct Node* temp = head;


struct Node* prev = NULL;

for (int i = 1; i < position && temp->next != head; i++) {


prev = temp;
temp = temp->next;
}

if (temp->next == head && position > 1) {


printf("Position out of range.\n");
} else {
prev->next = temp->next;
free(temp);
printf("Node deleted from position %d.\n", position);
}
}

// Display the list


void display() {
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("(head)\n");
}
Output:
Question 5: WAP to implement Doubly Linked List.
Code:
#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};

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--- 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");
}

// Insert at specific position


void insertAtPosition(int data, int position) {
if (position < 1) {
printf("Invalid position.\n");
return;
}

if (position == 1) {
insertAtBeginning(data);
return;
}

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = data;

struct Node* temp = head;


for (int i = 1; i < position - 1 && temp != NULL; i++)
temp = temp->next;

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);
}

// Delete from beginning


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


head = head->next;

if (head != NULL)
head->prev = NULL;

free(temp);
printf("Node deleted from beginning.\n");
}

// Delete from end


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


if (temp->next == NULL) {
head = NULL;
} else {
while (temp->next != NULL)
temp = temp->next;

temp->prev->next = NULL;
}
free(temp);
printf("Node deleted from end.\n");
}

// Delete from a specific position


void deleteFromPosition(int position) {
if (head == NULL || position < 1) {
printf("Invalid operation.\n");
return;
}

if (position == 1) {
deleteFromBeginning();
return;
}

struct Node* temp = head;


for (int i = 1; i < position && temp != NULL; i++)
temp = temp->next;

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);
}

// Display the list


void display() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Output:
Question 6: Write a menu driven program to implement a (i)
Static (ii) Dynamic Stack.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5

// Static Stack
int staticStack[MAX], top = -1;

void pushStatic(int data) {


if (top == MAX-1) {
printf("Stack Overflow\n");
return;
}
staticStack[++top] = data;
}

int popStatic() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return staticStack[top--];
}

// Dynamic Stack
struct Node {
int data;
struct Node* next;
};

struct Node* topDynamic = NULL;

void pushDynamic(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = topDynamic;
topDynamic = newNode;
}

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

// Static Circular Queue


int staticQueue[MAX], front = -1, rear = -1;

void enqueueStatic(int data) {


if ((rear + 1) % MAX == front) {
printf("Queue Full\n");
return;
}
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
staticQueue[rear] = data;
}

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;
}

// Dynamic Circular Queue


struct Node {
int data;
struct Node* next;
};

struct Node *frontDynamic = NULL, *rearDynamic = NULL;

void enqueueDynamic(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (rearDynamic == NULL) {
frontDynamic = rearDynamic = newNode;
newNode->next = frontDynamic;
} else {
rearDynamic->next = newNode;
rearDynamic = newNode;
rearDynamic->next = frontDynamic;
}
}

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;

void insertFrontStatic(int data) {


if ((front == 0 && rear == MAX-1) || (front == rear + 1)) {
printf("Deque Full\n");
return;
}
if (front == -1) front = rear = 0;
else if (front == 0) front = MAX-1;
else front--;
staticDeque[front] = data;
}

void insertRearStatic(int data) {


if ((front == 0 && rear == MAX-1) || (front == rear + 1)) {
printf("Deque Full\n");
return;
}
if (rear == -1) front = rear = 0;
else if (rear == MAX-1) rear = 0;
else rear++;
staticDeque[rear] = data;
}

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;
};

struct Node *frontDynamic = NULL, *rearDynamic = NULL;

void insertFrontDynamic(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = frontDynamic;
if (frontDynamic == NULL) {
frontDynamic = rearDynamic = newNode;
} else {
frontDynamic->prev = newNode;
frontDynamic = newNode;
}
}

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;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int data) {


if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}

struct Node* search(struct Node* root, int key) {


if (root == NULL || root->data == key) return root;
if (key < root->data) return search(root->left, key);
return search(root->right, key);
}

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;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int data) {


if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}

void inorder(struct Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

void preorder(struct Node* root) {


if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

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, 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>

void linearSearch(int arr[], int size, int target);


void binarySearch(int arr[], int size, int target);
void sortArray(int arr[], int size); // for binary search

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;

printf("Enter the element to search: ");


scanf("%d", &target);

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;

while (low <= high) {


mid = (low + high) / 2;

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;
}

printf("Element %d not found in the array.\n", target);


}

// Sort Array using Bubble Sort


void sortArray(int arr[], int size) {
int temp;
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
// swap
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

printf("Sorted Array for Binary Search: ");


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
Output:
Question 12: WAP to accept a matrix from user,
find out matrix is sparse or not and convert
into triplex matrix.
Code:
#include <stdio.h>
#define MAX 100

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("Matrix is %s\n", (count <= (rows * cols) / 3) ? "Sparse" : "Not


Sparse");

int triplex[count + 1][3], k = 1;


triplex[0][0] = rows;
triplex[0][1] = cols;
triplex[0][2] = count;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (matrix[i][j] != 0) {
triplex[k][0] = i;
triplex[k][1] = j;
triplex[k][2] = matrix[i][j];
k++;
}

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()

#define MAX 100

// Stack Utility
int stack[MAX];
int top = -1;

void push(int value) {


if (top >= MAX - 1)
printf("Stack Overflow\n");
else
stack[++top] = value;
}

int pop() {
if (top < 0)
printf("Stack Underflow\n");
else
return stack[top--];
}

// ---------- Postfix Evaluation ----------


int evaluatePostfix(char* exp) {
top = -1; // Reset stack
for (int i = 0; exp[i]; i++) {
if (isdigit(exp[i])) {
push(exp[i] - '0'); // convert char to int
} else {
int b = pop();
int a = pop();
switch (exp[i]) {
case '+': push(a + b); break;
case '-': push(a - b); break;
case '*': push(a * b); break;
case '/': push(a / b); break;
}
}
}
return pop();
}

// ---------- Prefix Evaluation ----------


int evaluatePrefix(char* exp) {
top = -1; // Reset stack
int len = strlen(exp);
for (int i = len - 1; i >= 0; i--) {
if (isdigit(exp[i])) {
push(exp[i] - '0');
} else {
int a = pop();
int b = pop();
switch (exp[i]) {
case '+': push(a + b); break;
case '-': push(a - b); break;
case '*': push(a * b); break;
case '/': push(a / b); break;
}
}
}
return pop();
}

// Main Function
int main() {
char postfix[] = "231*+9-";
char prefix[] = "-+*2319";

printf("Postfix Expression: %s\n", postfix);


printf("Postfix Result: %d\n", evaluatePostfix(postfix));

printf("\nPrefix Expression: %s\n", prefix);


printf("Prefix Result: %d\n", evaluatePrefix(prefix));

return 0;
}
Output:
Question 14: Write a program to evaluate
Prefix Expression Postfix Expression
using stack.
Code:
#include <stdio.h>
#include <string.h>

int stack[100], top = -1;

void push(int data) {


stack[++top] = data;
}

int pop() {
return stack[top--];
}

int evaluatePrefix(char* exp) {


int len = strlen(exp);
for (int i = len-1; i >= 0; i--) {
if (exp[i] >= '0' && exp[i] <= '9')
push(exp[i] - '0');
else {
int a = pop();
int b = pop();
switch (exp[i]) {
case '+': push(a + b); break;
case '-': push(a - b); break;
case '*': push(a * b); break;
case '/': push(a / b); break;
}
}
}
return pop();
}

int evaluatePostfix(char* exp) {


for (int i = 0; exp[i]; i++) {
if (exp[i] >= '0' && exp[i] <= '9')
push(exp[i] - '0');
else {
int b = pop();
int a = pop();
switch (exp[i]) {
case '+': push(a + b); break;
case '-': push(a - b); break;
case '*': push(a * b); break;
case '/': push(a / b); break;
}
}
}
return pop();
}

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:

Question 15: WAP to implement an expression


tree.
Code:
#include <stdio.h>
#include <stdlib.h>

struct Node {
char data;
struct Node *left, *right;
};

struct Node* createNode(char data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

void inorder(struct Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%c ", root->data);
inorder(root->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:

You might also like