Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package edu.brcc.maxfieldj.ch21Lab;
  2.  
  3. import java.io.PrintStream;
  4. import java.util.EmptyStackException;
  5. import java.util.Scanner;
  6.  
  7. public class GenericLinkedStack<E>
  8. {
  9. private class Node
  10. {
  11. E value;
  12. GenericLinkedStack<E>.Node next;
  13.  
  14. Node(GenericLinkedStack<E>.Node val)
  15. {
  16. this.value = val;
  17. this.next = n;
  18. }
  19. }
  20.  
  21. private GenericLinkedStack<E>.Node top = null;
  22.  
  23. public boolean empty()
  24. {
  25. return this.top == null;
  26. }
  27.  
  28. public void push(E s)
  29. {
  30. this.top = new Node(s, this.top);
  31. }
  32.  
  33. public E pop()
  34. {
  35. if (empty()) {
  36. throw new EmptyStackException();
  37. }
  38. E retValue = this.top.value;
  39. this.top = this.top.next;
  40. return retValue;
  41. }
  42.  
  43. public E peek()
  44. {
  45. if (empty()) {
  46. throw new EmptyStackException();
  47. }
  48. return this.top.value;
  49. }
  50.  
  51. public String toString()
  52. {
  53. StringBuilder sBuilder = new StringBuilder();
  54. GenericLinkedStack<E>.Node p = this.top;
  55.  
  56. sBuilder.append("[");
  57. while (p != null)
  58. {
  59. sBuilder.append(p.value);
  60. p = p.next;
  61. if (p != null) {
  62. sBuilder.append(" ");
  63. }
  64. }
  65. sBuilder.append("]");
  66. return sBuilder.toString();
  67. }
  68.  
  69. public static void main(String[] args)
  70. {
  71. GenericLinkedStack<String> st = new GenericLinkedStack();
  72. System.out.println("Pushing: Amy Bob Chuck");
  73. System.out.println("Contents of Stack:");
  74. st.push("Amy");
  75. st.push("Bob");
  76. st.push("Chuck");
  77. System.out.println(st);
  78. String name = (String)st.pop();
  79. System.out.println("Popped: " + name);
  80. System.out.println("Contents of Stack:");
  81. System.out.println(st);
  82.  
  83.  
  84.  
  85. GenericLinkedStack<Double> nums = new GenericLinkedStack();
  86. Scanner keyboard = new Scanner(System.in);
  87. System.out.println("RPN calculator");
  88. System.out.print(">");
  89. for (;;)
  90. {
  91. String input = keyboard.nextLine();
  92. double num2;
  93. double num1;
  94. try
  95. {
  96. double num = Double.parseDouble(input);
  97. nums.push(Double.valueOf(num));
  98. System.out.print(nums.peek() + ">");
  99. }
  100. catch (Exception e)
  101. {
  102. num2 = ((Double)nums.pop()).doubleValue();
  103. num1 = ((Double)nums.pop()).doubleValue();
  104. switch (input.charAt(0))
  105. {
  106. case '+':
  107. nums.push(Double.valueOf(num1 + num2));
  108. System.out.print(nums.peek() + ">");
  109. }
  110. }
  111. continue;
  112.  
  113. nums.push(Double.valueOf(num1 - num2));
  114. System.out.print(nums.peek() + ">");
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement