C program:Implementation of Stack using Array

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

PROGRAM:

//Stack by Vikas Konparthi
#include <stdio.h>
#include<stdlib.h>

int stack[30],top,size;
void create();
void push();
void pop();
void display();
void create()
{
    top=-1;
}
void push()
{
    int val;
    if(top==size-1)
    printf("\n stack is full");
    else
    {
        printf("\n Enter a element to push");
        scanf("%d",&val);
        top++;
        stack[top]=val;
    }
}
void pop()
{
    if(top==-1)
    printf("\n Stack is Empty");
    else
    printf("\n The deleted element is %d",stack[top]);
    top--;
}
void display()
{
    int i;
    if(top==-1)
    printf("\n Stack is empty");
    else
    {
    printf("\n Stack is ...\t");
    for(i=top;i>=0;--i)
    printf("%d\n",stack[i]);
    }
}
void main()
{
    int ch;
    printf("\n Enter the size");
    scanf("%d",&size);
    while(1)
    {
        printf("\n1:Create\n2:Push\n3:Pop\n4:Display\n5:Exit");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:create();
            break;
            case 2:
            push();
            break;
            case 3:
            pop();
            break;
            case 4:
            display();
            break;
            case 5:
            exit(0);
            break;
         
        }
    }
}



Previous Post Next Post