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.

167 lines
3.5 KiB

  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. Node<E> first; // first item of the queue
  31. @Override
  32. public boolean add(E input) { // Übergeben der "Data"
  33. if (first == null) { // is the first empty?
  34. first = new Node(input); // yes? -> insert the node here
  35. return true;
  36. }
  37. return addNode(input, first); // no? -> goto addNode()
  38. }
  39. private boolean addNode(E input, Node n) { // run through the queue and find the first "place" which is empty
  40. if (n.getNext() == null) {
  41. n.setNext(new Node(input));
  42. return true;
  43. }
  44. return addNode(input, n.getNext());
  45. }
  46. @Override
  47. public int size() { // run through the queue and count the size
  48. Node temp = first;
  49. int size = 0;
  50. while (temp != null) {
  51. size++;
  52. temp = temp.getNext();
  53. }
  54. return size;
  55. }
  56. @Override
  57. public boolean isEmpty() { // is the queue empty? -> size == 0
  58. if (this.size() == 0) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. @Override
  64. public boolean contains(Object o) { // does object o exist in our queue?
  65. Node temp = first;
  66. for (int i = 0; i < this.size(); i++) {
  67. if (first.getData().equals(o)) {
  68. return true;
  69. }
  70. temp = temp.getNext();
  71. }
  72. return false;
  73. }
  74. @Override
  75. public Iterator<E> iterator() {
  76. return null;
  77. }
  78. @Override
  79. public Object[] toArray() {
  80. return new Object[0];
  81. }
  82. @Override
  83. public <T> T[] toArray(T[] ts) {
  84. return null;
  85. }
  86. @Override
  87. public boolean remove(Object o) {
  88. return false;
  89. }
  90. @Override
  91. public boolean containsAll(Collection<?> collection) {
  92. return false;
  93. }
  94. @Override
  95. public boolean addAll(Collection<? extends E> collection) {
  96. return false;
  97. }
  98. @Override
  99. public boolean removeAll(Collection<?> collection) {
  100. return false;
  101. }
  102. @Override
  103. public boolean retainAll(Collection<?> collection) {
  104. return false;
  105. }
  106. @Override
  107. public void clear() {
  108. }
  109. @Override
  110. public boolean offer(E e) {
  111. return false;
  112. }
  113. @Override
  114. public E remove() {
  115. return null;
  116. }
  117. @Override
  118. public E poll() { // poll next from queue and remove the first
  119. E temp = null;
  120. if (!this.isEmpty()) {
  121. temp = first.getData();
  122. first = first.getNext();
  123. }
  124. return temp;
  125. }
  126. @Override
  127. public E element() {
  128. return null;
  129. }
  130. @Override
  131. public E peek() { // only "see" into the queue and do not remove anything
  132. if (!this.isEmpty()) {
  133. return first.getData();
  134. }
  135. return null;
  136. }
  137. }