Implementation Of Stack Operation Using Array – Data Structures in C Program

#include <stdio.h>
#include <stdlib.h>

int top = -1;

void push(int *stack,int data){
    if (top==9){
        printf("Stack Overflown");
        return;
    }
    *(stack+(++top))=data;
    printf("Push Successfulln");
}

void pop(int* stack){
    if (top==-1){
        printf("Stack Underflow!n");
        return;
    }
    top--;
    printf("Pop Successfulln");
}

void peek(int* stack){
    if (top==-1){
        printf("Stack Emptyn");
        return;
    }
    printf("%d",stack[top]);
}

void print(int* stack){
    if (top==-1){
        printf("Stack Emptyn");
        return;
    }
    for(int i=top;i>=0;i--){
        printf("%d ",stack[i]);
    }
}


void main(){
    int stack[10];
    int data, choice;
    while(1){
        printf("n1.PUSHn2.POPn3.PEEKn4.PRINTn99.EXITnSelect Option>> ");
        scanf("%d",&choice);
        switch(choice){
            case 1: printf("Enter Data>> ");
                    scanf("%d",&data);
                    push(stack,data);
                    break;
            case 2: pop(stack);
                    break;
            case 3: peek(stack);
                    break;
            case 4: print(stack);
                    break;
            case 9: exit(0);
        }
    }
}

Rich Engineer - Study Materials, Tutorials, Updates and much more!
Leave a Reply