|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /************************************************
- * Name: David Wurm
- * Datum: 25.11.2019
- * Klasse: 3AHIF
- * Programm: HUE_06_GenericsQueue
- ************************************************/
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Queue;
-
- public class ChainQueue<E> implements Queue<E> {
- private class Node<E> {
- private E data;
- private Node<E> next;
-
- public Node<E> getNext() {
- return next;
- }
-
- public E getData() {
- return data;
- }
-
- public void setData(E data) {
- this.data = data;
- }
-
- public void setNext(Node<E> next) {
- this.next = next;
- }
-
- public Node(E data) {
- this.data = data;
-
- }
- }
-
- private Node<E> first; // first item of the queue
-
- public Node<E> getFirst() {
- return first;
- }
-
- public void setFirst(Node<E> first) {
- this.first = first;
- }
-
- @Override
- public boolean add(E input) {
- if (getFirst() == null) { // is the first empty?
- setFirst(new Node(input)); // yes? -> insert the node here
- return true;
- }
- return addNode(input, getFirst()); // no? -> goto addNode()
- }
-
- private boolean addNode(E input, Node n) { // run through the queue and find the first "place" which is empty
- if (n.getNext() == null) {
- n.setNext(new Node(input));
- return true;
- }
- return addNode(input, n.getNext());
- }
-
- @Override
- public int size() { // run through the queue and count the size
- Node temp = getFirst();
- int size = 0;
- while (temp != null) {
- size++;
- temp = temp.getNext();
- }
- return size;
- }
-
-
- @Override
- public boolean isEmpty() { // is the queue empty? -> size == 0
- if (this.size() == 0) {
- return true;
- }
- return false;
- }
-
- @Override
- public boolean contains(Object o) { // does object o exist in our queue?
- Node temp = getFirst();
- for (int i = 0; i < this.size(); i++) {
- if (getFirst().getData().equals(o)) {
- return true;
- }
- temp = temp.getNext();
- }
- return false;
- }
-
- @Override
- public E poll() { // poll next from queue and remove the first
- E temp = null;
- if (!this.isEmpty()) {
- temp = getFirst().getData();
- setFirst(getFirst().getNext());
- }
- return temp;
- }
-
- @Override
- public E element() {
- return null;
- }
-
- @Override
- public E peek() { // only "see" into the queue and do not remove anything
- if (!this.isEmpty()) {
- return getFirst().getData();
- }
- return null;
- }
-
- /*** the following is not implemented because it's not needed here ***/
-
- @Override
- public Iterator<E> iterator() {
- return null;
- }
-
- @Override
- public Object[] toArray() {
- return new Object[0];
- }
-
- @Override
- public <T> T[] toArray(T[] ts) {
- return null;
- }
-
- @Override
- public boolean remove(Object o) {
- return false;
- }
-
- @Override
- public boolean containsAll(Collection<?> collection) {
- return false;
- }
-
- @Override
- public boolean addAll(Collection<? extends E> collection) {
- return false;
- }
-
- @Override
- public boolean removeAll(Collection<?> collection) {
- return false;
- }
-
- @Override
- public boolean retainAll(Collection<?> collection) {
- return false;
- }
-
- @Override
- public void clear() {
-
- }
-
- @Override
- public boolean offer(E e) {
- return false;
- }
-
- @Override
- public E remove() {
- return null;
- }
- }
|