Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h> //required for using malloc() at line 19
- int size, count; //Global variables are initialized to 0
- void push(int stack[], int value){
- stack[count++] = value;
- }
- void pop(int stack[]){
- count--;
- }
- void display(int stack[]){
- if(count == 0) printf("\nThe stack is empty.");
- else for(int i = count-1; i >= 0; i--) printf("%d\n", stack[i]);
- if(count == size) printf("\nThe stack is full.");
- }
- int main(){
- int choice, n;
- printf("\nEnter the size of the stack: ");
- scanf("%d", &size);
- int *stack = (int*) malloc(sizeof(int) * size); //A better alternative to int stack[100];
- while(1){
- display(stack);
- printf("\n1.Push 2.Pop 3.Exit: ");
- scanf("%d", &choice);
- switch(choice){
- case 1: if(count != size){
- printf("Enter the number to push: ");
- scanf("%d", &n);
- push(stack, n);
- } else printf("\nError: Overflow\n");
- break;
- case 2: if(count != 0) pop(stack);
- else printf("\nError: Underflow.\n");
- break;
- case 3: return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement