C Program for Implementation of Stack Using Array

CODE:
#include "stdio.h"
#include "conio.h"

#define size 5

int stack[size] = {0};

int top = -1;

void push(int value)
{
 if(top < size)
 {
  top++;
  stack[top] = value;
 }
 else
 {
  printf("Stack overflow \n");
 }
}

void pop()
{
 if(top >= 0)
 {
  printf("The popped element is:\t %d \n", stack[top]);
  stack[top] = 0;
  top--;
 }

 else
 {
  printf("Stack underflow \n");
 }


}
void display()
{

                 int i;

  for(i=size - 1;i>=0;i--)
  {
   printf(" %d \n",stack[i]);
  }
}

void tos()
 {
  printf("The value at top is %d", stack[top]);
 }

int main()
{

 int i,j, ch=0 ,value = 0;

 while(1)
 {
  printf("\n Please Enter the choice. \n");
  printf("1 for push\n 2 for pop \n 3 for display\n 4 for top \t  \n 5 for exit");
  scanf("%d",&ch);
 
  switch(ch)
  {
   case 1:
    printf("\n Please Enter the number to be inserted: \t");
    scanf("%d", &value);
    push(value);
    break;
   
   case 2:
   
    pop();
   
    break;
 
   case 3:
   
    display();
   
    break;
   
 
   case 4:
    tos();
   
    break;
   
   case 5:
   
    exit(0);
   
 
 
  }
 }
 

}


Contact Us:
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

Total Pageviews

CONTACT US

Prof. Roshan P. Helonde
Mobile: +917276355704
WhatsApp: +917276355704
Email: roshanphelonde@rediffmail.com

Enter Project Title

Popular Projects

All Archive

Contact Form

Name

Email *

Message *