Program to Perform different Stack Operations using Structure


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5

struct stack
{
int stack[max];
int top;
}
st;
void push();
void pop();
void display();

main()
{
 int choice1,choice2;
 st.top=-1;
 clrscr();

 do
 {
  printf("\n enter the operation \n");
  printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
  scanf("%d",&choice1);

  switch(choice1)
  {
   case 1:
   push();
   break;
   case 2:
   pop();
   break;
   case 3:
   display();
   break;
   case 4:
   exit(0);
   break;
   default:
   printf("\n Please enter a valid choice \n");
  }
  printf("\n Press 1 to Continue");
  scanf("%d",&choice2);
 }
  while(choice2==1);

  if(choice2!=1)
  exit(0);

  getch();
 }

void push()
{
 int item;
 if(st.top==(max-1))
 printf("\n Stack is full \n");
 else
 {
 printf("\n enter the item \n");
 scanf("%d",&item);

 st.top=st.top+1;
 st.stack[st.top]=item;
 printf("\n the pushed item is %d",st.stack[st.top]);
 }
}

void pop()
{
 int item;
 if(st.top==-1)
 printf("\n Stack is empty \n");
 else
 {
 item=st.stack[st.top];
 st.top=st.top-1;
 printf("\n the poped item is %d \n",item);
 }
}

void display()
{
 int i;
 printf("\n Displaying the elemets \n");

 for(i=0;i<=st.top;i++)
 printf("\n %d",st.stack[i]);
}






0 comments:

Post a Comment