|
| 1 | +/* |
| 2 | + Title : Stack Operations : |
| 3 | + 1.PUSH 2.POP 3.DISPLAY 4.PEAK ELEMENT |
| 4 | + are performed using arrays . i.e Static memory allocation in c language . |
| 5 | + User need to choose the option number which user wants to perform . |
| 6 | + PUSH : To enter the data elements . |
| 7 | + POP : To POP Out the element (To delete). |
| 8 | + Peak : To get the top most element present on the stack. |
| 9 | + It follows first in last out principle . |
| 10 | + Node can be entered and can be deleted from one end only. |
| 11 | +*/ |
| 12 | +#include<stdio.h> |
| 13 | +#include<stdlib.h> |
| 14 | +void main() |
| 15 | +{ |
| 16 | + int size,opt,i,x; |
| 17 | + printf(" \n ******* WELCOME ******* : \n We are going to implement stack data structure : \n It follows LIFO (Last In First Out Principle .)\n"); |
| 18 | + printf("\n Enter the size of STACK : \n"); |
| 19 | + scanf("%d",&size); |
| 20 | + int top=-1; |
| 21 | + int arr[size]; |
| 22 | + |
| 23 | + do |
| 24 | + { printf("\n ---- 1.PUSH ---- \n ---- 2.DISPLAY ---- \n ---- 3.POP ---- \n ---- 4.Peak ---- \n ---- 5.Exit ---- "); |
| 25 | + printf("\n Enter the option number : \n"); |
| 26 | + scanf("%d",&opt); |
| 27 | + switch(opt) |
| 28 | + { |
| 29 | + case 1 : printf("\n PUSH "); |
| 30 | + if(top==size-1) |
| 31 | + { printf("\n STACK IS FULL ! \n"); } |
| 32 | + else |
| 33 | + { top++; |
| 34 | + printf("\n ENETR PUSH THE ELEMENT \n"); |
| 35 | + scanf("%d",&x); |
| 36 | + arr[top]=x; } |
| 37 | + break; |
| 38 | + case 2 : printf("\n DISPLAY "); |
| 39 | + if(top<=-1) |
| 40 | + { printf("\n The Stack is Empty ! "); |
| 41 | + top=-1; } |
| 42 | + else |
| 43 | + { printf("\n The elements are : " ); |
| 44 | + for(i=top;i>=0;i--) |
| 45 | + { printf("\n %d " , arr[i] ); } } |
| 46 | + break; |
| 47 | + case 3: printf("\n POP "); |
| 48 | + |
| 49 | + if(top<=-1){ printf("\n The Stack is Empty ! \n "); |
| 50 | + top=-1; } |
| 51 | + else { |
| 52 | + printf("\n Element popped out is : %d " ,arr[top]); |
| 53 | + top--; |
| 54 | + if(top<-1) { top=-1 ; } |
| 55 | + } |
| 56 | + break; |
| 57 | + |
| 58 | + case 4 : printf("\n Peak "); |
| 59 | + if(top<=-1) |
| 60 | + { printf("\n No element is at the top of the stack . \n Empty stack !"); |
| 61 | + top=-1; } |
| 62 | + |
| 63 | + else |
| 64 | + { printf("\n The present top/peak element of the stack : %d ",arr[top]); } |
| 65 | + break ; |
| 66 | + case 5 : |
| 67 | + exit(0); |
| 68 | + break ; |
| 69 | + default : printf("\n Invalid Option !!\n Please try again . "); |
| 70 | + break; |
| 71 | + |
| 72 | + } |
| 73 | + printf("\n DO YOU WISH TO CONTINUE ? \n 1.Yes \n 2.No\n"); /* Enter 1 or other tahn 0 to continue otherwise enter 2 . */ |
| 74 | + scanf("%d",&opt); |
| 75 | + |
| 76 | + } |
| 77 | + while(opt!=2); |
| 78 | +} |
| 79 | +/* |
| 80 | +Enter the size of STACK : |
| 81 | +2 |
| 82 | +
|
| 83 | +---- 1.PUSH ---- |
| 84 | +---- 2.DISPLAY ---- |
| 85 | +---- 3.POP ---- |
| 86 | +---- 4.Peak ---- |
| 87 | +---- 5.Exit ---- |
| 88 | +Enter the option number : |
| 89 | +1 |
| 90 | +
|
| 91 | + PUSH |
| 92 | +ENETR PUSH THE ELEMENT |
| 93 | +1 |
| 94 | +
|
| 95 | +DO YOU WISH TO CONTINUE ? |
| 96 | +1.Yes |
| 97 | +2.No |
| 98 | +1 |
| 99 | +
|
| 100 | +---- 1.PUSH ---- |
| 101 | +---- 2.DISPLAY ---- |
| 102 | +---- 3.POP ---- |
| 103 | +---- 4.Peak ---- |
| 104 | +---- 5.Exit ---- |
| 105 | +Enter the option number : |
| 106 | +1 |
| 107 | +
|
| 108 | + PUSH |
| 109 | +ENETR PUSH THE ELEMENT |
| 110 | +2 |
| 111 | +
|
| 112 | +DO YOU WISH TO CONTINUE ? |
| 113 | +1.Yes |
| 114 | +2.No |
| 115 | +1 |
| 116 | +
|
| 117 | +---- 1.PUSH ---- |
| 118 | +---- 2.DISPLAY ---- |
| 119 | +---- 3.POP ---- |
| 120 | +---- 4.Peak ---- |
| 121 | +---- 5.Exit ---- |
| 122 | +Enter the option number : |
| 123 | +1 |
| 124 | +
|
| 125 | + PUSH |
| 126 | +STACK IS FULL ! |
| 127 | +
|
| 128 | +DO YOU WISH TO CONTINUE ? |
| 129 | +1.Yes |
| 130 | +2.No |
| 131 | +1 |
| 132 | +
|
| 133 | +---- 1.PUSH ---- |
| 134 | +---- 2.DISPLAY ---- |
| 135 | +---- 3.POP ---- |
| 136 | +---- 4.Peak ---- |
| 137 | +---- 5.Exit ---- |
| 138 | +Enter the option number : |
| 139 | +2 |
| 140 | +
|
| 141 | + DISPLAY |
| 142 | +The elements are : |
| 143 | +2 |
| 144 | +1 |
| 145 | +DO YOU WISH TO CONTINUE ? |
| 146 | +1.Yes |
| 147 | +2.No |
| 148 | +1 |
| 149 | +
|
| 150 | +---- 1.PUSH ---- |
| 151 | +---- 2.DISPLAY ---- |
| 152 | +---- 3.POP ---- |
| 153 | +---- 4.Peak ---- |
| 154 | +---- 5.Exit ---- |
| 155 | +Enter the option number : |
| 156 | +3 |
| 157 | +
|
| 158 | + POP |
| 159 | +Element popped out is : 2 |
| 160 | +DO YOU WISH TO CONTINUE ? |
| 161 | +1.Yes |
| 162 | +2.No |
| 163 | +1 |
| 164 | +
|
| 165 | +---- 1.PUSH ---- |
| 166 | +---- 2.DISPLAY ---- |
| 167 | +---- 3.POP ---- |
| 168 | +---- 4.Peak ---- |
| 169 | +---- 5.Exit ---- |
| 170 | +Enter the option number : |
| 171 | +4 |
| 172 | +
|
| 173 | + Peak |
| 174 | +The present top/peak element of the stack : 1 |
| 175 | +DO YOU WISH TO CONTINUE ? |
| 176 | +1.Yes |
| 177 | +2.No |
| 178 | +1 |
| 179 | +
|
| 180 | +---- 1.PUSH ---- |
| 181 | +---- 2.DISPLAY ---- |
| 182 | +---- 3.POP ---- |
| 183 | +---- 4.Peak ---- |
| 184 | +---- 5.Exit ---- |
| 185 | +Enter the option number : |
| 186 | +5 |
| 187 | +*/ |
0 commit comments