Wednesday, 12 August 2015

Write a C Program To Implement Stack program using An Array For BCA , MCA . MScIT , Engineering

#include<stdio.h>
#include<conio.h>
int stack[10],top=-1; //global declaration
void push();  //declaring functions
void pop();
void display();
void main()
{
  int ch;
  printf(">>> StackOperations <<<\n\n");
  do
  {
      printf("\n 1.Push");
      printf("\n 2.Pop");
      printf("\n 3.Display");
      printf("\n 4.Exit");
      printf("\n Enter your choice: ");
      scanf("%d",&ch);
      if(ch==1)
        push();
      else if(ch==2)
        pop();
      else if(ch==3)
        display();
      else if(ch==4)
       printf("\n\nEnd Of program");
 
  }while(ch!=4);
}
//to push elements in a stack
void push()
{
  if(top>9)
    printf("\n Stack Overflow");
  else
  {
      top=top+1;
      printf("\n Enter a value: ");
      scanf("%d",&stack[top]);
  }
 
}
 
//to pop/delete stack elements
void pop()
{
  if(top==-1)
   printf("\n Stack Overflow. No Elements to pop");
  else
  {
      printf("%d is deleted..",stack[top]);
      top--;  
  }
}
 
//displaying stack elements
void display()
{
    int i;
    if(top==-1)
      printf("\n Stack underflow. No elements to display..");
    else
    {
        printf("\n The stack elements are: ");
        for(i=0;i<=top;i++)
        {
          printf("\t %d",stack[i]);
        }
 
    }
}

No comments:

Post a Comment