所以我试图制作一个字符的链表,首先我要指出的是,我们已经不允许在 java 中使用内置的链表方法。当我添加字符然后尝试显示它们时,它只会给我一个数字列表。为什么会这样?
class Node{
int data;
Node next;
public Node(Character x){
data = x; next = null;
}
public Node next(){return next;}
public void setNext(Node p){
next = p;
}
public void set(Character x){data = x;}
public int data(){return data;}
}
class Reader{
Node head = null;//empty list
public void add(Character x){ //add at head
Node nw = new Node(x);
nw.setNext(head);
head = nw;
}
public void display(){
Node k = head;
System.out.print('[');
while(k!=null){
if(k.next!=null)
System.out.print(k.data()+",");
else
System.out.print(k.data());
k=k.next();
}
System.out.print(']');
}
}
class assignment9{
public static void main(String[]args){
Reader r1 = new Reader();
r1.add('r');
r1.add('e');
r1.add('l');
r1.add('l');
r1.add('o');
r1.display();
}
}