用了外部类Link来管理,用内部类Node来实现一些功能
实现的功能:
1、在链表末尾添加数据节点——add()
2、获得链表长度——size()
3、判断链表是否为空——isEmpty()
4、判断某一数据是否存在——contains(数据)
5、根据索引获得指定位置处的数据——get(index)
6、修改指定索引处的数据,(替换)——repalce(数据,index)
7、从链表里删除数据(移除节点)——remove(数据)
8、清空链表——clear()
class Link //链表
{
class Node //节点
{
private String msg;
private Node next;
//构造
public Node(String msg) {
this.msg = msg;
}
//setter、getter
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
//添加节点
public void addNode(Node newNode)
{
if(this.next==null)
{
this.next=newNode;
}
else
{
this.next.addNode(newNode); //递归实现,以下内部类中的很多方法都是用递归实现的
}
}
//判断节点是否存在
public boolean containsNode(String temp)
{
if(temp.equals(this.msg))
{
return true;
}
else
{
if(this.next==null)
return false;
else
return this.next.containsNode(temp);
}