Java实现简单的单链表
class Link
{ // 链表类定义
class Node
{ // 内部类:定义链表的一个节点
private String name; // 节点数据域
private Node next; // 节点引用域
public Node(String name)
{ // 构造器
this.name = name;
this.next = null;
System.out.println(“内部类调用:一个新的空节点类对象初始化完毕,当前节点next引用域被显式初始化为null!”);
}
public String getName()
{ // 取得节点数据
return this.name;
}
public void addNode(Node newNode)
{ // 添加节点
if (this.next == null)
{ // 后面没有节点,直接添加新节点
this.next = newNode;
}
else
{ // 如果后面还有节点,往下一节点后添加
this.next.addNode(newNode); // 递归调用
}
}
public boolean searchNode(String name) // 从当前节点检查是否有匹配指定数据的节点
{
if (this.name.equals(name))
{ // 当前节点含有符合的目标数据,返回true
return true;
}
else
{ //当前节点不是目标,往下一节点找,并把查找状态返回
if (this.next != null) // 确保操作有意义
{
return this.next.searchNode(name);
}
else // 搜索完毕,仍为找到目标,返回false
{
return false;
}
}
}
public void deleteNode(Node preNode, String name)
{
if (this.name.equals(name))
{
preNode.next = this.next;
}
else
{
this.next.deleteNode(this, name);
}
}
public void printNode() // 打印节点
{
System.out.println(this.name + “–>”); // 打印当前节点数据域name
if (this.next != null) // 当前节点后面还有节点
{
this.next.printNode(); // 继续打印下一个节点
}
else
{System.out.println(“*****************************************************打印完毕”);}
}
}
// 定义根节点,链表必须有root节点,标识她的存在
private Node root;
public Link() // 链表构造器
{
this.root = null;
System.out.println(“外部类调用完毕:一个新的空链表类对象初始化完毕,当前链表的根节点root被显式初始化为null!”);
}
public void add(String name) // 向链表中添加数据到新节点
{
System.out.println(“调用内部类开始,将实例化一个节点Node!”);
Node newNode = new Node(name); // 定义这个新节点,加入新的数据域
if (this.root == null)
{
this.root = newNode; // 如无root节点,直接添加新节点为root
}
else
{
this.root.addNode(newNode); // 有root节点,往下一节点后添加;
}
}
public boolean search(String name) // 对当前链表执行指定name数据域的搜索检查
{
if (this.root != null)
{
return this.root.searchNode(name); // 从当前链表根节点搜索所有节点,并返回查找状态
}
else // 空链表,返回false
{
return false;
}
}
public void delete(String name)
{
if (this.search(name)) // 如果存在要删除的节点,含有指定的数据
{
if (this.root.name.equals(name)) // 如果根节点就是匹配的待删除节点
{
if (this.root.next == null) {System.out.println(“当前链表只有根节点root了,this.root.next == null!”);}
this.root = this.root.next; // 将root的next节点设为root,此时root可以为null
// if (this.root.next != null){this.root = this.root.next;}
// else { this.root = null;} // this.root.next = null!
System.out.println(“删除操作完毕!删掉的是根节点root!”);
}
else
{
this.root.next.deleteNode(this.root, name);
System.out.println(“节点删除完毕,删掉的不是根节点!”);
}
}
else
{
System.out.println(“当前链表,没有您要删除的数据!”);
}
}
public void print() // 打印链表
{
if (this.root != null) // 链表存在
{
System.out.println(“当前链表打印结果:”);
this.root.printNode(); // 从root根节点打印该链表的所有节点
}
else
{
System.out.println(“链表不存在!或者根节点为空!打印请求终止!”);
}
}
}
public class LinkMain1
{
public static void main(String[] args)
{
Link link = new Link(); // 实例化一个链表
link.add(“root”); // 为新链表对象自定义根节点name数据域为root
link.add(“the first node”);
link.add(“the second node”);
link.add(“the third node”);
if (link.search(“the first node”))
{
System.out.println(“您的查询在当前链表中找到了!”);
link.print();
}
else
{
System.out.println(“您查的数据不在当前链表中!”);
link.print();
}
link.delete(“the first node”);
link.print();
link.delete(“the second node”);
link.print();
link.delete(“the third node”);
link.print();
link.delete(“root”);
link.print(); // 打印该链表
}
}
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《Java实现简单的单链表》
本文地址:http://www.xiupu.net/archives-57.html
关注公众号:
微信赞赏
支付宝赞赏