java中建立单链表,java 建立单链表解决方案

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值