From 3f7c6462b6a540c5f69cd7fec2210921de548f7d Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Wed, 7 Aug 2024 17:08:15 +0530 Subject: [PATCH] Update DS/C/Lab/Week3/MulSta.c --- DS/C/Lab/Week3/MulSta.c | 61 +++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/DS/C/Lab/Week3/MulSta.c b/DS/C/Lab/Week3/MulSta.c index 3e14b18..a759340 100644 --- a/DS/C/Lab/Week3/MulSta.c +++ b/DS/C/Lab/Week3/MulSta.c @@ -1,45 +1,48 @@ #include -#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; }