如何修复队列实现? (JAVA)

时间:2015-10-23 14:45:26

标签: java data-structures queue implementation doubly-linked-list

Java中的My Queue实现不起作用,我不确定原因。我认为这是dequeue()类中的MyQueue方法。我该如何解决这个问题,以便队列正常工作?我已经包含了我的MyQueue类以及我用来构建队列的MyDoublyLinkedList类和MyDoubleNode类。

这是队列的实现:

public class MyQueue<AnyType> implements Queue<AnyType> {

    MyDoublyLinkedList queuey = new KWLab03();

    @Override
    public boolean isEmpty() {

        return queuey.isEmpty() ;
    }

    @Override
    public void enqueue(AnyType x) {

        queuey.insert(x); //inserts the data into the end of the queue
    }

    @Override
    public AnyType dequeue() {

        if(isEmpty())
        {
            System.out.println("Queue is empty."); 
            return null; 
        }
        else
        {
            MyDoubleNode tmp = queuey.last; 
            MyDoubleNode cur = queuey.last.next; 

            queuey.last = cur; 

            return (AnyType) tmp.data; 
        }
    }

    @Override
    public AnyType peek() {

        return (AnyType) queuey.last.data;
    }
}

以下是双向链表的实现:

public class MyDoublyLinkedList实现DoublyLinkedList {

MyDoubleNode first, last;
int size;

public void insert(AnyType a) 
{  
    if (lookup(a) == false)
    {           
        if(first == null)
        {
            last = new MyDoubleNode();
            last.data = a;
            first = last;
        }
        else{
            last.next = new MyDoubleNode();
            last.next.data = a;
            last = last.next;
        }
    }
    else 
    {
        System.out.println("Duplicates not allowed: Item already on list.");
    }
    size++;
}

public void delete(AnyType a)
{
    if (lookup(a) == true) 
    {
        MyDoubleNode cur = first;
        MyDoubleNode previous = new MyDoubleNode();

        while (cur.data != a) 
        {
            previous = cur;
            cur = cur.next;
        }
        previous.next = cur.next;

    } 
    else 
    {
        System.out.println("Requested item not found.");
    }
    size--;
}

public boolean lookup(AnyType a) 
{
    MyDoubleNode cur = first;

    while (cur != null) 
    {
        if (cur.data.equals(a)) 
        {
            return true;
        }
        cur = cur.next;
    }
    return false;
}

public int sizeChecker()
{
    return size;
}

public boolean isEmpty()
{
    return first == null;
}

public void printList() 
{ 
    MyDoubleNode cur = first;

    while (cur != null) 
    {
        System.out.println(cur.data.toString());
        cur = cur.next;
    }
}

public MyDoublyLinkedList () {
    //Linked List Constructor
}

}

这是我的双重链接列表的Node类:

public class MyDoubleNode<AnyType> {

    public AnyType data;
    public MyDoubleNode<AnyType> next;
    public MyDoubleNode<AnyType> prev;
}

这是测试代码:

public class TestQueue {

    public static void main(String[] args)
    {
        MyQueue queue1 = new MyQueue();

        int a = 1, b = 2, c = 3;

        queue1.enqueue(a); //enqueues a
        System.out.println("The first item in the queue is " + queue1.peek()); 

        queue1.enqueue(b); //enqueues b
        System.out.println("The first item in the queue is " + queue1.peek()); 

        queue1.enqueue(c); //enqueues c
        System.out.println("The first item in the queue is " + queue1.peek()); 

        System.out.println(queue1.dequeue());
        System.out.println(queue1.dequeue());
        System.out.println(queue1.dequeue());

    }

}

非常感谢任何帮助。

0 个答案:

没有答案