Stack implementation using array

1:  #include <stdio.h>  
2:  #include <stdlib.h>  
3:  #include <dos.h>  
4:  #define SIZE 5  
5:  int STACK[SIZE];  
6:  int top=-1;  
7:  int main(void)  
8:  {  
9:       int option;  
10:    void push(void);  
11:    void pop(void);  
12:    void peek(void);  
13:       while(1)  
14:       {  
15:            system("CLS");  
16:            printf("\nPUSH PRESS 1: ");  
17:            printf("\nPOP PRESS 2: ");  
18:            printf("\nPEEK PRESS 3: ");  
19:            printf("\nEXIT PRESS 4: ");  
20:            scanf("%d",&option);  
21:            switch(option)  
22:            {  
23:                 case 1: push();  
24:                           break;  
25:                 case 2: pop();  
26:                           break;  
27:                 case 3: peek();  
28:                           break;                 
29:                 case 4: return EXIT_SUCCESS;  
30:                 default: printf("INVALID OPTION\n");  
31:                            system("PAUSE");  
32:            }  
33:       }  
34:  }  
35:  void push()  
36:  {  
37:    int data;  
38:    if(top==SIZE-1)  
39:    {  
40:          printf("STACK IS OVERFLOW.....\n");  
41:          system("PAUSE");  
42:          return;  
43:    }  
44:    printf("Enter data: ");  
45:    scanf("%d",&data);  
46:    ++top;  
47:    STACK[top]=data;         
48:  }  
49:  void pop()  
50:  {  
51:       if(top==-1)  
52:       {  
53:            printf("STACK IS UNDER FLOW.....\n");  
54:            system("PAUSE");  
55:            return;  
56:       }  
57:       printf("POP ELEMENT IS:%d\n",STACK[top]);  
58:       --top;  
59:       system("PAUSE");  
60:  }  
61:  void peek()  
62:  {  
63:       int i;  
64:       if(top==-1)  
65:       {  
66:            printf("STACK IS EMPTY....\n");  
67:            system("PAUSE");  
68:            return;  
69:       }  
70:       printf("STACK ELEMENTS: ");  
71:       for(i=top; i>=0; i--)  
72:        printf("%d ",STACK[i]);  
73:       printf("\n");  
74:       system("PAUSE");  
75:  }  

No comments:

Post a Comment