C program to implement stack | Push Pop Operation on Stack Using Functions

Copy the Code 
/*     c program to implement stack with struct and functions.
 *
 *     BY :- KHUSHIT SHAH, khushitshah1@gmail.com
 *
 */
#include<stdio.h>
#include<conio.h> // not necessary.
#include<process.h>
#define MAX 10
struct Stack{
    int top;
    int data[MAX];
}stack;

void push(){
    if(stack.top == MAX-1){
        printf("Stack Overflow \n");
        return;
    }
    else{
        int item;
        printf("Enter the value of data item : ");
        scanf("%d",&item);
        stack.data[++stack.top] = item;
        printf("%d is added at place %d \n",stack.data[stack.top],stack.top);
    }
}
void pop(){
    if(stack.top == -1){
        printf("Stack Undeflow\n");
        return;
    }else{
        int value = stack.data[stack.top--];
        printf("%d is popped from position %d \n",value , stack.top + 1);
    }
}
void display(){
    int i;
    for(i = stack.top ; i >=0 ; i--){
        printf("%d , " ,stack.data[i]);
    }
    printf("\n");
}

int main(){
    int choice;
    // initializing neccesary data.
    stack.top = -1;
    clrscr ();
    while(1){
        // Menu driven program.
        printf("--------------------------------------\n");
        printf(" Enter your choice from below : \n");
        printf("\t 1. for push.\n");
        printf("\t 2. for pop. \n");
        printf("\t 3. for display. \n");
        printf("\t 4. for exit. \n");
        printf("---------------------------------------\n");

        // taking the choice.
        scanf("%d",&choice);

        switch(choice){
            case 1:
                push();
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                exit(0);
                break;
            default:
                printf("Please Enter a Valid Choice\n");
        }
    }
    getch();
    return 0;
}

OR
  download it from here.

Comments

Popular posts from this blog

NPTEL Introduction to programming in c Assignment 4 solutions

Code to Implement Dynamic Queue in C++ using arrays

C PROGRAM TO ECHO "*" - C PROGRAM TO GET PASSWORD FROM USER