25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChainQueue.java 3.7 KiB

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