tampurus

2.1 Stack operation

Nov 25th, 2021 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. /*
  2. Input & Output :->
  3. 1 for Push
  4. 2 for Pop
  5. Any key to just print the stack
  6. 1
  7.  
  8. Enter the element 5
  9.  
  10. 1 - for repeat and any key to exit 1
  11.  
  12. 1 for Push
  13. 2 for Pop
  14. Any key to just print the stack
  15. 3
  16. 5
  17. 1 - for repeat and any key to exit -1
  18. */
  19. #include<stdio.h>
  20. #include<conio.h>
  21. #define Stack_Size 50
  22. int stack[Stack_Size],top;
  23. int Is_empty(){
  24.     if(top==-1)return 1;
  25.     else return 0;
  26. }
  27. int Is_full(){
  28.     if(top==Stack_Size-1) return 1;
  29.     else return 0;
  30. }
  31. void Push(int element){
  32.     stack[++top]=element;
  33. }
  34. void Pop(){
  35.     stack[top--]=0;
  36. }
  37. void Print(){
  38.     if(Is_empty()){
  39.         printf("There is no element to print");
  40.         return;
  41.     }
  42.     else{
  43.         for(int i=0 ; i<=top ; i++){
  44.             printf("%d ",stack[i]);
  45.         }
  46.     }
  47. }
  48. int main()
  49. {
  50.     top=-1;
  51.     int key,choice,element;
  52.     while(1){
  53.         printf("\n1 for Push \n2 for Pop\nAny key to just print the stack\n");
  54.         scanf("%d",&choice);
  55.         if(choice==1){
  56.             printf("\nEnter the element ");
  57.             scanf("%d",&element);
  58.             Push(element);
  59.         }
  60.         else if(choice==2){
  61.             Pop();
  62.         }
  63.         else Print();
  64.        
  65.         printf("\n1 - for repeat and any key to exit ");
  66.         scanf("%d",&key);
  67.         if(key!=1)break;
  68.     }
  69.     return 0;
  70. }
Add Comment
Please, Sign In to add comment