单向链表(2021-11-12)

package packageA;

interface ILink<A> {
	public void add(A data); // 增加节点

	public int getLength(); // 获取节点个数

	public boolean isEmpty(); // 判断链表是否为空

	public Object[] toArray(); // 链表转化为数组

	public A getData(int index); // 根据序号获取数据

	public void setData(int index, A data); // 根据序号赋值

	public boolean contains(A data); // 判断数据是否在链表中

	public void remove(A data); // 删除节点

	public void clean(); // 清空链表
}

class LinkImpl<A> implements ILink<A> {
	class Node { // 将节点设为内部类
		private A data;
		private Node next = null;

		public Node(A data) {
			this.data = data;
		}
	}

	private Node root = null;
	private int length = 0;
	private Object[] array = null;

	public void add(A data) {
		Node newNode = new Node(data);
		this.length++;

		if (this.root == null) {
			this.root = newNode;
		} else {
			Node temp = this.root;
			while (temp.next != null) {
				temp = temp.next;
			}
			temp.next = newNode;
		}
	}

	public int getLength() {
		return this.length;
	}

	public boolean isEmpty() {
		return this.root == null;
	}

	public Object[] toArray() {
		if (this.isEmpty()) {
			return null;
		} else {
			array = new Object[this.length];
			Node temp = this.root;
			int counter = 0;
			while (temp != null) {
				array[counter++] = temp.data;
				temp = temp.next;
			}
			return array;
		}
	}

	public A getData(int index) {
		Node temp = this.root;
		while (index != 0) {
			temp = temp.next;
			index--;
		}
		return temp.data;
	}

	public void setData(int index, A data) {
		Node temp = this.root;
		while (index != 0) {
			temp = temp.next;
			index--;
		}
		temp.data = data;
	}

	public boolean contains(A data) {
		Node temp = this.root;
		while (temp != null) {
			if (data.equals(temp.data)) {
				return true;
			} else
				temp = temp.next;
		}
		return false;
	}

	public void remove(A data) {
		if (this.root == null) {
			return;
		}
		if (data.equals(this.root.data)) {
			root = root.next;
		}
		Node previous = this.root;
		Node temp = this.root.next;
		while (temp != null) {
			if (data.equals(temp.data)) {
				previous.next = temp.next;
				this.length--;
				temp = null;
			} else {
				previous = temp;
				temp = temp.next;
			}
		}
	}

	public void clean() {
		this.root = null;
		this.length = 0;
	}

}

public class Main {
	public static void main(String[] args) {
		ILink<String> link = new LinkImpl<String>();
		System.out.println("[当前节点个数]" + link.getLength() + " 、[链表是否为空]" + link.isEmpty());
		link.add("你好");
		link.add("世界");
		link.add("我是战士k");
		System.out.println("——————————转化为数组,然后输出——————————");
		for (Object data : link.toArray()) {
			System.out.println(data);
		}
		System.out.println("[当前节点个数]" + link.getLength() + " 、[链表是否为空]" + link.isEmpty());
		System.out.println();
		System.out.println("[2号节点数据]" + link.getData(2));
		link.setData(2, "我是fighterk");
		System.out.println("[修改后的2号节点数据]" + link.getData(2));
		System.out.println("[包含“世界”吗]" + link.contains("世界"));
		System.out.println("[包含“world”吗]" + link.contains("world"));
		link.remove("世界");
		System.out.println("——————————删除了“世界”之后的链表——————————");
		for (Object data : link.toArray()) {
			System.out.println(data);
		}
		link.clean();
		System.out.println("[清空的链表isEmpty]"+link.isEmpty());
		
	}
}

posted @   fighterk  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示