LinkedQueue下一个值确实不起作用

时间:2016-10-16 20:17:46

标签: java queue

我想在FIFO规则之后创建一个Queue,每个元素都是在队列末尾创建的,但是我的元素的getNext()不起作用(Value为null),我不知道。

这是UML:

enter image description here

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 result;
    }

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

    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;
    }

}

0 个答案:

没有答案