Data Structure Definition: -
                                         1.          An array stack [MAX], where MAX is the maximum size of the array.
                                         2.          Initialization top = -1


Source Code:
/*******Program of stack using array *************************/
  1. #include<stdio.h>
  2. #define size 5
  3. int stack[size];
  4. int tos= -1;
  5.  
  6. void push();
  7. void pop();
  8. void display();
  9.  
  10. int main()
  11. {
  12.                 int ch,c;
  13.                 do
  14.                 {
  15.                               printf("\n1. push\n 2.pop \n3. exit\n Enter your choice:\t");
  16.                               scanf("%d",&ch);
  17.                              
  18.                               switch(ch)
  19.                               {
  20.                                              case 1:
  21.                                                     push();
  22.                                                     printf("\nstack:\t");
  23.                                                     display();
  24.                                              break;
  25.                                               
  26.                                              case 2:
  27.                                                       pop();
  28.                                                       printf("\nstack:\t");
  29.                                               display();
  30.                                              break;
  31.                                             
  32.                                              case 3:
  33.                                              break;
  34.                                             
  35.                                              default:
  36.                                                             printf(" invalid choice!");
  37.                                }
  38.                                printf("\nreopen the series:?press 1:");
  39.                                scanf("%d",&c);
  40.                } while(c==1);
  41.                 return 0;
  42. }
  43. void push()
  44.  {
  45.                 int data;
  46.                 if (tos,(size-1))
  47.                 { tos=tos+1;
  48.                   printf("\n Enter data:");
  49.                   scanf("%d",&data);
  50.                   stack[tos]=data;
  51.                   if(tos==(size-1))
  52.                      printf("\nstack is full");
  53.                  }
  54.                  else
  55.                    printf("\n overflow");
  56.  }
  57.  void pop()
  58.  {
  59.                int data;
  60.                if (tos==-1)
  61.                printf("\n underflow");
  62.                else
  63.                 {
  64.                               data=stack[tos];
  65.                               printf("\n popped=%d",data);
  66.                               tos=tos-1;
  67.                 }
  68.                 }
  69. void display()
  70. {
  71.                int i;
  72.                if(tos==-1)
  73.                 printf("stack is empty.");
  74.                 else
  75.                 {
  76.                               for(i=tos;i>=0;i--)
  77.                               {
  78.                                              printf("\n%d",stack[i]);
  79.                                }
  80.                 }
  81. }
  82.  

/********** End of Program ********************
OUTPUT:

1. push
 2.pop
3. exit
 Enter your choice:     1
 Enter data:12
stack:
12
reopen the series:?press 1:1

1. push
 2.pop
3. exit
 Enter your choice:     1

 Enter data:23

stack:
23
12
reopen the series:?press 1:1

1. push
 2.pop
3. exit
 Enter your choice:     1

 Enter data:34

stack:
34
23
12
reopen the series:?press 1:1

1. push
 2.pop
3. exit
 Enter your choice:     2

 popped=34
stack:
23
12
reopen the series:?press 1:1

1. push
 2.pop
3. exit
 Enter your choice:     2

 popped=23
stack:
12
reopen the series:?press 1:1

1. push
 2.pop
3. exit
 Enter your choice:     3



0 comments:

Post a Comment