队列复制构造函数

时间:2017-04-07 02:46:10

标签: java linked-list queue copy-constructor

我不确定我的队列(DSQueue)的复制构造函数是否有问题,或者我的问题还有其他原因。复制构造函数似乎可以复制内容,但如果我尝试操作复制的队列,程序将失败。我已经在我的队列主要方法(见下文)中完成了一些测试,并且我已经通过调试器。当我尝试在main方法中更改行testQ.list.add(new Token(1));中的队列时,IDE指出NullPointerException。此行可以使令牌正常,但它不会启动add方法。不知道为什么......

public class DSQueue extends Queue {

    public static void main(String[] args) {
        DSQueue testQ = new DSQueue();

        System.out.println("Size of testQueue: " + testQ.theQueue.size());
        testQ.offer(new Token(100));
        testQ.offer(new Token(200));
        System.out.println("Size of testQueue after adding: " + testQ.theQueue.size());

        System.out.println("\ntestQueue: " + testQ);

        // TETING COPY CONSTRUCTOR
        DSQueue newQ = new DSQueue(testQ);
        System.out.println("New queue (should be the same as testQueue): " + newQ + " - size: " + newQ.size());

        System.out.println("\nAfter changing one queue...");
        testQ.list.add(new Token(1));

        System.out.println("testQ: " + testQ + " - size: " + testQ.size());
        System.out.println("New queue: " + newQ + " - size: " + newQ.size());
    }

    // The list containing the data
    private DSList theQueue;

    public DSQueue() {
    theQueue = new DSList();
    }

    public DSQueue(Queue s) {
        this();

        // Cast s to a DSQueue type
        DSQueue sCasted = (DSQueue)s;

        // Create a reference for the Queue to be copied
        Node queueToCopy = sCasted.theQueue.head;

        while(queueToCopy != null) {
            this.offer(queueToCopy.getToken());
            queueToCopy = queueToCopy.next;
        }
    }
}

public abstract class Queue {

    public DSList list;

    public abstract boolean offer(Token t);


    public abstract Token poll();

    public abstract Token peek();

    @Override
    public abstract String toString();

    public abstract int size();

    public abstract boolean isEmpty();
}

public class DSList implements List {

    public Node head;

    public DSList() {
        head = null;
    }

    public DSList(Node head_) {
        head = head_;
    }

    // Copy constructor.
    public DSList(DSList other) {       

        // Create a reference for the list to be copied
        Node oldListNode = other.head;  

        while (oldListNode != null) {
            this.add(oldListNode.getToken());
            oldListNode = oldListNode.next;
        }       
    }

    /**
     * Appends the specified element to the end of this list.
     * @param obj The object to add. 
     * @return True if the object has been added to the list. 
     * 
     * @throws NullPointerException if the specified object is null.
     */
    public boolean add(Token obj) {
        if (obj == null) {
            throw new NullPointerException();
        }
        // If list is empty, add new node to front.
        if(isEmpty()) {  
            // Create a new Node. Add Token obj as data
            Node newNode = new Node(null, null, obj);  
            // point head to new Node, newNode
            head = newNode;
            return true;
        } else { 
            // create a reference of the start position in the list
            Node current = head;
            // While there are nodes remaining in the list, advance through to reach the end.
            while (current.next != null) 
                current = current.next;
            // Create a new node and append it to the end of the list, with 'prev' pointing to current (2nd to last node)
            Node newNode = new Node(null, current, obj);
            // Point old last element to the newest, last node in the list (in next variable)
            current.next = newNode;
            // Return true if successful addition of node.
            return true;
        }
    }
}

public class Token {

    public enum Type { OPERATOR, OPERAND, PAREN };
    public Type type;

    private String operator;
    private double operand;

    public Token(double result) {
        this.operand = result;
        this.type = Type.OPERAND;
    }
}

public class Node {

    public Node next;
    public Node prev;

    // data being stored in each node
    private Token t;

    // Node constructor
    public Node(Node next, Node prev, Token token) {
        this.next = next;
        this.prev = prev;
        this.t = token;
    }

    public Token getToken() {
        return t;
    }
}

0 个答案:

没有答案