Guest User

Untitled

a guest
Mar 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. ----Shape---
  2. /
  3. Rectangle Circle
  4. / /
  5. BlueRectangle RedRectangle BlueCircle RedCircle
  6.  
  7. ----Shape--- Color
  8. / /
  9. Rectangle(Color) Circle(Color) Blue Red
  10.  
  11. class Node {
  12. public int value;
  13. public Node prev, next;
  14.  
  15. public Node(int i) {
  16. value = i;
  17. }
  18. }
  19.  
  20. class Stack {
  21. private StackImpl impl;
  22.  
  23. public Stack( String s ) {
  24. if (s.equals("array")) {
  25. impl = new StackArray();
  26. } else if (s.equals("list")) {
  27. impl = new StackList();
  28. } else {
  29. System.out.println("Stack: unknown parameter");
  30. }
  31. }
  32.  
  33. public Stack() {
  34. this("array");
  35. }
  36.  
  37. public void push(int in) {
  38. impl.push( in );
  39. }
  40.  
  41. public int pop() {
  42. return impl.pop();
  43. }
  44.  
  45. public int top() {
  46. return impl.top();
  47. }
  48.  
  49. public boolean isEmpty() {
  50. return impl.isEmpty();
  51. }
  52.  
  53. public boolean isFull() {
  54. return impl.isFull();
  55. }
  56. }
  57.  
  58. class StackHanoi extends Stack {
  59. private int totalRejected = 0;
  60.  
  61. public StackHanoi() {
  62. super("array");
  63. }
  64.  
  65. public StackHanoi(String s) {
  66. super(s);
  67. }
  68.  
  69. public int reportRejected() {
  70. return totalRejected;
  71. }
  72.  
  73. public void push(int in) {
  74. if (!isEmpty() && in > top()) {
  75. totalRejected++;
  76. }
  77. else {
  78. super.push(in);
  79. }
  80. }
  81. }
  82.  
  83. class StackFIFO extends Stack {
  84. private StackImpl stackImpl = new StackList();
  85.  
  86. public StackFIFO() {
  87. super("array");
  88. }
  89.  
  90. public StackFIFO(String s) {
  91. super(s);
  92. }
  93.  
  94. public int pop() {
  95. while (!isEmpty()) {
  96. stackImpl.push(super.pop());
  97. }
  98. int ret = stackImpl.pop();
  99. while (!stackImpl.isEmpty()) {
  100. push(stackImpl.pop());
  101. }
  102. return ret;
  103. }
  104. }
  105.  
  106. interface StackImpl {
  107. void push(int i);
  108. int pop();
  109. int top();
  110. boolean isEmpty();
  111. boolean isFull();
  112. }
  113.  
  114. class StackArray implements StackImpl {
  115. private int[] items;
  116. private int total = -1;
  117.  
  118. public StackArray() {
  119. this.items = new int[12];
  120. }
  121.  
  122. public StackArray(int cells) {
  123. this.items = new int[cells];
  124. }
  125.  
  126. public void push(int i) {
  127. if (!isFull()) {
  128. items[++total] = i;
  129. }
  130. }
  131.  
  132. public boolean isEmpty() {
  133. return total == -1;
  134. }
  135.  
  136. public boolean isFull() {
  137. return total == items.length - 1;
  138. }
  139.  
  140. public int top() {
  141. if (isEmpty()) {
  142. return -1;
  143. }
  144. return items[total];
  145. }
  146.  
  147. public int pop() {
  148. if (isEmpty()) {
  149. return -1;
  150. }
  151. return items[total--];
  152. }
  153. }
  154.  
  155. class StackList implements StackImpl {
  156. private Node last;
  157.  
  158. public void push(int i) {
  159. if (last == null) {
  160. last = new Node(i);
  161. } else {
  162. last.next = new Node(i);
  163. last.next.prev = last;
  164. last = last.next;
  165. }
  166. }
  167.  
  168. public boolean isEmpty() {
  169. return last == null;
  170. }
  171.  
  172. public boolean isFull() {
  173. return false;
  174. }
  175.  
  176. public int top() {
  177. if (isEmpty()) {
  178. return -1;
  179. }
  180. return last.value;
  181. }
  182.  
  183. public int pop() {
  184. if (isEmpty()) {
  185. return -1;
  186. }
  187. int ret = last.value;
  188. last = last.prev;
  189. return ret;
  190. }
  191. }
  192.  
  193. public class BridgeDisk {
  194. public static void main(String[] args) {
  195. Stack[] stacks = {new Stack("array"), new Stack("list"),
  196. new StackFIFO(), new StackHanoi()};
  197. for (int i=1, num; i < 15; i++) {
  198. for (int j=0; j < 3; j++) {
  199. stacks[j].push( i );
  200. }
  201. }
  202. Random rn = new Random();
  203. for (int i=1, num; i < 15; i++) {
  204. stacks[3].push(rn.nextInt(20));
  205. }
  206. for (int i=0, num; i < stacks.length; i++) {
  207. while (!stacks[i].isEmpty()) {
  208. System.out.print(stacks[i].pop() + " ");
  209. }
  210. System.out.println();
  211. }
  212. System.out.println("total rejected is " + ((StackHanoi)stacks[3]).reportRejected());
  213. }
  214. }
Add Comment
Please, Sign In to add comment