Data structures(Stacks using arrays Program)

 

  1. #include <stdio.h>   
  2. int stack[100],i,j,choice=0,n,top=-1;  
  3. void push();  
  4. void pop();  
  5. void show();  
  6. void main ()  
  7. {  
  8.       
  9.     printf("Enter the number of elements in the stack ");   
  10.     scanf("%d",&n);  
  11.     printf("*********Stack operations using array*********");  
  12.   
  13. printf("\n----------------------------------------------\n");  
  14.     while(choice != 4)  
  15.     {  
  16.         printf("Chose one from the below options...\n");  
  17.         printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");  
  18.         printf("\n Enter your choice \n");        
  19.         scanf("%d",&choice);  
  20.         switch(choice)  
  21.         {  
  22.             case 1:  
  23.             {   
  24.                 push();  
  25.                 break;  
  26.             }  
  27.             case 2:  
  28.             {  
  29.                 pop();  
  30.                 break;  
  31.             }  
  32.             case 3:  
  33.             {  
  34.                 show();  
  35.                 break;  
  36.             }  
  37.             case 4:   
  38.             {  
  39.                 printf("Exiting....");  
  40.                 break;   
  41.             }  
  42.             default:  
  43.             {  
  44.                 printf("Please Enter valid choice ");  
  45.             }   
  46.         }
  47.     }  
  48. }   
  49.   
  50. void push ()  
  51. {  
  52.     int val;      
  53.     if (top == n )   
  54.     printf("\n Overflow");   
  55.     else   
  56.     {  
  57.         printf("Enter the value?");  
  58.         scanf("%d",&val);         
  59.         top = top +1;   
  60.         stack[top] = val;   
  61.     }   
  62. }   
  63.   
  64. void pop ()   
  65. {   
  66.     if(top == -1)   
  67.     printf("Underflow");  
  68.     else  
  69.     top = top -1;   
  70. }   
  71. void show()  
  72. {  
  73.     for (i=top;i>=0;i--)  
  74.     {  
  75.         printf("%d\n",stack[i]);  
  76.     }  
  77.     if(top == -1)   
  78.     {  
  79.         printf("Stack is empty");  
  80.     }  
  81. }  

Comments

Popular posts from this blog

PHP Array Functions

IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM

Tableau(Line Graphs)