C Program For Stack Using Menu Driven Program In Switch Case

 /*Stack using array*/



if you get any error while executing in DosBox Press "Alt+R" Command and then press "Y"



#include<stdio.h>
#include<conio.h>
void main()
{
  int a[10]={0},i,top=-1,max=10,n;
  clrscr();
  printf("\n..........MENU.........\n1.push\n2.pop\n3.display\n4.exit\n-----------------------");
  do
  {
    printf("\nenter your choice\n");
    scanf("%d",&n);
    switch(n)
    {
      case 1:
      if(top==max-1)
      printf("over flow\n");
      else
      {
 printf("enter the element\n");
 scanf("%d",&i);
 a[++top]=i;
      }
      break;
      case 2:
      if(top<0)
      printf("under flow\n");
      else
      printf("the deleted item=%d",a[top--]);
      break;
      case 3:
      if(top<0)
      printf("stack is empty\n");
      else
      {
 printf("the elements of stack are\n");
 for(i=top;i>=0;i--)
 printf("%d\n",a[i]);
      }
      break;
      case 4:
      break;
      default:
      printf("invalid choice");
      break;
      }
      }
      while(n!=4);
      }


                                                   OR



#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 25
int top = -1;

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

int stack[MAX_SIZE];

void main()
{
int y, ch;
clrscr();
do
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("Enter the choice");
scanf("%d",&ch);

 switch(ch)
 {
 case 1:

    printf("Enter the value");
    scanf("%d",&y);

    push(y);
    break;
 case 2:
  pop();
  break;
 case 3:
 display();
  break;
 case 4:
  return;

 default:
   printf("Enter a valid option ( 1 to 4 ) \n");
   break;

 }
}while(ch!=4);
}

void push(int item)
{
 if (top==MAX_SIZE-1)
 {
    printf("Stack is full ! \n");
    return;
 }
   else
   {
      top = top + 1;
      stack[top] = item;
      printf("Pushed successfully %d \n", item);
   }
}

void pop()
{
  int x;

 if (top==-1)
 {
  printf("The stack is empty\n");
  return;
 }
 else
 {
   x = stack[top];                {
   printf("The element popped out is %d\n",x);
   top = top - 1;
 }

}

void display()
{
  int i;

  for(i=top;i>=0;i--)
  printf("%d\n",stack[i]);

}


Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors and Social Networking. Feel Free to let me know any of your concerns about hacking or let me know if you need any more methods on hacking anything. Enjoy Learning

No comments:

Post a Comment