Advertisement
AnindyaBiswas

temp

Jun 1st, 2022
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math;
  3. public class stac{
  4. public static void main(String Args[])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. stack_maker st = new stack_maker(5);
  8. st.Display();
  9. st.Push(1);
  10. st.Push(2);
  11. st.Display();
  12. System.out.println();
  13.  
  14. }
  15. }
  16.  
  17. class stack_maker {
  18. private int[] stack;
  19. private int top, size;
  20.  
  21. stack_maker(int size)
  22. {
  23. top = -1;
  24. this.size = size;
  25. stack = new int[size];
  26. }
  27.  
  28. boolean isFull()
  29. {
  30. if(top == size)
  31. return true;
  32. return false;
  33. }
  34.  
  35. boolean isEmpty()
  36. {
  37. if(top == -1)
  38. return true;
  39. return false;
  40. }
  41.  
  42. void Push(int n)
  43. {
  44. if(isFull())
  45. {
  46. System.out.println("Stack Overflow!");
  47. return;
  48. }
  49. stack[++top] = n;
  50. }
  51.  
  52. void Display()
  53. {
  54. if(isEmpty())
  55. {
  56. System.out.println("Empty Stack!");
  57. return;
  58. }
  59. for(int i = top; i >= 0; i--)
  60. System.out.println(stack[i]);
  61. }
  62.  
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement