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



所以我正在尝试实现带有链表的优先级队列。我认为我已经掌握了基础知识,但由于某种原因,我的测试用例不起作用。当我运行它时,大小显示正常,但没有显示任何节点值(只弹出一次箭头"->"(。如果有人能帮助我弄清楚它为什么不起作用,或者建议一种更好的方法来在 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());
}   
}

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


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

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

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

最新更新