Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

ChainQueue.java 3.8 KiB

5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /************************************************
  2. * Name: David Wurm
  3. * Datum: 25.11.2019
  4. * Klasse: 3AHIF
  5. * Programm: HUE_06_GenericsQueue
  6. ************************************************/
  7. import java.lang.reflect.Type;
  8. import java.util.Collection;
  9. import java.util.Iterator;
  10. import java.util.Queue;
  11. public class ChainQueue<E> implements Queue<E> {
  12. private class Node<E> {
  13. private E data;
  14. private Node<E> next;
  15. public Node<E> getNext() {
  16. return next;
  17. }
  18. public E getData() {
  19. return data;
  20. }
  21. public void setData(E data) {
  22. this.data = data;
  23. }
  24. public void setNext(Node<E> next) {
  25. this.next = next;
  26. }
  27. public Node(E data) {
  28. this.data = data;
  29. }
  30. }
  31. private Node<E> first; // first item of the queue
  32. public Node<E> getFirst() {
  33. return first;
  34. }
  35. public void setFirst(Node<E> first) {
  36. this.first = first;
  37. }
  38. @Override
  39. public boolean add(E input) {
  40. if (getFirst() == null) { // is the first empty?
  41. setFirst(new Node(input)); // yes? -> insert the node here
  42. return true;
  43. }
  44. return addNode(input, getFirst()); // no? -> goto addNode()
  45. }
  46. private boolean addNode(E input, Node n) { // run through the queue and find the first "place" which is empty
  47. if (n.getNext() == null) {
  48. n.setNext(new Node(input));
  49. return true;
  50. }
  51. return addNode(input, n.getNext());
  52. }
  53. @Override
  54. public int size() { // run through the queue and count the size
  55. Node temp = getFirst();
  56. int size = 0;
  57. while (temp != null) {
  58. size++;
  59. temp = temp.getNext();
  60. }
  61. return size;
  62. }
  63. @Override
  64. public boolean isEmpty() { // is the queue empty? -> size == 0
  65. if (first == null) {
  66. return true;
  67. }
  68. return false;
  69. }
  70. @Override
  71. public boolean contains(Object o) { // does object o exist in our queue?
  72. Node temp = getFirst();
  73. for (int i = 0; i < this.size(); i++) {
  74. if (getFirst().getData().equals(o)) {
  75. return true;
  76. }
  77. temp = temp.getNext();
  78. }
  79. return false;
  80. }
  81. @Override
  82. public E poll() { // poll next from queue and remove the first
  83. E temp = null;
  84. if (!this.isEmpty()) {
  85. temp = getFirst().getData();
  86. setFirst(getFirst().getNext());
  87. }
  88. return temp;
  89. }
  90. @Override
  91. public E peek() { // only "see" into the queue and do not remove anything
  92. if (!this.isEmpty()) {
  93. return getFirst().getData();
  94. }
  95. return null;
  96. }
  97. /*** the following is not implemented because it's not needed here ***/
  98. @Override
  99. public E element() {
  100. return null;
  101. }
  102. @Override
  103. public Iterator<E> iterator() {
  104. return null;
  105. }
  106. @Override
  107. public Object[] toArray() {
  108. return new Object[0];
  109. }
  110. @Override
  111. public <T> T[] toArray(T[] ts) {
  112. return null;
  113. }
  114. @Override
  115. public boolean remove(Object o) {
  116. return false;
  117. }
  118. @Override
  119. public boolean containsAll(Collection<?> collection) {
  120. return false;
  121. }
  122. @Override
  123. public boolean addAll(Collection<? extends E> collection) {
  124. return false;
  125. }
  126. @Override
  127. public boolean removeAll(Collection<?> collection) {
  128. return false;
  129. }
  130. @Override
  131. public boolean retainAll(Collection<?> collection) {
  132. return false;
  133. }
  134. @Override
  135. public void clear() {
  136. }
  137. @Override
  138. public boolean offer(E e) {
  139. return false;
  140. }
  141. @Override
  142. public E remove() {
  143. return null;
  144. }
  145. public Queue<Type> merge(Queue q1, Queue q2) {
  146. return null;
  147. }
  148. }