Welcome To Uday Satya Blog

12 . STACK ARRAY


/* **************************************
                 12.Basic Operations on Stack
    ************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*stdlib.h>
int a[20],top;
void main()
{
int ch,n,data;
void push(int);
void pop();
void display();
void option();
clrscr();
printf("\n enter the size of the stack : ");
scanf("%d",&n);
top=0;
 do
{
option();
printf("\n\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : if(top>=n)
            {
             printf("\n stack size is over flown");
             }
             else
             {
              printf("\n enter the data into the stack : ");
              scanf("%d",&data);
              push(data);
              }
              getch();
              break;
case 2 :  if(top==0)
             {
              printf("\n stack is empty");
              }
              else
              {
              pop();
              getch();
              }
              break;
 case 3 :  if(top==0)
            {
              printf("\n stack is empty");
              }
              else
              {
              display();
              }
              break;
case 4 :  printf("\n end of the program ");
              exit(1);
              break;
}
}while((ch>0)&&(ch<5));
getch();
}

void option()
{
printf("\n");
printf("\n 1 : push");
printf("\n 2 : pop");
printf("\n 3 : display");
printf("\n 4 : exit");
}

void push(int data)
{
a[top]=data;
top++;
printf("\n the pushed element is : %d",data);
}

void pop()
{
top--;
printf("\n the popped element is : %d ",a[top]);
}

 void display()
{
int i;
printf("\n the elements of the stack are :");
for(i=0;i
{
printf("\t%d",a[i]);
}
}

 Outputs

 enter the size of the stack : 3
 1 : push
 2 : pop
 3 : display
 4 : exit

enter your choice : 1

enter the data into the stack : 1

the pushed element is : 1

  1 : push
  2 : pop
  3 : display
  4 : exit

 enter your choice : 1

enter the data into the stack : 2

 the pushed element is : 2

1 : push
2 : pop
3 : display
4 : exit

  enter your choice : 1

 enter the data into the stack : 3

 the pushed element is : 3

 1 : push
 2 : pop
 3 : display
 4 : exit

 enter your choice : 3

 the elements of the stack are :        1       2       3

 1 : push
 2 : pop
 3 : display
 4 : exit

 enter your choice : 1
stack size is over flown

  1 : push
  2 : pop
  3 : display
  4 : exit























 enter your choice : 2

 the popped element is : 3

 1 : push

 2 : pop

 3 : display

 4 : exit

 enter your choice : 2

 the popped element is : 2



 1 : push

 2 : pop

 3 : display

 4 : exit



 enter your choice : 2



 the popped element is : 1



 1 : push

 2 : pop

 3 : display

 4 : exit

 enter your choice : 2

 stack is empty

 1 : push

 2 : pop

 3 : display

 4 : exit

 enter your choice : 4

 end of the program

0 comments:

Post a Comment