Stack

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #define max 3  
4:  int top=0, stack[max];  
5:  void push( );  
6:  void pop( );  
7:  void display( );  
8:  void main( )  
9:  {  
10:       int ch;  
11:  clrscr( );  
12:  while(1)  
13:  {  
14:       printf(“\n”);  
15:       printf(“\n 1. PUSH”);  
16:       printf(“\n 2. POP”);  
17:       printf(“\n 3. DISPLAY”);  
18:       printf(“\n 4. EXIT”);  
19:       printf(“\n Enter your choice  : ”);  
20:       scanf(“%d”, &ch);  
21:       switch(ch)  
22:       {  
23:            case 1       :      push( );  
24:                          break;  
25:            case 2       :     pop( );  
26:                      break;  
27:            case 3   :     display( );  
28:                      break;  
29:            case 4   :      exit(1);  
30:            default  :   printf(“\n INVALID CHOICE”);  
31:       }  
32:  }  
33:  getch( );  
34:  }  
35:  void push( )  
36:  {  
37:       int x;  
38:       if(top==max)  
39:            printf(“\nStack OVERFLOWS”);  
40:       else  
41:       {  
42:            printf(“\n Enter the element to be pushed in stack : ”);  
43:            scanf(“%d”, &x);  
44:            top++;  
45:            stack[top]=x;  
46:       }  
47:  }  
48:  void pop( )  
49:  {  
50:       if(top==0)  
51:            printf(“Stack Underflows”);  
52:       else  
53:       {  
54:            printf(“\nPopped element is %d ”, stack[top]);  
55:            top--;  
56:       }  
57:  }  
58:  void display( )  
59:  {  
60:       int I;  
61:       if(top==0)  
62:            printf(“Stack is empty”);  
63:       else  
64:       {  
65:            printf(“Stack contains \n”);  
66:            for(i=1; i<=top; i++)  
67:                 printf(“%d \t” , stack[i]);  
68:       }  
69:  }  

No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive