Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. int max = 5;
  6. int top = -1;
  7. int[] stack = new int[max];
  8.  
  9. public static void main(String[] args) {
  10. Scanner input = new Scanner(System.in);
  11. Main main = new Main();
  12.  
  13. while (true) {
  14. System.out.println("1. Push\n2. Pop\n3. Show");
  15. int choice = input.nextInt();
  16.  
  17. if (choice == 1) {
  18. if (main.top == main.max - 1) {
  19. System.out.println("Overflow!");
  20. }
  21. else {
  22. System.out.print("Input the value: ");
  23. int value = input.nextInt();
  24. main.push(value);
  25. System.out.println("Done!");
  26. }
  27. }
  28. else if (choice == 2) {
  29. if (main.top == -1) {
  30. System.out.println("Underflow!");
  31. }
  32. else {
  33. System.out.printf("Item deleted: %d", main.pop());
  34. }
  35. }
  36. else if (choice == 3) {
  37. System.out.print("The stack:\t");
  38. main.printStack();
  39. }
  40. else {
  41. break;
  42. }
  43. }
  44.  
  45. input.close();
  46. }
  47.  
  48. void printStack() {
  49. for (int i = 0; i <= top; i++) {
  50. System.out.printf("%d\t", stack[i]);
  51. }
  52. System.out.println();
  53. }
  54.  
  55. int pop() {
  56. int value = stack[top];
  57. stack[top] = 0;
  58. top--;
  59. return value;
  60. }
  61.  
  62. void push(int value) {
  63. top++;
  64. stack[top] = value;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement