Professional Documents
Culture Documents
SOLUTION
#include <stdio.h>
arr[i + 1] = arr[i];
arr[index] = element;
printf("\n");
int start = 0;
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int merged[]) {
int i, j, k;
i = j = k = 0;
merged[k] = arr1[i];
i++;
} else {
merged[k] = arr2[j];
j++;
k++;
merged[k] = arr1[i];
i++;
k++;
merged[k] = arr2[j];
j++;
k++;
}
int main() {
int size1 = 5;
int size2 = 5;
int merged[10];
traverseArray(arr1, size1);
reverseArray(arr1, size1);
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>
int i, j;
arr[j + 1] = temp;
int i, j, k;
i = 0;
j = 0;
k = left;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int i, j, minIndex;
minIndex = i;
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
printf("\n");
int main() {
int choice, n, i;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
printf("MENU:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
bubbleSort(arr, n);
break;
case 2:
mergeSort(arr, 0, n - 1);
break;
case 3:
insertionSort(arr, n);
printf("Array sorted using Insertion Sort: ");
break;
case 4:
selectionSort(arr, n);
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;
};
newNode->data = value;
newNode->next = *head;
*head = newNode;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
} else {
*head = temp->next;
free(temp);
if (*head == NULL) {
free(*head);
*head = NULL;
} else {
temp = temp->next;
free(temp->next);
temp->next = NULL;
if (head == NULL) {
printf("List is empty.\n");
} else {
printf("List elements: ");
head = head->next;
printf("\n");
int main() {
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;
};
newNode->data = value;
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
*head = newNode;
newNode->data = value;
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
if (*head == NULL) {
free(*head);
*head = NULL;
} else {
temp = temp->next;
*head = (*head)->next;
temp->next = *head;
free(deleteNode);
if (*head == NULL) {
printf("List is already empty.\n");
free(*head);
*head = NULL;
} else {
prev = temp;
temp = temp->next;
prev->next = *head;
free(temp);
if (head == NULL) {
printf("List is empty.\n");
} else {
do {
current = current->next;
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;
};
newNode->data = value;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
*head = newNode;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
} else {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
if (*head == NULL) {
} else {
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
free(temp);
if (*head == NULL) {
free(*head);
*head = NULL;
} else {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
if (head == NULL) {
printf("List is empty.\n");
} else {
head = head->next;
printf("\n");
int main() {
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>
int staticStack[MAX_SIZE];
if (staticTop == MAX_SIZE - 1) {
} else {
staticStack[++staticTop] = value;
void popStatic() {
if (staticTop == -1) {
} else {
void displayStatic() {
if (staticTop == -1) {
} else {
printf("\n");
struct Node {
int data;
};
newNode->data = value;
newNode->next = dynamicTop;
dynamicTop = newNode;
void popDynamic() {
if (dynamicTop == NULL) {
} else {
dynamicTop = dynamicTop->next;
free(temp);
void displayDynamic() {
if (dynamicTop == NULL) {
temp = temp->next;
printf("\n");
int main() {
while (1) {
printf("\n----------------------\n");
printf("Menu:\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
pushStatic(value);
break;
case 2:
popStatic();
break;
case 3:
displayStatic();
break;
case 4:
scanf("%d", &value);
pushDynamic(value);
break;
case 5:
popDynamic();
break;
case 6:
displayDynamic();
break;
case 7:
exit(0);
default:
return 0;
}
OUTPUT
Q7. WAP to implement a (i) Static (ii) Dynamic Circular Queue
SOLUTION
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int staticQueue[MAX_SIZE];
} else {
if (staticFront == -1) {
staticFront = 0;
staticRear = 0;
staticRear = 0;
} else {
staticRear++;
staticQueue[staticRear] = value;
void dequeueStatic() {
if (staticFront == -1) {
} else {
staticFront = -1;
staticRear = -1;
staticFront = 0;
} else {
staticFront++;
void displayStatic() {
if (staticFront == -1) {
} else {
int i;
} else {
printf("\n");
}
// Dynamic Circular Queue Implementation
struct Node {
int data;
};
newNode->data = value;
newNode->next = NULL;
} else {
dynamicRear->next = newNode;
dynamicRear = newNode;
void dequeueDynamic() {
if (dynamicFront == NULL) {
} else {
dynamicFront = dynamicFront->next;
free(temp);
if (dynamicFront == NULL) {
dynamicRear = NULL;
void displayDynamic() {
if (dynamicFront == NULL) {
} else {
temp = temp->next;
printf("\n");
int main() {
while (1) {
printf("\n----------------------\n");
printf("Menu:\n");
printf("7. Exit\n");
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:
scanf("%d", &value);
enqueueDynamic(value);
break;
case 5:
dequeueDynamic();
break;
case 6:
displayDynamic();
break;
case 7:
exit(0);
default:
return 0;
}
OUTPUT
Q8. WAP to implement a (i) Static (ii) Dynamic De-Queue.
SOLUTION
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int staticDeque[MAX_SIZE];
} else {
if (staticFront == -1) {
staticFront = 0;
staticRear = 0;
} else if (staticFront == 0) {
staticFront = MAX_SIZE - 1;
} else {
staticFront--;
staticDeque[staticFront] = value;
} else {
if (staticFront == -1) {
staticFront = 0;
staticRear = 0;
staticRear = 0;
} else {
staticRear++;
staticDeque[staticRear] = value;
void deleteFrontStatic() {
if (staticFront == -1) {
} else {
if (staticFront == staticRear) {
staticFront = -1;
staticRear = -1;
staticFront = 0;
} else {
staticFront++;
void deleteRearStatic() {
if (staticFront == -1) {
if (staticFront == staticRear) {
staticFront = -1;
staticRear = -1;
} else if (staticRear == 0) {
staticRear = MAX_SIZE - 1;
} else {
staticRear--;
void displayStatic() {
if (staticFront == -1) {
} else {
int i;
} else {
}
printf("\n");
struct Node {
int data;
};
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
if (dynamicFront == NULL) {
} else {
newNode->next = dynamicFront;
dynamicFront->prev = newNode;
dynamicFront = newNode;
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
if (dynamicFront == NULL) {
dynamicFront = dynamicRear = newNode;
} else {
dynamicRear->next = newNode;
newNode->prev = dynamicRear;
dynamicRear = newNode;
void deleteFrontDynamic() {
if (dynamicFront == NULL) {
} else {
dynamicFront = dynamicFront->next;
if (dynamicFront != NULL) {
dynamicFront->prev = NULL;
} else {
dynamicRear = NULL;
free(temp);
void deleteRearDynamic() {
if (dynamicFront == NULL) {
} else {
dynamicRear = dynamicRear->prev;
if (dynamicRear != NULL) {
dynamicRear->next = NULL;
} else {
dynamicFront = NULL;
free(temp);
void displayDynamic() {
if (dynamicFront == NULL) {
} else {
temp = temp->next;
printf("\n");
int main() {
while (1) {
printf("\n----------------------\n");
printf("Menu:\n");
printf("11. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
insertFrontStatic(value);
break;
case 2:
scanf("%d", &value);
insertRearStatic(value);
break;
case 3:
deleteFrontStatic();
break;
case 4:
deleteRearStatic();
break;
case 5:
displayStatic();
break;
case 6:
insertFrontDynamic(value);
break;
case 7:
scanf("%d", &value);
insertRearDynamic(value);
break;
case 8:
deleteFrontDynamic();
break;
case 9:
deleteRearDynamic();
break;
case 10:
displayDynamic();
break;
case 11:
exit(0);
default:
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;
};
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (root == NULL) {
return createNode(value);
return root;
return root;
} else {
if (root != NULL) {
inorderTraversal(root->left);
inorderTraversal(root->right);
int main() {
// Insertion
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// Searching
if (searchResult != NULL) {
// 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;
};
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (root != NULL) {
inorderTraversal(root->left);
inorderTraversal(root->right);
if (root != NULL) {
preorderTraversal(root->left);
preorderTraversal(root->right);
postorderTraversal(root->left);
postorderTraversal(root->right);
int main() {
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
inorderTraversal(root);
printf("\n");
preorderTraversal(root);
printf("\n");
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>
if (arr[i] == target) {
return i;
return -1;
if (arr[mid] == target) {
return mid;
low = mid + 1;
} else {
high = mid - 1;
return -1;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
int target;
scanf("%d", &target);
if (linearSearchIndex != -1) {
} else {
if (binarySearchIndex != -1) {
} else {
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>
void convertToTriplet(int matrix[][MAX_SIZE], int rows, int cols, int triplet[][3], int* numTerms) {
int count = 0;
if (matrix[i][j] != 0) {
triplet[count][0] = i;
triplet[count][1] = j;
triplet[count][2] = matrix[i][j];
count++;
*numTerms = count;
int zeroCount = 0;
if (matrix[i][j] == 0) {
zeroCount++;
return 1;
}
return 0;
printf("\n");
printf("Triplet Matrix:\n");
int main() {
int matrix[MAX_SIZE][MAX_SIZE];
scanf("%d", &matrix[i][j]);
printf("Matrix:\n");
int numTerms;
displayTriplet(triplet, numTerms);
} else {
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;
};
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
return newNode;
if (*poly == NULL) {
*poly = newNode;
} else {
temp = temp->next;
temp->next = newNode;
if (temp == NULL) {
printf("Polynomial is empty.\n");
return;
temp = temp->next;
if (temp != NULL) {
printf("+ ");
printf("\n");
temp1 = temp1->next;
temp2 = temp2->next;
} else {
if (sum != 0) {
temp1 = temp1->next;
temp2 = temp2->next;
}
}
temp1 = temp1->next;
temp2 = temp2->next;
return result;
int main() {
insertTerm(&poly1, 5, 2);
insertTerm(&poly1, 3, 0);
insertTerm(&poly2, 2, 3);
insertTerm(&poly2, 1, 2);
printf("Polynomial 1: ");
displayPolynomial(poly1);
printf("Polynomial 2: ");
displayPolynomial(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;
};
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
void splitLinkedList(struct Node* head, struct Node** firstList, struct Node** secondList) {
if (count % 2 == 0) {
insertNode(firstList, current->data);
} else {
insertNode(secondList, current->data);
count++;
current = current->next;
current = current->next;
printf("\n");
int main() {
insertNode(&head, input[i]);
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];
};
void insertStudent(struct Student** head, char name[], int rollno, int marks[]) {
strcpy(newNode->name, name);
newNode->rollno = rollno;
float sum = 0;
sum += marks[i];
newNode->average = sum / 5;
strcpy(newNode->result, "Fail");
} else {
strcpy(newNode->result, "Pass");
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
printf("Marks: ");
printf("\n");
printf("\n");
current = current->next;
int main() {
struct Student* head = NULL;
displayStudents(head);
return 0;
OUTPUT
Q16. Write a program to convert Infix to equivalent
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>
struct Stack {
int top;
int items[MAX_SIZE];
};
stack->top = -1;
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
stack->items[++stack->top] = item;
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
return stack->items[stack->top--];
initialize(&stack);
if (isdigit(expression[i])) {
} else {
op1 = pop(&stack);
op2 = pop(&stack);
switch (expression[i]) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
printf("Invalid Expression\n");
return -1;
}
push(&stack, result);
return pop(&stack);
initialize(&stack);
if (isdigit(expression[i])) {
} else {
op2 = pop(&stack);
op1 = pop(&stack);
switch (expression[i]) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
printf("Invalid Expression\n");
return -1;
push(&stack, result);
return pop(&stack);
int main() {
char prefixExpression[MAX_SIZE];
char postfixExpression[MAX_SIZE];
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>
struct Stack {
int top;
int items[MAX_SIZE];
};
stack->top = -1;
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
stack->items[++stack->top] = item;
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
return stack->items[stack->top--];
return stack->top + 1;
push(stack, patientId);
int main() {
initialize(&stack);
do {
printf("\n----------------------\n");
printf("3. Exit\n");
printf("----------------------\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &patientId);
generateCoupon(&stack, patientId);
break;
case 2:
displayCount(&stack);
break;
case 3:
printf("Exiting program.\n");
break;
default:
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;
};
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
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);
inorderTraversal(root->right);
if (root == NULL) {
return;
preorderTraversal(root->left);
preorderTraversal(root->right);
if (root == NULL) {
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
int main() {
char postfixExpression[100];
printf("\n");
preorderTraversal(root);
printf("\n");
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>
typedef struct {
int data[MAX_SIZE];
} TwoStacks;
stacks->data[++stacks->top1] = item;
} else {
} else {
if (stacks->top1 >= 0) {
return stacks->data[stacks->top1--];
} else {
return stacks->data[stacks->top2++];
} else {
}
int main() {
TwoStacks stacks;
initializeTwoStacks(&stacks);
pushStack1(&stacks, 10);
pushStack1(&stacks, 20);
pushStack2(&stacks, 30);
pushStack2(&stacks, 40);
return 0;
OUTPUT