Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. /*Write a stack class from scratch to house integers using any programming language of your choosing that implements the following methods:
  2.  
  3. push(integer n) - adds the integer to top of the stack
  4. pop() - removes the top-most item from the stack
  5. peek() - gets the integer from top of the stack without removing it
  6. depth() - determines the number of integers in the stack
  7.  
  8. If a number, then the number is pushed onto the stack
  9. If the minus character (-), a pop operation is performed against the stack
  10. If the question mark character (?), a peek operation is performed
  11. If the hash character (#), a depth operation is performed
  12.  
  13. Inputs will not cause the stack to underflow
  14. The stack must support depth d, where d < 2,000,000 without overflowing
  15. -1048576000000 < n < 1048576000000
  16. Using a pre-existing stack class provided by the programming language, library, package, or framework is not acceptable
  17.  
  18. For an integer token, output the depth of the stack after the push operation
  19. For a minus character (-), output the integer popped from the stack
  20. For a question mark character (?), output the element peeked from the stack
  21. For a hash character (#), output the depth of the stack
  22.  
  23. Before any integer is pushed onto the stack, the stack is empty, and therefore has zero elements. Per the input format specification, and "0" should be printed.
  24. When token 34 is encountered, it is pushed onto the stack since it is an integer. Now stack has one element, and "1" should be printed.
  25. When token 25 is encountered, it is pushed ont othe stack as well. Stack now has two elements, and "2" should be printed.
  26. When token ? is encountered, it peeks at the stack, and prints "25" since that is the top-most element in the stack.
  27. When token -5 is encountered, it is pusehd onto the stack and "3" is printed.
  28. When token - is encountered, the top-most element is popped from the stack, and "-5" is printed
  29. When token # is encountered, "2" is printed because the stack now has two elements.
  30. When token ? is encountered, "25" is printed because it is at the top of the stack.
  31.  
  32. import java.io.*;
  33. import java.util.*;
  34. import java.lang.*;
  35. // stack size<= 2000000
  36. // it should hold integers -1048576000000 < n < 1048576000000
  37. public class StackThemUp {
  38. private static LinkedList<Long> list = new LinkedList<>();
  39.  
  40.  
  41. @SuppressWarnings("resource")
  42. public static void main(String[] args) {
  43. // Scanner scan = new Scanner(System.in);
  44. int stackSize;
  45. long lastInt;
  46. // String userInput = scan.nextLine();
  47. // scan.close();
  48.  
  49. String[] stackEntry = null;
  50. String line = "";
  51. // String userInput = scan.nextLine();
  52. // List<String> result = new ArrayList<>();
  53. try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
  54.  
  55. // while (line != null) {
  56.  
  57. line = reader.readLine();
  58. // stackEntry=line.split(" ");
  59. // }
  60. } catch (IOException exc) {
  61.  
  62. }
  63. // initializeStack(stackEntry.length);
  64.  
  65. for (String s : line.split(" ")) {
  66. try {
  67. if (s.equals("?")) {
  68. lastInt = peek();
  69. System.out.println(lastInt);
  70. } else if (s.equals("#")) {
  71. stackSize = depth();
  72. System.out.println(stackSize);
  73. } else if (s.equals("-")) {
  74. lastInt = peek();
  75. System.out.println(lastInt);
  76. pop();
  77. } else if (s.matches("[-]?\d+")) {
  78. push(Long.parseLong(s));
  79. stackSize = depth();
  80. System.out.println(stackSize);
  81. } else {
  82. throw new IllegalArgumentException();
  83. }
  84. } catch (IllegalArgumentException ex) {
  85. }
  86. }
  87. }
  88.  
  89. private static void push(Long number) {
  90. list.add(number);
  91. // stackTop++;
  92. }
  93.  
  94. private static Long peek() {
  95.  
  96. return list.getLast();
  97. }
  98.  
  99. private static int depth() {
  100. return list.size();
  101. }
  102.  
  103. private static void pop() {
  104. if (list.size() > 0)
  105. list.removeLast();
  106. // stackTop--;
  107.  
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement