C Saved Program: majestic-af

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 100
void push(int st[]);
void pop();
void peek(int st[]);
void display(int st[]);
int st[max]; int s;
int top=-1;
int main()
{
    int ch;
    printf("enter size:\n");
    scanf("%d",&s);
    while(1)
    {
      printf("enter choice:\n");
      scanf("%d",&ch);
      switch(ch)
      {
          case 1:push(st);
                 break;
          case 2:pop(st);
                 break;
          case 3:peek(st);
                 break;
          case 4:display(st);
                 break;
          case 5:exit(0);
                 break;
      }
    }
    return 0;
}
void push(int st[])
{
    int ele;
    printf("enter element:\n");
    scanf("%d",&ele);
    if(top==s-1)
    {
        printf("stack is full\n");
    }
    else
    {
        top++;
        st[top]=ele;
    }
}
void pop()
{
    if(top==-1)
    {
        printf("stack is empty\n");
    }
    else
    {
        top--;
    }

}
void peek(int st[])
{
    if(top==-1)
    {
        printf("stack is empty\n");
    }
    else
    {
       printf("%d",st[top]);
    }
}
voiddisplay(int st[])
{
    if(top==-1)
    {
        printf("stack is empty\n");
    }
    else
    {
       for(i=top;i>=0;i--)
       {
           printf("%d",st[i]);
       }
    }

}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs