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.3 KiB

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