0
我想知道如何实现一个测试程序来彻底测试这些方法,并通过在测试程序中使用两个Csc2001LinkedList对象来完成此操作,一个作为有序列表维护,另一个作为无序列表。我知道的一个方面是使用打印方法来检查方法是否正常工作。如何实现测试程序(用Java)来测试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;
}
}
}
类节点
public class Node {
protected char ch;
protected Node next;
public Node(char ch, Node next)
{
this.ch = ch;
this.next = null;
}
}