Stack Using Array

Praktek Mata Kuliah Data Structure

Stack on Array using DevC++
 
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define n 6

void push ();
void pop ();
void display ();

int array[n];
int top=-1;
int pick;

int main ()
{
    do
    {
    printf ("\n\nSelection Menu");
    printf ("\n1. Push Stack");
    printf ("\n2. Pop Stack");
    printf ("\n3. Display Stack");
    printf ("\nInsert your selection = ");
    scanf ("%d",&pick);
    switch (pick)
         {
         case 1:
              push ();
              break;
         case 2:
              pop ();
              break;
         case 3:
              display ();
              break;
         }
    }while (pick!=0);  
getche ();
return 0;
}

void push ()
{
     if (top<n-1)
     {
        top++;
        printf ("insert element to the stack = ");
        scanf ("%d",&array[top]);           
     }
     else
     {
         printf ("Stack is overflow");   
     }    
}

void pop ()
{
     if (top>-1)
     {
        top--;
        printf ("The Top Value = %d", array[top]);          
     }
     else
     {
         printf ("Stack is underflow");   
     }    
}

void display ()
{
     if (top>-1)
     {
        for (int i=top;i>=0;i--)
        {
            printf ("%d\n",array[i]);   
        }          
     }
     else
     {
         printf ("Stack is Empty");   
     }    
}