Java codeimport java.io.*;
public class Link {
public static void main(String args[]) {
String s = "";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
s = br.readLine();
} catch (IOException e) {
}
LinklistCheck(s);
}
public static void LinklistCheck(String s) {
int i = 1;
System.out.println("开始建立链表");
LinkList a = new LinkList();
for (i = 0; i < s.length(); i++)
a.insert(s.charAt(i));
// i++;
System.out.println("检验链表是否建立成功");
System.out.println(a.Display());
}
}
class Node {
private char data;
private Node next;
Node() {
data = ' ';
next = null;
}
Node(char data) {
this.data = data;
next = null;
}
Node(char data, Node next){
this.data = data;
this.next = next;
}
public char getData() {
return data;
}
public void setNext(Node next) {
this.next = next;
}
Node getNext() {
return next;
}
}
class LinkList{
Node head;
Node tail;
// Boolean i;
LinkList() {
head = null;
tail = null;
// i=false;
}
LinkList(char data){
head = new Node(data);
tail = head;
// i=false;
}
public String Display(){
Node next = head;
String s = "";
while (next != null){
s += next.getData() + " ";
next = next.getNext();
}
return s;
}
public void insert(char data) // 将插入节点放在上次插入节点之后
{
if (tail == null){
head = new Node(data);
tail = head;
}else {
Node node = new Node(data);
tail.setNext(node);
tail = node;
}
}
}