Следующее значение LinkedQueue работает не так, как на самом деле

#java #queue

#java #очередь

Вопрос:

Я хочу создать очередь после правила FIFO, каждый элемент создается в конце очереди, но мой GetNext() для элемента не работает (значение равно null), и я понятия не имею.

Вот UML:

введите описание изображения здесь

 import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyLinkedQueue<E> implements Iterable<E>, MyQueue<E> {

    private Cell front;
    private Cell rear;
    private int count;

    public Cell getFront() {
        return front;
    }

    public void setFront(Cell front) {
        this.front = front;
    }

    public Cell getRear() {
        return rear;
    }

    public void setRear(Cell rear) {
        this.rear = rear;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public class Cell {
        private E value;
        private Cell next;

        Cell(E element) {
            this.value = element;

        }

        public E getValue() {
            return value;
        }

        public void setValue(E value) {
            this.value = value;
        }

        public Cell getNext() {
            return next;
        }

        public void setNext(Cell next) {
            this.next = next;
        }
    }

    public class MyIterator implements Iterator<E> {
        private Cell pointer;
        private boolean begin;
        private boolean removable;

        public Cell getPointer() {
            return pointer;
        }

        public void setPointer(Cell pointer) {
            this.pointer = pointer;
        }

        public boolean isBegin() {
            return begin;
        }

        public void setBegin(boolean begin) {
            this.begin = begin;
        }

        public boolean isRemovable() {
            return removable;
        }

        public void setRemovable(boolean removable) {
            this.removable = removable;
        }

        public MyIterator() {

        }

        public boolean hasNext() {
            return pointer != null ? true : false;
        }

        public E next() {
            if (!hasNext())
                throw new NoSuchElementException();
            E e = pointer.value;
            pointer = pointer.getNext();
            return e;
        }

        public void remove() {
            throw new NoSuchElementException();

        }
    }

    public boolean isEmpty() {
        return count == 0;

    }

    public E peek() throws NoSuchElementException {
        E result = null;
        if (isEmpty()) {
            System.out.println("Queue is empty");
        } else {
            result = front.getValue();
        }
        return resu<
    }

    public E peekLast() throws NoSuchElementException {
        E result = null;
        if (isEmpty()) {
            System.out.println("Queue is empty");
        } else {
            result = rear.getValue();
        }
        return resu<
    }

    public MyQueue<E> append(E element) {
        if (front == null)
            front = new Cell(element);
        rear = new Cell(element);
        // rear=rear.getNext(); getNext() PROBLEM
        count  ;
        return this;

    }

    public E delete() throws NoSuchElementException {
        E e;
        e = front.getValue();
        // front = front.getNext();getNext() PROBLEM
        count--;
        if (count == 0)
            rear = null;
        return e;

    }


    public int size() {
        return count;
    }


    public String toString() {
        StringBuilder s = new StringBuilder();
        for (E e : this)
            s.append(e   "; ");
        return s.toString();

    }

    @Override
    public Iterator<E> iterator() {
        Iterator<E> iterator = new MyIterator();
        return iterator;
    }

}
 

Комментарии:

1. Вы не устанавливаете rear следующее Cell значение перед заменой того, что rear есть, при добавлении. Как первая ячейка узнает о своем соседе и так далее по очереди элементов?