Java链表(单向)简单定义

class Link {
	private class Node {
		private Node next;// 保存下一个节点
		private String data;// 保存数据

		public Node(String data) {// 带参构造方法用于打包数据data
			this.data = data;
		}

		// 1.添加节点方法
		public void addNode(Node newNode) {
			if (this.next == null) {// 根节点的下一个节点是否为null值。
				this.next = newNode;// 满足条件,新节点为根节点的下一个节点
			} else {
				this.next.addNode(newNode);// 以上条件不满足。递归调用
			}
		}

		// 2.内容查询
		public boolean containsNode(String data) {
			if (data.equals(this.data)) {
				return true;
			} else {
				if (this.next != null) {
					return this.next.containsNode(data);
				} else {
					return false;
				}
			}

		}

		// 3.通过索引获取数据
		public String getNode(int index) {
			if (Link.this.foot++ == index) {// 为要查询的索引
				return this.data;// 返回当前节点数据
			} else {// 上面应该继续向后查询
				return this.next.getNode(index);
			}
		}

		// 4.更改数据
		public void setNode(int index, String data) {
			if (Link.this.foot++ == index) {
				this.data = data;// 满足条件修改数据
			} else {
				this.next.setNode(index, data);
			}
		}

		// 5.删除数据
		public void removeNode(Node previous, String data) {
			if (data.equals(this.data)) {
				previous.next = this.next;
			} else {
				this.next.removeNode(this, data);
			}

		}

		// 6.对象数组
		public void toArrayNode() {
			Link.this.retArray[Link.this.foot++] = this.data;
			if (this.next != null) {
				this.next.toArrayNode();
			}
		}

	}

	// =========================以上为内部类Node=========================
	private Node root;// 保存根节点
	private int count = 0;// 计数器用于计算链表的长度
	private int foot;
	private String retArray[];
	// 1.添加数据方法

	public void add(String data) {
		Node newNode = new Node(data);// 数据打包
		if (this.root == null) {// 判断根节点是否为空
			this.root = newNode;// 如果根节点为空,新节点为根节点
		} else {
			this.root.addNode(newNode);
		}
		this.count++;// 每次保存,长度加1
	}

	// 2.计算长度方法
	public int size() {
		return this.count;
	}

	// 3.链表是否为空方法
	public boolean isEmpty() {
		return this.count == 0;
	}

	// 4.内容查询
	public boolean contains(String data) {
		if (data == null || this.root == null) {// 满足条件直接返回false
			return false;
		}
		return this.root.containsNode(data);// 以上条件不满足,让Node类处理
	}

	// 5.通过索引获取数据
	public String get(int index) {
		if (index > this.count) {
			return null;
		}
		this.foot = 0;
		return this.root.getNode(index);
	}

	// 6.更改链表数据
	public void set(int index, String data) {
		if (index > this.count) {
			return;
		}
		this.foot = 0;// 重新设置foot属性
		this.root.setNode(index, data);// 数据交给Node类处理
	}

	// 7.删除数据
	public void remove(String data) {
		if (this.contains(data)) {
			if (data.equals(this.root.data)) {// 判断是否根节点数据
				this.root = this.root.next;// 根节点的下一个节点为根节点
			} else {

				this.root.next.removeNode(this.root, data);
			}
			this.count--;
		}

	}

	// 8.对象数组转换
	public String[] toArray() {
		if (this.root == null) {
			return null;
		} else {
			this.foot = 0;
			this.retArray = new String[this.count];//定义数组长度
			this.root.toArrayNode();//数据交给Node类处理
			return retArray;
		}
	}
}

public class LinkForm {
	public static void main(String[] args) {
		Link link = new Link();
		link.add("大家好!欢迎来到贪玩揽月!");
		link.add("我是渣渣辉!");
		link.add("装备不用打。全靠送!");
		System.out.println("链表是否包含\"喳喳辉\":" + link.contains("喳喳辉"));
		System.out.println();
		link.remove("我是渣渣辉!");
		link.set(1, "我是古天乐");
		System.out.println(link.size());
		String[] data = link.toArray();
		for (int i = 0; i < data.length; i++) {
			System.out.println(data[i]);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值