Update DS/C/Lab/Week3/MulSta.c

This commit is contained in:
Aadit Agrawal 2024-08-07 17:08:15 +05:30
parent 5065f1f758
commit 3f7c6462b6

View File

@ -1,45 +1,48 @@
#include <stdio.h>
#define MAX_SIZE 100
#define NUM_STACKS 3
#define MAX 100
int tops[NUM_STACKS] = {-1, -1, -1};
int stack[MAX_SIZE];
int stack_size = MAX_SIZE / NUM_STACKS;
int top[NUM_STACKS];
int stack[NUM_STACKS][MAX];
void push(int stack_num, int value) {
int index = stack_num * stack_size + tops[stack_num] + 1;
if (tops[stack_num] < stack_size - 1) {
tops[stack_num]++;
stack[index] = value;
printf("Pushed %d to stack %d\n", value, stack_num);
} else {
printf("Stack %d is full\n", stack_num);
void push(int stack_index, int value) {
if (top[stack_index] == MAX - 1) {
printf("Stack is full\n");
return;
}
stack[stack_index][++top[stack_index]] = value;
}
int pop(int stack_num) {
int index = stack_num * stack_size + tops[stack_num];
if (tops[stack_num] >= 0) {
int value = stack[index];
tops[stack_num]--;
printf("Popped %d from stack %d\n", value, stack_num);
return value;
} else {
printf("Stack %d is empty\n", stack_num);
int pop(int stack_index) {
if (top[stack_index] == -1) {
printf("Stack is empty\n");
return -1;
}
return stack[stack_index][top[stack_index]--];
}
int main() {
push(0, 10);
push(0, 20);
push(1, 30);
push(2, 40);
for (int i = 0; i < NUM_STACKS; i++) {
top[i] = -1;
}
pop(0);
pop(1);
pop(2);
pop(2);
for (int i = 0; i < NUM_STACKS; i++) {
printf("Enter elements for Stack %d:\n", i + 1);
for (int j = 0; j < 5; j++) {
int value;
scanf("%d", &value);
push(i, value);
}
}
for (int i = 0; i < NUM_STACKS; i++) {
printf("Stack %d: ", i + 1);
for (int j = 0; j <= top[i]; j++) {
printf("%d ", stack[i][j]);
}
printf("\n");
}
return 0;
}