链接列表测试用例中的Java优先级队列

时间:2017-08-21 08:26:13

标签: java testing linked-list nodes priority-queue

所以我正在尝试使用链表实现优先级队列。我想我已经掌握了基础知识,但由于某些原因,我的测试用例不起作用。当我运行它时,大小显示正常,但没有显示任何节点值(只弹出一个箭头“ - >”)。如果有人可以帮我弄清楚它为什么不起作用,或建议一个更好的方法来设置java中的测试用例(我以前从未这样做过),我们将不胜感激!

节点类:

public class Node {     //Node class structure

    int data;   //data contained in Node; for assignment purposes, data is an int
    Node next;  //pointer to Next Node

    //Node Constructor
    public Node(int data) {    
        this.data = data;
        next = null;
    }

    //Set Methods
    public void setData(int data) {         //set Node value
        this.data = data;
    }
    public void setNext(Node next) {        //set next Node value
        this.next = next;
    }   

    //Get Methods
    public int getData() {                  //get Node value
        return this.data;
    }   
    public Node getNext() {                 //get next Node value
        return this.next;
    }

    //Display the Node Value
    public void displayNode() {
        System.out.println(data + "urgh");  //display value as a string
    }
}

链接列表类:

import Question1.Node;

//basic set-up of a FIFO singly linked list
public class SLList{

    protected Node head;    //head of SLList
    protected Node tail;    //tail of SLList
    int n;      //number of elements in SLList

    //SLList constructor
    public SLList() {
        head = null;
        n = 0;
    }   

    //check if list is empty
    public boolean isEmpty() {
        return head == null;
    }

    //return the size of the list
    public int size() {
        return n;           
    }

    //add a new node to the end of the list
    public boolean insert(int x){
        Node y = new Node(x);

        if (head == null){  //if head is null, thus an empty list
            head = y;       //assign head as y
        }
        else{               //if there is already a tail node
            tail.next = y;  //assign the tail's pointer to the new node
        }
        tail = y;           //assign tail to y
        this.n++;           //increment the queue's size
        return true;        //show action has taken place
    }

    //remove and return node from head of list
    public Node remove(){
        if (n == 0){            //if the list is of size 0, and thus empty
            return null;        //do nothing
        }
        else{                           //if there are node(s) in the list
            Node pointer = head;        //assign pointer to the head
            head = head.next;           //reassign head as next node,               
            n--;                        //decrement list size
            return pointer;             //return the pointer
        }       
    }

    //display SLList as string
    public void displayList() {
        Node pointer = head;

        while (pointer != null) {
            pointer.displayNode();
            pointer = pointer.next;
        }
        System.out.println(" ");
    }
}

优先级队列类:

import Question1.Node;
import Question1.SLList;

public class PriorityQueue extends SLList {

    private SLList list;        //SLList variable

    public PriorityQueue(){     //create the official SLList
        list = new SLList();
    }

    //add a new node; new add method that ensures the first element is sorted to be the "priority"
    public boolean add(int x){
        Node y = new Node(x);

        if (n == 0){        //if there are 0 elements, thus an empty list
            head = y;       //assign head as y
        }
        else if (y.data < head.data){   //if new node y is the smallest element, thus highest priority
            y.next = head;              //assign y's next to be current head of queue
            head = y;                   //reassign head to be actual new head of queue (y)
        }
        else{               //if there is already a tail node
            tail.next = y;  //assign the tail's pointer to the new node
        }
        tail = y;           //assign tail to y
        n++;                //increment the queue's size
        return true;        //show action has taken place
    }

    //delete the minimim value (highest priority value) from the queue and return its value
    public Node deleteMin(){
        return list.remove();       //the list is sorted such that the element being removed in indeed the min
    }

    //return the size of the queue
    public int size() {
        return n;           
    }

    //display Queue as string
    public void displayQueue() {
        System.out.println("->");
        list.displayList();
    }
}

测试用例(到目前为止,删除一个没有工作,所以它被注释掉了):

import Question1.PriorityQueue;

public class TestQ1 {           //Test code

    public static void main(String[] args){
        PriorityQueue PQueue1 = new PriorityQueue();

        PQueue1.add(3);
        PQueue1.add(2);
        PQueue1.add(8);
        PQueue1.add(4);

        System.out.println("Test add(x): ");
        PQueue1.displayQueue();
        System.out.println("Test size(): " + PQueue1.size());

        PriorityQueue PQueue2 = new PriorityQueue();
        //Node node1 = PQueue1.deleteMin();

        System.out.println("Test deleteMin():");
        PQueue2.displayQueue();
        System.out.println("Test size(): " + PQueue2.size());
    }   
}

1 个答案:

答案 0 :(得分:0)

list.displayList()更改为displayList(),您将看到预期的输出。

为什么呢?因为您的队列已经是列表(即SLList的实例)。当课程A 扩展另一个课程B时,A的实例也是B的实例。这是继承

您还在private SLList list实现中包含了一个实例变量PriorityQueue,这是 composition 的一个示例。通常,您只会根据自己的情况选择这两个选项中的一个或另一个。在这种情况下,您似乎正在尝试使用继承,因此没有理由创建单独的list实例变量。您正在将数据直接添加到队列中(使用本质上它本身就是一个列表的事实)。

您应该删除list实例变量,并且它的所有用法都应该引用父类的方法或变量。