Untitled
unknown
plain_text
4 years ago
2.1 kB
4
Indexable
#include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> // A structure to represent a stack struct Stack { int top; unsigned capacity; int* array; }; // function to create a stack of given capacity. It initializes size of // stack as 0 struct Stack* createStack(unsigned capacity) { struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (int*)malloc(stack->capacity * sizeof(int)); return stack; } // Stack is full when top is equal to the last index int isFull(struct Stack* stack) { return stack->top == stack->capacity - 1; } // Stack is empty when top is equal to -1 int isEmpty(struct Stack* stack) { return stack->top == -1; } // Function to add an item to stack. It increases top by 1 void push(struct Stack* stack, int item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to stack\n", item); } // Function to remove an item from stack. It decreases top by 1 int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack->array[stack->top--]; } // Function to return the top from stack without removing it int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack->array[stack->top]; } // Driver program to test above functions int main() { struct Stack* stack = createStack(100); char exp[100]; int a, b; printf("Enter a valid postfixed expression (each number can be a positive digit only): "); scanf("%s", exp); for(int i = 0; i < strlen(exp); i++) { switch(expr[i]) { case '+': a = pop(); b = pop(); push(a + b); case '-': a = pop(); b = pop(); push(a - b); } } getch(); return 0; }
Editor is loading...