[英]How to implement a test program (in Java) to test Csc2001LinkedList methods?
I want to know how to implement a test program to test these methods thoroughly, and to do this by using two Csc2001LinkedList objects in the test program, one that is maintained as an ordered list, the other as an unordered list. 我想知道如何实现一个测试程序来彻底测试这些方法,并通过在测试程序中使用两个Csc2001LinkedList对象来做到这一点,一个对象保持为有序列表,另一个保持为无序列表。 One aspect of this that I'm aware of is to use the print method to check whether or not the methods are working properly. 我知道的这一方面是使用print方法来检查这些方法是否正常工作。
Class Csc2001LinkedList 类Csc2001LinkedList
public class Csc2001LinkedList {
protected Node head;
public Csc2001LinkedList(Node head)
{
this.head = null;
}
public Node getHead()
{
return head;
}
public void addFirst(char c)
{
head = new Node(c, head);
}
public void addAtEnd(char c)
{
Node n = new Node(c, head);
Node temp = head;
while(temp.next != null)
{
temp = temp.next;
}
temp.next = n;
}
public void addInOrder(char c)
{
Node n = new Node(c, head);
if(isEmpty())
{
addFirst(c);
}
else
{
Node pre = head;
Node succ = head.next;
if(n.ch < pre.ch)
{
n.next = head;
head = n;
}
else
{
while(succ != null && n.ch > succ.ch)
{
pre = succ;
succ = pre.next;
}
n.next = succ;
pre.next = n;
}
}
}
public boolean isEmpty()
{
return (head == null);
}
public char removeFirst()
{
Node temp = head.next;
if(temp != null)
{
head.next = temp.next;
return temp.ch;
}
else
{
System.out.println("List is empty");
return '?';
}
}
public void printList()
{
Node temp = head;
while(temp != null)
{
System.out.println(temp.ch + " ");
temp = temp.next;
}
}
}
class Node 类节点
public class Node {
protected char ch;
protected Node next;
public Node(char ch, Node next)
{
this.ch = ch;
this.next = null;
}
}
My suggestion would be to add a third class to your program. 我的建议是在您的程序中添加第三类。 This class would be a driver class, and will be used to test the list and node methods thoroughly. 此类将是驱动程序类,并将用于彻底测试列表和节点方法。 You can do this in one of two ways: 您可以通过以下两种方式之一执行此操作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.