Advertisement
Guest User

Untitled

a guest
May 30th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package com.company;
  2.  
  3. /**
  4. * Created by lonestar on 30.05.2016.
  5. */
  6. public class Queue {
  7.  
  8. int size;
  9. int head;
  10. int tail;
  11. private int nElem;
  12. double[] queue;
  13.  
  14. Queue(int size)
  15. {
  16. this.size = size;
  17. queue = new double[size];
  18. head = 0;
  19. tail = -1;
  20. nElem = 0;
  21. }
  22.  
  23. void Add(double value)
  24. {
  25. if(tail == size -1)
  26. tail = -1;
  27. queue[++tail] = value;
  28. nElem++;
  29. }
  30.  
  31. double Extract(){
  32. double tmp = queue[head++];
  33. if(head == size)
  34. head=0;
  35. nElem--;
  36. return tmp;
  37. }
  38. public boolean isEmpty() {
  39. return (nElem == 0);
  40. }
  41.  
  42. public boolean isFull() {
  43. return (nElem == size - 1);
  44. }
  45.  
  46. public double getHead()
  47. {
  48. return queue[head];
  49. }
  50.  
  51. public double getTail()
  52. {
  53. return queue[tail];
  54. }
  55.  
  56. public void ShowAdd()
  57. {
  58. System.out.print("Queue task:\n");
  59. Queue myQueue = new Queue(5);
  60.  
  61. myQueue.Add(10);
  62. myQueue.Add(20);
  63. myQueue.Add(30);
  64. myQueue.Add(40);
  65. myQueue.Add(50.5);
  66.  
  67. while (!myQueue.isEmpty()) {
  68. double N = myQueue.Extract();
  69. System.out.println("Elem: " + N);
  70.  
  71. }
  72.  
  73. }
  74.  
  75. public void ShowAfterDelete()
  76. {
  77. System.out.print("After Delete:\n");
  78. Queue myQueue = new Queue(5);
  79.  
  80. myQueue.Add(10);
  81. myQueue.Add(20);
  82. myQueue.Add(30);
  83. myQueue.Add(40);
  84. myQueue.Add(50.5);
  85.  
  86. myQueue.Extract();
  87. myQueue.Extract();
  88. myQueue.Extract();
  89.  
  90. myQueue.Add(60);
  91.  
  92. while (!myQueue.isEmpty()) {
  93. double N = myQueue.Extract();
  94. System.out.println("Elem: " + N);
  95.  
  96. }
  97. }
  98.  
  99. }
  100. package com.company;
  101.  
  102. /**
  103. * Created by lonestar on 30.05.2016.
  104. */
  105. public class Stack<T> {
  106. private Node<T> top;
  107. private int count;
  108.  
  109. public int size() {
  110. return count;
  111. }
  112.  
  113. public boolean isEmpty() {
  114. return top == null;
  115. }
  116.  
  117. public void push(T element) {
  118. top = new Node<T>(element, top);
  119. count++;
  120. }
  121.  
  122. public T pop() {
  123. if (top == null) {
  124. throw new RuntimeException("This stack doesn't contain items.");
  125. }
  126. T element = top.item;
  127. top = top.next;
  128. count--;
  129. return element;
  130. }
  131.  
  132. private class Node<T> {
  133. private Node next;
  134. private T item;
  135.  
  136. public Node(T element, Node<T> head) {
  137. item = element;
  138. next = head;
  139. }
  140. }
  141.  
  142. public void ShowPush()
  143. {
  144. System.out.print("\nStack after push:\n");
  145. Stack<String> stack = new Stack<>();
  146. stack.push(Integer.toString(123,2));
  147. stack.push(Integer.toString(122,2));
  148. stack.push(Integer.toString(121,2));
  149. stack.push(Integer.toString(113,2));
  150. stack.push(Integer.toString(123,2));
  151. stack.push(Integer.toString(101,2));
  152. stack.push(Integer.toString(111,2));
  153. stack.push(Integer.toString(88,2));
  154. stack.push(Integer.toString(2,2));
  155. stack.push(Integer.toString(13,2));
  156. int i=0;
  157. while (i<stack.size())
  158. {
  159. System.out.println(stack.pop());
  160. }
  161.  
  162. }
  163.  
  164. public void ShowAfterPop()
  165. {
  166. System.out.print("\nStack after pop:\n");
  167. Stack<String> stack = new Stack<>();
  168.  
  169. stack.push(Integer.toString(123,2));
  170. stack.push(Integer.toString(122,2));
  171. stack.push(Integer.toString(121,2));
  172. stack.push(Integer.toString(113,2));
  173. stack.push(Integer.toString(123,2));
  174. stack.push(Integer.toString(101,2));
  175. stack.push(Integer.toString(111,2));
  176. stack.push(Integer.toString(88,2));
  177. stack.push(Integer.toString(2,2));
  178. stack.push(Integer.toString(13,2));
  179.  
  180. stack.pop();
  181. stack.pop();
  182.  
  183. for(int i=stack.size();i>0;i--)
  184. {
  185. System.out.println(stack.pop());
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement