You can not select more than 25 topics 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 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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) { // Übergeben der "Data"
  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 Iterator<E> iterator() {
  82. return null;
  83. }
  84. @Override
  85. public Object[] toArray() {
  86. return new Object[0];
  87. }
  88. @Override
  89. public <T> T[] toArray(T[] ts) {
  90. return null;
  91. }
  92. @Override
  93. public boolean remove(Object o) {
  94. return false;
  95. }
  96. @Override
  97. public boolean containsAll(Collection<?> collection) {
  98. return false;
  99. }
  100. @Override
  101. public boolean addAll(Collection<? extends E> collection) {
  102. return false;
  103. }
  104. @Override
  105. public boolean removeAll(Collection<?> collection) {
  106. return false;
  107. }
  108. @Override
  109. public boolean retainAll(Collection<?> collection) {
  110. return false;
  111. }
  112. @Override
  113. public void clear() {
  114. }
  115. @Override
  116. public boolean offer(E e) {
  117. return false;
  118. }
  119. @Override
  120. public E remove() {
  121. return null;
  122. }
  123. @Override
  124. public E poll() { // poll next from queue and remove the first
  125. E temp = null;
  126. if (!this.isEmpty()) {
  127. temp = getFirst().getData();
  128. setFirst(getFirst().getNext());
  129. }
  130. return temp;
  131. }
  132. @Override
  133. public E element() {
  134. return null;
  135. }
  136. @Override
  137. public E peek() { // only "see" into the queue and do not remove anything
  138. if (!this.isEmpty()) {
  139. return getFirst().getData();
  140. }
  141. return null;
  142. }
  143. }