You are on page 1of 81

Q1.

WAP to implement following operation on one dimensional array (i)

Insertion (ii) Deletion (iii) Traversal (iv) Reverse (v) Merge

SOLUTION

#include <stdio.h>

void insertElement(int arr[], int size, int index, int element) {

for (int i = size - 1; i >= index; i--) {

arr[i + 1] = arr[i];

arr[index] = element;

printf("Element inserted successfully.\n");

void deleteElement(int arr[], int size, int index) {

for (int i = index; i < size - 1; i++) {

arr[i] = arr[i + 1];

printf("Element deleted successfully.\n");

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

printf("Array elements: ");

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

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

int start = 0;

int end = size - 1;

while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;
start++;

end--;

printf("Array reversed successfully.\n");

void mergeArrays(int arr1[], int size1, int arr2[], int size2, int merged[]) {

int i, j, k;

i = j = k = 0;

while (i < size1 && j < size2) {

if (arr1[i] < arr2[j]) {

merged[k] = arr1[i];

i++;

} else {

merged[k] = arr2[j];

j++;

k++;

while (i < size1) {

merged[k] = arr1[i];

i++;

k++;

while (j < size2) {

merged[k] = arr2[j];

j++;

k++;

printf("Arrays merged successfully.\n");

}
int main() {

int arr1[5] = {2, 4, 6, 8, 10};

int size1 = 5;

int arr2[5] = {1, 3, 5, 7, 9};

int size2 = 5;

int merged[10];

insertElement(arr1, size1, 3, 20);

deleteElement(arr1, size1, 2);

traverseArray(arr1, size1);

reverseArray(arr1, size1);

mergeArrays(arr1, size1, arr2, size2, merged);

return 0;

}
OUTPUT
2. WAP to Sort an array using menu driven: (i) BUBBLE SORT (ii) MERGE SORT(iii) INSERTION SORT
(iv) SELECTION SORT

SOLUTION

#include <stdio.h>

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

int i, j;

for (i = 0; i < size - 1; i++) {

for (j = 0; j < size - 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 left, int mid, int right) {

int i, j, k;

int size1 = mid - left + 1;

int size2 = right - mid;

int L[size1], R[size2];

for (i = 0; i < size1; i++) {

L[i] = arr[left + i];

for (j = 0; j < size2; j++) {

R[j] = arr[mid + 1 + j];


}

i = 0;

j = 0;

k = left;

while (i < size1 && j < size2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

while (i < size1) {

arr[k] = L[i];

i++;

k++;

while (j < size2) {

arr[k] = R[j];

j++;

k++;

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

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

int i, key, j;

for (i = 1; i < size; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

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

int i, j, minIndex;

for (i = 0; i < size - 1; i++) {

minIndex = i;

for (j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

int temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

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

for (int i = 0; i < size; i++) {


printf("%d ", arr[i]);

printf("\n");

int main() {

int choice, n, i;

printf("Enter the size of the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter the array elements: ");

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("MENU:\n");

printf("1. Bubble Sort\n");

printf("2. Merge Sort\n");

printf("3. Insertion Sort\n");

printf("4. Selection Sort\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

bubbleSort(arr, n);

printf("Array sorted using Bubble Sort: ");

break;

case 2:

mergeSort(arr, 0, n - 1);

printf("Array sorted using Merge Sort: ");

break;

case 3:

insertionSort(arr, n);
printf("Array sorted using Insertion Sort: ");

break;

case 4:

selectionSort(arr, n);

printf("Array sorted using Selection Sort: ");

break;

default:

printf("Invalid choice.\n");

return 0;

printArray(arr, n);

return 0;

OUTPUT
Q3. WAP to implement a Singly Linked List.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void insertAtBeginning(struct Node** head, int value) {

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

newNode->data = value;

newNode->next = *head;

*head = newNode;

printf("Node inserted at the beginning.\n");

void insertAtEnd(struct Node** head, 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 at the end.\n");

void deleteAtBeginning(struct Node** head) {


if (*head == NULL) {

printf("List is already empty.\n");

} else {

struct Node* temp = *head;

*head = temp->next;

free(temp);

printf("Node deleted from the beginning.\n");

void deleteAtEnd(struct Node** head) {

if (*head == NULL) {

printf("List is already empty.\n");

} else if ((*head)->next == NULL) {

free(*head);

*head = NULL;

printf("Node deleted from the end.\n");

} else {

struct Node* temp = *head;

while (temp->next->next != NULL) {

temp = temp->next;

free(temp->next);

temp->next = NULL;

printf("Node deleted from the end.\n");

void displayList(struct Node* head) {

if (head == NULL) {

printf("List is empty.\n");

} else {
printf("List elements: ");

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("\n");

int main() {

struct Node* head = NULL;

insertAtBeginning(&head, 5);

insertAtBeginning(&head, 10);

insertAtEnd(&head, 15);

displayList(head);

deleteAtBeginning(&head);

deleteAtEnd(&head);

deleteAtEnd(&head);

displayList(head);

return 0;

}
OUTPUT
Q4. WAP to implement a Circular Linked Lists.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void insertAtBeginning(struct Node** head, int value) {

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

newNode->data = value;

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;

*head = newNode;

printf("Node inserted at the beginning.\n");

void insertAtEnd(struct Node** head, int value) {

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

newNode->data = value;

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 the end.\n");

void deleteAtBeginning(struct Node** head) {

if (*head == NULL) {

printf("List is already empty.\n");

} else if ((*head)->next == *head) {

free(*head);

*head = NULL;

printf("Node deleted from the beginning.\n");

} else {

struct Node* temp = *head;

while (temp->next != *head) {

temp = temp->next;

struct Node* deleteNode = *head;

*head = (*head)->next;

temp->next = *head;

free(deleteNode);

printf("Node deleted from the beginning.\n");

void deleteAtEnd(struct Node** head) {

if (*head == NULL) {
printf("List is already empty.\n");

} else if ((*head)->next == *head) {

free(*head);

*head = NULL;

printf("Node deleted from the end.\n");

} else {

struct Node* temp = *head;

struct Node* prev = NULL;

while (temp->next != *head) {

prev = temp;

temp = temp->next;

prev->next = *head;

free(temp);

printf("Node deleted from the end.\n");

void displayList(struct Node* head) {

if (head == NULL) {

printf("List is empty.\n");

} else {

struct Node* current = head;

printf("List elements: ");

do {

printf("%d ", current->data);

current = current->next;

} while (current != head);

printf("\n");

int main() {
struct Node* head = NULL;

insertAtBeginning(&head, 55);

insertAtBeginning(&head, 32);

insertAtEnd(&head, 75);

displayList(head);

deleteAtBeginning(&head);

deleteAtEnd(&head);

deleteAtEnd(&head);

displayList(head);

return 0;

OUTPUT
Q5. WAP to implement Doubly Linked Lists.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

void insertAtBeginning(struct Node** head, int value) {

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

newNode->data = value;

newNode->prev = NULL;

newNode->next = *head;

if (*head != NULL) {

(*head)->prev = newNode;

*head = newNode;

printf("Node inserted at the beginning.\n");

void insertAtEnd(struct Node** head, 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;
}

temp->next = newNode;

newNode->prev = temp;

printf("Node inserted at the end.\n");

void deleteAtBeginning(struct Node** head) {

if (*head == NULL) {

printf("List is already empty.\n");

} else {

struct Node* temp = *head;

*head = (*head)->next;

if (*head != NULL) {

(*head)->prev = NULL;

free(temp);

printf("Node deleted from the beginning.\n");

void deleteAtEnd(struct Node** head) {

if (*head == NULL) {

printf("List is already empty.\n");

} else if ((*head)->next == NULL) {

free(*head);

*head = NULL;

printf("Node deleted from the end.\n");

} else {

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

}
temp->prev->next = NULL;

free(temp);

printf("Node deleted from the end.\n");

void displayList(struct Node* head) {

if (head == NULL) {

printf("List is empty.\n");

} else {

printf("List elements: ");

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("\n");

int main() {

struct Node* head = NULL;

insertAtBeginning(&head, 56);

insertAtBeginning(&head, 12);

insertAtEnd(&head, 38);

displayList(head);

deleteAtBeginning(&head);

deleteAtEnd(&head);

deleteAtEnd(&head);

displayList(head);

return 0;

}
OUTPUT
Q6. Write a menu driven program to implement (i) Static Stack (ii) Dynamic Stack.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

// Static Stack Implementation

#define MAX_SIZE 100

int staticStack[MAX_SIZE];

int staticTop = -1;

void pushStatic(int value) {

if (staticTop == MAX_SIZE - 1) {

printf("Stack Overflow: Static Stack is full.\n");

} else {

staticStack[++staticTop] = value;

printf("Element pushed to Static Stack: %d\n", value);

void popStatic() {

if (staticTop == -1) {

printf("Stack Underflow: Static Stack is empty.\n");

} else {

int poppedElement = staticStack[staticTop--];

printf("Element popped from Static Stack: %d\n", poppedElement);

void displayStatic() {

if (staticTop == -1) {

printf("Static Stack is empty.\n");

} else {

printf("Static Stack elements: ");

for (int i = 0; i <= staticTop; i++) {

printf("%d ", staticStack[i]);


}

printf("\n");

// Dynamic Stack Implementation

struct Node {

int data;

struct Node* next;

};

struct Node* dynamicTop = NULL;

void pushDynamic(int value) {

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

newNode->data = value;

newNode->next = dynamicTop;

dynamicTop = newNode;

printf("Element pushed to Dynamic Stack: %d\n", value);

void popDynamic() {

if (dynamicTop == NULL) {

printf("Stack Underflow: Dynamic Stack is empty.\n");

} else {

struct Node* temp = dynamicTop;

dynamicTop = dynamicTop->next;

int poppedElement = temp->data;

free(temp);

printf("Element popped from Dynamic Stack: %d\n", poppedElement);

void displayDynamic() {

if (dynamicTop == NULL) {

printf("Dynamic Stack is empty.\n");


} else {

struct Node* temp = dynamicTop;

printf("Dynamic Stack elements: ");

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\n----------------------\n");

printf("Menu:\n");

printf("1. Push to Static Stack\n");

printf("2. Pop from Static Stack\n");

printf("3. Display Static Stack\n");

printf("4. Push to Dynamic Stack\n");

printf("5. Pop from Dynamic Stack\n");

printf("6. Display Dynamic Stack\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to push: ");

scanf("%d", &value);

pushStatic(value);

break;

case 2:
popStatic();

break;

case 3:

displayStatic();

break;

case 4:

printf("Enter the value to push: ");

scanf("%d", &value);

pushDynamic(value);

break;

case 5:

popDynamic();

break;

case 6:

displayDynamic();

break;

case 7:

printf("Exiting the program.\n");

exit(0);

default:

printf("Invalid choice. Please try again.\n");

return 0;

}
OUTPUT
Q7. WAP to implement a (i) Static (ii) Dynamic Circular Queue

SOLUTION

#include <stdio.h>

#include <stdlib.h>

// Static Circular Queue Implementation

#define MAX_SIZE 5

int staticQueue[MAX_SIZE];

int staticFront = -1;

int staticRear = -1;

void enqueueStatic(int value) {

if ((staticFront == 0 && staticRear == MAX_SIZE - 1) || (staticFront == staticRear + 1)) {

printf("Queue Overflow: Static Circular Queue is full.\n");

} else {

if (staticFront == -1) {

staticFront = 0;

staticRear = 0;

} else if (staticRear == MAX_SIZE - 1) {

staticRear = 0;

} else {

staticRear++;

staticQueue[staticRear] = value;

printf("Element enqueued to Static Circular Queue: %d\n", value);

void dequeueStatic() {

if (staticFront == -1) {

printf("Queue Underflow: Static Circular Queue is empty.\n");

} else {

int dequeuedElement = staticQueue[staticFront];

printf("Element dequeued from Static Circular Queue: %d\n", dequeuedElement);


if (staticFront == staticRear) {

staticFront = -1;

staticRear = -1;

} else if (staticFront == MAX_SIZE - 1) {

staticFront = 0;

} else {

staticFront++;

void displayStatic() {

if (staticFront == -1) {

printf("Static Circular Queue is empty.\n");

} else {

printf("Static Circular Queue elements: ");

int i;

if (staticFront <= staticRear) {

for (i = staticFront; i <= staticRear; i++) {

printf("%d ", staticQueue[i]);

} else {

for (i = staticFront; i < MAX_SIZE; i++) {

printf("%d ", staticQueue[i]);

for (i = 0; i <= staticRear; i++) {

printf("%d ", staticQueue[i]);

printf("\n");

}
// Dynamic Circular Queue Implementation

struct Node {

int data;

struct Node* next;

};

struct Node* dynamicFront = NULL;

struct Node* dynamicRear = NULL;

void enqueueDynamic(int value) {

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

newNode->data = value;

newNode->next = NULL;

if (dynamicFront == NULL && dynamicRear == NULL) {

dynamicFront = dynamicRear = newNode;

} else {

dynamicRear->next = newNode;

dynamicRear = newNode;

printf("Element enqueued to Dynamic Circular Queue: %d\n", value);

void dequeueDynamic() {

if (dynamicFront == NULL) {

printf("Queue Underflow: Dynamic Circular Queue is empty.\n");

} else {

struct Node* temp = dynamicFront;

int dequeuedElement = temp->data;

dynamicFront = dynamicFront->next;

free(temp);

if (dynamicFront == NULL) {

dynamicRear = NULL;

printf("Element dequeued from Dynamic Circular Queue: %d\n", dequeuedElement);


}

void displayDynamic() {

if (dynamicFront == NULL) {

printf("Dynamic Circular Queue is empty.\n");

} else {

printf("Dynamic Circular Queue elements: ");

struct Node* temp = dynamicFront;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\n----------------------\n");

printf("Menu:\n");

printf("1. Enqueue to Static Circular Queue\n");

printf("2. Dequeue from Static Circular Queue\n");

printf("3. Display Static Circular Queue\n");

printf("4. Enqueue to Dynamic Circular Queue\n");

printf("5. Dequeue from Dynamic Circular Queue\n");

printf("6. Display Dynamic Circular Queue\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
printf("Enter the value to enqueue: ");

scanf("%d", &value);

enqueueStatic(value);

break;

case 2:

dequeueStatic();

break;

case 3:

displayStatic();

break;

case 4:

printf("Enter the value to enqueue: ");

scanf("%d", &value);

enqueueDynamic(value);

break;

case 5:

dequeueDynamic();

break;

case 6:

displayDynamic();

break;

case 7:

printf("Exiting the program.\n");

exit(0);

default:

printf("Invalid choice. Please try again.\n");

return 0;

}
OUTPUT
Q8. WAP to implement a (i) Static (ii) Dynamic De-Queue.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

// Static Deque Implementation

#define MAX_SIZE 5

int staticDeque[MAX_SIZE];

int staticFront = -1;

int staticRear = -1;

void insertFrontStatic(int value) {

if ((staticFront == 0 && staticRear == MAX_SIZE - 1) || (staticFront == staticRear + 1)) {

printf("Deque Overflow: Static Deque is full.\n");

} else {

if (staticFront == -1) {

staticFront = 0;

staticRear = 0;

} else if (staticFront == 0) {

staticFront = MAX_SIZE - 1;

} else {

staticFront--;

staticDeque[staticFront] = value;

printf("Element inserted at front in Static Deque: %d\n", value);

void insertRearStatic(int value) {

if ((staticFront == 0 && staticRear == MAX_SIZE - 1) || (staticFront == staticRear + 1)) {

printf("Deque Overflow: Static Deque is full.\n");

} else {

if (staticFront == -1) {
staticFront = 0;

staticRear = 0;

} else if (staticRear == MAX_SIZE - 1) {

staticRear = 0;

} else {

staticRear++;

staticDeque[staticRear] = value;

printf("Element inserted at rear in Static Deque: %d\n", value);

void deleteFrontStatic() {

if (staticFront == -1) {

printf("Deque Underflow: Static Deque is empty.\n");

} else {

int deletedElement = staticDeque[staticFront];

printf("Element deleted from front in Static Deque: %d\n", deletedElement);

if (staticFront == staticRear) {

staticFront = -1;

staticRear = -1;

} else if (staticFront == MAX_SIZE - 1) {

staticFront = 0;

} else {

staticFront++;

void deleteRearStatic() {

if (staticFront == -1) {

printf("Deque Underflow: Static Deque is empty.\n");


} else {

int deletedElement = staticDeque[staticRear];

printf("Element deleted from rear in Static Deque: %d\n", deletedElement);

if (staticFront == staticRear) {

staticFront = -1;

staticRear = -1;

} else if (staticRear == 0) {

staticRear = MAX_SIZE - 1;

} else {

staticRear--;

void displayStatic() {

if (staticFront == -1) {

printf("Static Deque is empty.\n");

} else {

printf("Static Deque elements: ");

int i;

if (staticFront <= staticRear) {

for (i = staticFront; i <= staticRear; i++) {

printf("%d ", staticDeque[i]);

} else {

for (i = staticFront; i < MAX_SIZE; i++) {

printf("%d ", staticDeque[i]);

for (i = 0; i <= staticRear; i++) {

printf("%d ", staticDeque[i]);

}
printf("\n");

// Dynamic Deque Implementation

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

struct Node* dynamicFront = NULL;

struct Node* dynamicRear = NULL;

void insertFrontDynamic(int value) {

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

newNode->data = value;

newNode->prev = NULL;

newNode->next = NULL;

if (dynamicFront == NULL) {

dynamicFront = dynamicRear = newNode;

} else {

newNode->next = dynamicFront;

dynamicFront->prev = newNode;

dynamicFront = newNode;

printf("Element inserted at front in Dynamic Deque: %d\n", value);

void insertRearDynamic(int value) {

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

newNode->data = value;

newNode->prev = NULL;

newNode->next = NULL;

if (dynamicFront == NULL) {
dynamicFront = dynamicRear = newNode;

} else {

dynamicRear->next = newNode;

newNode->prev = dynamicRear;

dynamicRear = newNode;

printf("Element inserted at rear in Dynamic Deque: %d\n", value);

void deleteFrontDynamic() {

if (dynamicFront == NULL) {

printf("Deque Underflow: Dynamic Deque is empty.\n");

} else {

struct Node* temp = dynamicFront;

int deletedElement = temp->data;

dynamicFront = dynamicFront->next;

if (dynamicFront != NULL) {

dynamicFront->prev = NULL;

} else {

dynamicRear = NULL;

free(temp);

printf("Element deleted from front in Dynamic Deque: %d\n", deletedElement);

void deleteRearDynamic() {

if (dynamicFront == NULL) {

printf("Deque Underflow: Dynamic Deque is empty.\n");

} else {

struct Node* temp = dynamicRear;

int deletedElement = temp->data;

dynamicRear = dynamicRear->prev;
if (dynamicRear != NULL) {

dynamicRear->next = NULL;

} else {

dynamicFront = NULL;

free(temp);

printf("Element deleted from rear in Dynamic Deque: %d\n", deletedElement);

void displayDynamic() {

if (dynamicFront == NULL) {

printf("Dynamic Deque is empty.\n");

} else {

printf("Dynamic Deque elements: ");

struct Node* temp = dynamicFront;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\n----------------------\n");

printf("Menu:\n");

printf("1. Insert at Front in Static Deque\n");

printf("2. Insert at Rear in Static Deque\n");

printf("3. Delete from Front in Static Deque\n");

printf("4. Delete from Rear in Static Deque\n");


printf("5. Display Static Deque\n");

printf("6. Insert at Front in Dynamic Deque\n");

printf("7. Insert at Rear in Dynamic Deque\n");

printf("8. Delete from Front in Dynamic Deque\n");

printf("9. Delete from Rear in Dynamic Deque\n");

printf("10. Display Dynamic Deque\n");

printf("11. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to insert at front in Static Deque: ");

scanf("%d", &value);

insertFrontStatic(value);

break;

case 2:

printf("Enter the value to insert at rear in Static Deque: ");

scanf("%d", &value);

insertRearStatic(value);

break;

case 3:

deleteFrontStatic();

break;

case 4:

deleteRearStatic();

break;

case 5:

displayStatic();

break;

case 6:

printf("Enter the value to insert at front in Dynamic Deque: ");


scanf("%d", &value);

insertFrontDynamic(value);

break;

case 7:

printf("Enter the value to insert at rear in Dynamic Deque: ");

scanf("%d", &value);

insertRearDynamic(value);

break;

case 8:

deleteFrontDynamic();

break;

case 9:

deleteRearDynamic();

break;

case 10:

displayDynamic();

break;

case 11:

printf("Exiting the program.\n");

exit(0);

default:

printf("Invalid choice. Please try again.\n");

return 0;

}
OUTPUT
Q9. Implement recursive algorithms for the following operations on Binary Search Tree

a) Insertion

b) Searching

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int value) {

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

newNode->data = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

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

if (root == NULL) {

return createNode(value);

if (value < root->data) {

root->left = insert(root->left, value);

} else if (value > root->data) {

root->right = insert(root->right, value);

return root;

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


if (root == NULL || root->data == value) {

return root;

if (value < root->data) {

return search(root->left, value);

} else {

return search(root->right, value);

void inorderTraversal(struct Node* root) {

if (root != NULL) {

inorderTraversal(root->left);

printf("%d ", root->data);

inorderTraversal(root->right);

int main() {

struct Node* root = NULL;

// Insertion

root = insert(root, 50);

insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

insert(root, 60);

insert(root, 80);

// Searching

int searchValue = 40;

struct Node* searchResult = search(root, searchValue);

if (searchResult != NULL) {

printf("%d is found in the BST.\n", searchValue);


} else {

printf("%d is not found in the BST.\n", searchValue);

// Inorder Traversal

printf("Inorder Traversal: ");

inorderTraversal(root);

printf("\n");

return 0;

OUTPUT
Q10. Implement recursive algorithms for BST traversal- Inorder, Preorder, Postorder.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int value) {

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

newNode->data = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

void inorderTraversal(struct Node* root) {

if (root != NULL) {

inorderTraversal(root->left);

printf("%d ", root->data);

inorderTraversal(root->right);

void preorderTraversal(struct Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preorderTraversal(root->left);

preorderTraversal(root->right);

void postorderTraversal(struct Node* root) {


if (root != NULL) {

postorderTraversal(root->left);

postorderTraversal(root->right);

printf("%d ", root->data);

int main() {

struct Node* root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

printf("Inorder Traversal: ");

inorderTraversal(root);

printf("\n");

printf("Preorder Traversal: ");

preorderTraversal(root);

printf("\n");

printf("Postorder Traversal: ");

postorderTraversal(root);

printf("\n");

return 0;

}
OUTPUT
Q11. WAP to search & display the location of an element specified by the user, in an array using (i)
Linear Search (ii) Binary Search technique.

SOLUTION

#include <stdio.h>

int linearSearch(int arr[], int n, int target) {

for (int i = 0; i < n; i++) {

if (arr[i] == target) {

return i;

return -1;

int binarySearch(int arr[], int low, int high, int target) {

while (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] < target) {

low = mid + 1;

} else {

high = mid - 1;

return -1;

int main() {

int n;

printf("Enter the size of the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array:\n");


for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

int target;

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

scanf("%d", &target);

int linearSearchIndex = linearSearch(arr, n, target);

if (linearSearchIndex != -1) {

printf("Element %d found at index %d using linear search.\n", target, linearSearchIndex);

} else {

printf("Element %d not found using linear search.\n", target);

int binarySearchIndex = binarySearch(arr, 0, n - 1, target);

if (binarySearchIndex != -1) {

printf("Element %d found at index %d using binary search.\n", target, binarySearchIndex);

} else {

printf("Element %d not found using binary search.\n", target);

return 0;

}
OUTPUT
Q12. WAP to accept a matrix from user, find out matrix is sparse or not and convert into triplex
matrix.

SOLUTION

#include <stdio.h>

#define MAX_SIZE 100

void convertToTriplet(int matrix[][MAX_SIZE], int rows, int cols, int triplet[][3], int* numTerms) {

int count = 0;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

if (matrix[i][j] != 0) {

triplet[count][0] = i;

triplet[count][1] = j;

triplet[count][2] = matrix[i][j];

count++;

*numTerms = count;

int isSparseMatrix(int matrix[][MAX_SIZE], int rows, int cols) {

int zeroCount = 0;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

if (matrix[i][j] == 0) {

zeroCount++;

int totalElements = rows * cols;

if (zeroCount > totalElements / 2) {

return 1;
}

return 0;

void displayMatrix(int matrix[][MAX_SIZE], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

printf("%d ", matrix[i][j]);

printf("\n");

void displayTriplet(int triplet[][3], int numTerms) {

printf("Triplet Matrix:\n");

for (int i = 0; i < numTerms; i++) {

printf("%d %d %d\n", triplet[i][0], triplet[i][1], triplet[i][2]);

int main() {

int matrix[MAX_SIZE][MAX_SIZE];

int rows, cols;

printf("Enter the number of rows and columns of the matrix: ");

scanf("%d %d", &rows, &cols);

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

printf("Matrix:\n");

displayMatrix(matrix, rows, cols);


if (isSparseMatrix(matrix, rows, cols)) {

printf("The matrix is sparse.\n");

int triplet[MAX_SIZE * MAX_SIZE][3];

int numTerms;

convertToTriplet(matrix, rows, cols, triplet, &numTerms);

displayTriplet(triplet, numTerms);

} else {

printf("The matrix is not sparse.\n");

return 0;

OUTPUT
Q13. WAP to implement Polynomial addition operation using linked list

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

int coefficient;

int exponent;

struct Node* next;

};

struct Node* createNode(int coefficient, int exponent) {

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

newNode->coefficient = coefficient;

newNode->exponent = exponent;

newNode->next = NULL;

return newNode;

void insertTerm(struct Node** poly, int coefficient, int exponent) {

struct Node* newNode = createNode(coefficient, exponent);

if (*poly == NULL) {

*poly = newNode;

} else {

struct Node* temp = *poly;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void displayPolynomial(struct Node* poly) {

struct Node* temp = poly;

if (temp == NULL) {
printf("Polynomial is empty.\n");

return;

while (temp != NULL) {

printf("%dx^%d ", temp->coefficient, temp->exponent);

temp = temp->next;

if (temp != NULL) {

printf("+ ");

printf("\n");

struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {

struct Node* result = NULL;

struct Node* temp1 = poly1;

struct Node* temp2 = poly2;

while (temp1 != NULL && temp2 != NULL) {

if (temp1->exponent > temp2->exponent) {

insertTerm(&result, temp1->coefficient, temp1->exponent);

temp1 = temp1->next;

} else if (temp1->exponent < temp2->exponent) {

insertTerm(&result, temp2->coefficient, temp2->exponent);

temp2 = temp2->next;

} else {

int sum = temp1->coefficient + temp2->coefficient;

if (sum != 0) {

insertTerm(&result, sum, temp1->exponent);

temp1 = temp1->next;

temp2 = temp2->next;

}
}

while (temp1 != NULL) {

insertTerm(&result, temp1->coefficient, temp1->exponent);

temp1 = temp1->next;

while (temp2 != NULL) {

insertTerm(&result, temp2->coefficient, temp2->exponent);

temp2 = temp2->next;

return result;

int main() {

struct Node* poly1 = NULL;

struct Node* poly2 = NULL;

insertTerm(&poly1, 5, 2);

insertTerm(&poly1, -4, 1);

insertTerm(&poly1, 3, 0);

insertTerm(&poly2, 2, 3);

insertTerm(&poly2, 1, 2);

insertTerm(&poly2, -7, 0);

printf("Polynomial 1: ");

displayPolynomial(poly1);

printf("Polynomial 2: ");

displayPolynomial(poly2);

struct Node* sum = addPolynomials(poly1, poly2);

printf("Sum: ");

displayPolynomial(sum);

return 0;

}
OUTPUT
14. Write a C program to create two linked lists from a given list in following

way

INPUT List:- 1 2 3 4 5 6 7 8 9 10

OUTPUT:-

First List:- 1 3 5 7 9

Second List:- 2 4 6 8 10

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void insertNode(struct Node** head, 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;

void splitLinkedList(struct Node* head, struct Node** firstList, struct Node** secondList) {

struct Node* current = head;


int count = 0;

while (current != NULL) {

if (count % 2 == 0) {

insertNode(firstList, current->data);

} else {

insertNode(secondList, current->data);

count++;

current = current->next;

void displayLinkedList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

printf("\n");

int main() {

struct Node* head = NULL;

struct Node* firstList = NULL;

struct Node* secondList = NULL;

int input[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int n = sizeof(input) / sizeof(input[0]);

for (int i = 0; i < n; i++) {

insertNode(&head, input[i]);

splitLinkedList(head, &firstList, &secondList);

printf("First List: ");


displayLinkedList(firstList);

printf("Second List: ");

displayLinkedList(secondList);

return 0;

OUTPUT
15. WAP to implement Student Database using Linked List with the following

structure

 Name

 Rollno

 Marks of 5 subjects

 Average

 Result, If the average < 50, then print ‘Fail’, otherwise ‘Pass’

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Student {

char name[50];

int rollno;

int marks[5];

float average;

char result[10];

struct Student* next;

};

void insertStudent(struct Student** head, char name[], int rollno, int marks[]) {

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

strcpy(newNode->name, name);

newNode->rollno = rollno;

memcpy(newNode->marks, marks, sizeof(int) * 5);

float sum = 0;

for (int i = 0; i < 5; i++) {

sum += marks[i];

newNode->average = sum / 5;

if (newNode->average < 50) {

strcpy(newNode->result, "Fail");
} else {

strcpy(newNode->result, "Pass");

newNode->next = NULL;

if (*head == NULL) {

*head = newNode;

} else {

struct Student* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void displayStudents(struct Student* head) {

struct Student* current = head;

while (current != NULL) {

printf("Name: %s\n", current->name);

printf("Roll No: %d\n", current->rollno);

printf("Marks: ");

for (int i = 0; i < 5; i++) {

printf("%d ", current->marks[i]);

printf("\n");

printf("Average: %.2f\n", current->average);

printf("Result: %s\n", current->result);

printf("\n");

current = current->next;

int main() {
struct Student* head = NULL;

insertStudent(&head, "ADITYA", 1, (int[]){80, 75, 90, 85, 95});

insertStudent(&head, "ANKIT", 2, (int[]){70, 65, 80, 75, 60});

insertStudent(&head, "SHIVAM", 3, (int[]){90, 85, 95, 80, 90});

displayStudents(head);

return 0;

OUTPUT
Q16. Write a program to convert Infix to equivalent

(i) Prefix expression

(ii) Postfix expression

SOLUTION

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
struct Stack {
int top;
char items[MAX_SIZE];
};
void initialize(struct Stack* stack) {
stack->top = -1;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
void push(struct Stack* stack, char item) {
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->items[++stack->top] = item;
}
char pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return '\0';
}
return stack->items[stack->top--];
}int isOperator(char ch) {
return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}
int precedence(char ch) {
if (ch == '+' || ch == '-') {
return 1;
} else if (ch == '*' || ch == '/') {
return 2;
}
return 0;
}
void infixToPrefix(char infix[], char prefix[]) {
int i, j;
struct Stack stack;
initialize(&stack);
strrev(infix);
for (i = 0, j = 0; infix[i] != '\0'; i++) {
if (infix[i] == '(') {
infix[i] = ')';
} else if (infix[i] == ')') {
infix[i] = '(';
}
}
for (i = 0, j = 0; infix[i] != '\0'; i++) {
if (infix[i] >= 'a' && infix[i] <= 'z') {
prefix[j++] = infix[i];
} else if (infix[i] == '(') {
push(&stack, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(&stack) && stack.items[stack.top] != '(') {
prefix[j++] = pop(&stack);
}
if (!isEmpty(&stack) && stack.items[stack.top] != '(') {
printf("Invalid Expression\n");
return;
} else {
pop(&stack);
}
} else if (isOperator(infix[i])) {
while (!isEmpty(&stack) && precedence(infix[i]) <=
precedence(stack.items[stack.top])) {
prefix[j++] = pop(&stack);
}
push(&stack, infix[i]);
}
}
while (!isEmpty(&stack)) {
prefix[j++] = pop(&stack);
}
prefix[j] = '\0';
strrev(prefix);
}
void infixToPostfix(char infix[], char postfix[]) {
int i, j;
struct Stack stack;
initialize(&stack);
for (i = 0, j = 0; infix[i] != '\0'; i++) {
if (infix[i] >= 'a' && infix[i] <= 'z') {
postfix[j++] = infix[i];
} else if (infix[i] == '(') {
push(&stack, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(&stack) && stack.items[stack.top] != '(') {
postfix[j++] = pop(&stack);
}
if (!isEmpty(&stack) && stack.items[stack.top] != '(') {
printf("Invalid Expression\n");
return;
} else {
pop(&stack);
}
} else if (isOperator(infix[i])) {
while (!isEmpty(&stack) && precedence(infix[i]) <=
precedence(stack.items[stack.top])) {
postfix[j++] = pop(&stack);
}
push(&stack, infix[i]);
}
}
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
int main() {
char infixExpression[100];
char prefixExpression[100];
char postfixExpression[100];
printf("Enter the infix expression: ");
fgets(infixExpression, sizeof(infixExpression), stdin);
infixExpression[strcspn(infixExpression, "\n")] = '\0';
infixToPrefix(infixExpression, prefixExpression);
infixToPostfix(infixExpression, postfixExpression);
printf("Prefix expression: %s\n", prefixExpression);
printf("Postfix expression: %s\n", postfixExpression);
return 0;
}
OUTPUT
17. Write a program to evaluate (i) Prefix Expression (ii) Postfix Expression using stack.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define MAX_SIZE 100

struct Stack {

int top;

int items[MAX_SIZE];

};

void initialize(struct Stack* stack) {

stack->top = -1;

int isEmpty(struct Stack* stack) {

return stack->top == -1;

int isFull(struct Stack* stack) {

return stack->top == MAX_SIZE - 1;

void push(struct Stack* stack, int item) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->items[++stack->top] = item;

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");

return -1;
}

return stack->items[stack->top--];

int evaluatePrefixExpression(char expression[]) {

struct Stack stack;

initialize(&stack);

int i, op1, op2, result;

int len = strlen(expression);

for (i = len - 1; i >= 0; i--) {

if (isdigit(expression[i])) {

push(&stack, expression[i] - '0');

} else {

op1 = pop(&stack);

op2 = pop(&stack);

switch (expression[i]) {

case '+':

result = op1 + op2;

break;

case '-':

result = op1 - op2;

break;

case '*':

result = op1 * op2;

break;

case '/':

result = op1 / op2;

break;

default:

printf("Invalid Expression\n");

return -1;

}
push(&stack, result);

return pop(&stack);

int evaluatePostfixExpression(char expression[]) {

struct Stack stack;

initialize(&stack);

int i, op1, op2, result;

int len = strlen(expression);

for (i = 0; i < len; i++) {

if (isdigit(expression[i])) {

push(&stack, expression[i] - '0');

} else {

op2 = pop(&stack);

op1 = pop(&stack);

switch (expression[i]) {

case '+':

result = op1 + op2;

break;

case '-':

result = op1 - op2;

break;

case '*':

result = op1 * op2;

break;

case '/':

result = op1 / op2;

break;

default:

printf("Invalid Expression\n");
return -1;

push(&stack, result);

return pop(&stack);

int main() {

char prefixExpression[MAX_SIZE];

char postfixExpression[MAX_SIZE];

printf("Enter the prefix expression: ");

fgets(prefixExpression, sizeof(prefixExpression), stdin);

prefixExpression[strcspn(prefixExpression, "\n")] = '\0';

printf("Enter the postfix expression: ");

fgets(postfixExpression, sizeof(postfixExpression), stdin);

postfixExpression[strcspn(postfixExpression, "\n")] = '\0';

int prefixResult = evaluatePrefixExpression(prefixExpression);

int postfixResult = evaluatePostfixExpression(postfixExpression);

printf("Result of prefix expression: %d\n", prefixResult);

printf("Result of postfix expression: %d\n", postfixResult);

return 0;

}
OUTPUT
Q18. Let us assume a Patient's coupon generator for the Doctors’ clinic. The patients are given the
coupons on first-come-first-serve basis. After the visit of a patient, patient-ID is kept stack-wise. At
the end of the day, the count is generated from the stack. Construct a menu-based program for
patients’ coupons generator using an appropriate data structure

SOLUTION

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

struct Stack {

int top;

int items[MAX_SIZE];

};

void initialize(struct Stack* stack) {

stack->top = -1;

int isEmpty(struct Stack* stack) {

return stack->top == -1;

int isFull(struct Stack* stack) {

return stack->top == MAX_SIZE - 1;

void push(struct Stack* stack, int item) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->items[++stack->top] = item;

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");

return -1;
}

return stack->items[stack->top--];

int getCount(struct Stack* stack) {

return stack->top + 1;

void generateCoupon(struct Stack* stack, int patientId) {

push(stack, patientId);

printf("Coupon generated for Patient ID: %d\n", patientId);

void displayCount(struct Stack* stack) {

int count = getCount(stack);

printf("Total coupons generated: %d\n", count);

int main() {

struct Stack stack;

initialize(&stack);

int choice, patientId;

do {

printf("\n----------------------\n");

printf("1. Generate Coupon\n");

printf("2. Display Total Coupons\n");

printf("3. Exit\n");

printf("----------------------\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter Patient ID: ");

scanf("%d", &patientId);

generateCoupon(&stack, patientId);
break;

case 2:

displayCount(&stack);

break;

case 3:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice. Please try again.\n");

} while (choice != 3);

return 0;

OUTPUT
Q19. WAP to implement an expression tree. (For example: (a + b / (c * d) – e) )

SOLUTION

#include <stdio.h>

#include <stdlib.h>

struct Node {

char data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(char data) {

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

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

struct Node* buildExpressionTree(char postfix[]) {

struct Node* stack[MAX_SIZE];

int top = -1;

for (int i = 0; postfix[i] != '\0'; i++) {

struct Node* newNode = createNode(postfix[i]);

if (isalpha(postfix[i])) {

stack[++top] = newNode;

} else {

newNode->right = stack[top--];

newNode->left = stack[top--];

stack[++top] = newNode;

return stack[top];

}
void inorderTraversal(struct Node* root) {

if (root == NULL) {

return;

inorderTraversal(root->left);

printf("%c ", root->data);

inorderTraversal(root->right);

void preorderTraversal(struct Node* root) {

if (root == NULL) {

return;

printf("%c ", root->data);

preorderTraversal(root->left);

preorderTraversal(root->right);

void postorderTraversal(struct Node* root) {

if (root == NULL) {

return;

postorderTraversal(root->left);

postorderTraversal(root->right);

printf("%c ", root->data);

int main() {

char postfixExpression[100];

printf("Enter the postfix expression: ");

fgets(postfixExpression, sizeof(postfixExpression), stdin);

struct Node* root = buildExpressionTree(postfixExpression);

printf("Inorder Traversal: ");


inorderTraversal(root);

printf("\n");

printf("Preorder Traversal: ");

preorderTraversal(root);

printf("\n");

printf("Postorder Traversal: ");

postorderTraversal(root);

printf("\n");

return 0;

OUTPUT
Q20. Sometimes a program requires two stacks containing the same type of items. Suppose two
stacks are stored in separate arrays, then one stack might overflow while there is considerable
unused space in the other. A neat way to avoid this problem is to put all spaces in one stack and let
this stack grow from one end of the array, and the other stack starts from the other end and grows
in the opposite direction, i.e., toward the first stack. In this way, if one stack turns out to be large
and the other small, then they will still both fit, and there will be no overflow until all space is used.
Declare a new structure that includes these two stacks and perform various stack operations.

SOLUTION

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100 // Maximum size of the array

typedef struct {

int data[MAX_SIZE];

int top1; // Top index of stack 1

int top2; // Top index of stack 2

} TwoStacks;

// Function to initialize the two stacks

void initializeTwoStacks(TwoStacks* stacks) {

stacks->top1 = -1; // Initialize top index of stack 1

stacks->top2 = MAX_SIZE; // Initialize top index of stack 2

// Function to push an element onto stack 1

void pushStack1(TwoStacks* stacks, int item) {

if (stacks->top1 < stacks->top2 - 1) {

stacks->data[++stacks->top1] = item;

} else {

printf("Stack 1 is full. Cannot push element.\n");

// Function to push an element onto stack 2

void pushStack2(TwoStacks* stacks, int item) {

if (stacks->top1 < stacks->top2 - 1) {


stacks->data[--stacks->top2] = item;

} else {

printf("Stack 2 is full. Cannot push element.\n");

// Function to pop an element from stack 1

int popStack1(TwoStacks* stacks) {

if (stacks->top1 >= 0) {

return stacks->data[stacks->top1--];

} else {

printf("Stack 1 is empty. Cannot pop element.\n");

return -1; // Return a sentinel value to indicate failure

// Function to pop an element from stack 2

int popStack2(TwoStacks* stacks) {

if (stacks->top2 < MAX_SIZE) {

return stacks->data[stacks->top2++];

} else {

printf("Stack 2 is empty. Cannot pop element.\n");

return -1; // Return a sentinel value to indicate failure

// Function to check if stack 1 is empty

int isEmptyStack1(TwoStacks* stacks) {

return stacks->top1 == -1;

// Function to check if stack 2 is empty

int isEmptyStack2(TwoStacks* stacks) {

return stacks->top2 == MAX_SIZE;

}
int main() {

TwoStacks stacks;

initializeTwoStacks(&stacks);

pushStack1(&stacks, 10);

pushStack1(&stacks, 20);

pushStack2(&stacks, 30);

pushStack2(&stacks, 40);

printf("Popped from stack 1: %d\n", popStack1(&stacks));

printf("Popped from stack 2: %d\n", popStack2(&stacks));

return 0;

OUTPUT

You might also like