Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int size, count; //count represents number of elements in the stack
- int* create(int size){
- printf("\nA stack of size %d is being created.", size);
- return (int*) malloc(sizeof(int) * size);//size to be allocated = size of int * no. of ints
- }
- 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]);//display from to to bottom
- if(count == size) printf("\nThe stack is full.");
- }
- int main(){
- int choice, n;
- int *stack = NULL;
- printf("\nEnter the size of the stack: ");
- scanf("%d", &size);
- stack = create(size);
- while(1){
- display(stack);
- printf("\n1.Push 2.Pop 3.Exit: ");
- scanf("%d", &choice);
- switch(choice){
- case 1: if(count != size){//If the stack is not full
- 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