Java 实现LinkedList

Java 实现LinkedList

附有详细注释,未实现泛型
其他文章链接:Java 实现ArrayList https://blog.csdn.net/qq_39004632/article/details/103267407。

类设计:

主类:MyLinked
节点类:Node
自定义异常类:MyListIndexOutOfBoundException
测试类:TestMyLinked

主类:MyLinked

/**
 * 自定义ArratList
 * 
 * @author WTF
 *
 */
public class MyLinked {

	private Node head = null;// 首节点,默认为null
	private Node foot = null;// 尾节点,默认为null
	static int index = 0;// 节点下标

	/**
	 * 无参构造
	 */
	public MyLinked() {

	}

	/**
	 * 有参构造
	 * 
	 * @param first
	 */
	public MyLinked(Node first) {
		Node n = new Node(first.data, null);// 创建新的节点,当作此链表的首元素
		this.head = n;// 初始节点是首节点
		this.foot = n;// 初始节点是尾节点
		MyLinked.index++;
		n.index = MyLinked.index;// 初始节点的下标
	}

	/**
	 * 校验输入下标是否合法
	 * 
	 * @param index
	 *            下标
	 */
	private void checkIndex(int index) {
		if (index > MyLinked.index || index < 1) {
			throw new MyListIndexOutOfBoundException("数组下标范围越界,范围是1——" + (MyLinked.index));
		}
	}

	/**
	 * 获取链表的长度
	 * 
	 * @return 链表长度
	 */
	public int size() {
		// 合法性校验
		return MyLinked.index;
	}

	/**
	 * 获取此链表指定位置的元素的值
	 * 
	 * @param index
	 *            指定元素的下标
	 * @return 返回制定下标元素的值
	 */
	public int get(int index) {
		// 合法性校验
		checkIndex(index);

		// 指定下标元素的值
		int out = 0;

		// 指定下标的元素
		Node n = this.head;

		// 循环查找指定下标元素的值
		for (int i = 1; i <= MyLinked.index; i++) {
			if (i == index) {
				out = n.data;
				break;
			}
			n = n.next;
		}
		return out;
	}

	/**
	 * 链表的添加方法:将输入的数据插到链表的最后
	 * 
	 * @param values
	 */
	public void add(int value) {
		// 创建新的节点
		Node n = new Node(value, null);

		// 判断当前链表是否有元素
		if (this.head == null && this.foot == null) {// 将此元素作为当前链表的首元素
			this.head = n;// 初始节点是首节点
			this.foot = n;// 初始节点是尾节点
			n.index = 1;
			return;
		}

		// 当前链表里已经有元素
		this.foot.next = n;
		n.index = MyLinked.index;
		this.foot = n;
	}

	/**
	 * 链表的删除方法:将输入的下标位置的元素删除
	 * 
	 * @param index
	 *            要删除元素的下标
	 * @return 返回指定删除元素的数据
	 */
	public int remove(int index) {
		// 合法性校验
		checkIndex(index);

		// 指定下标的前一个元素
		Node n_before = new Node();

		// 指定下标的后一个元素
		Node n_after = new Node();

		// 指定下标元素的值
		int out = 0;

		// 如果指定下标是第一个元素
		if (index == 1) {
			out = this.head.data;
			this.head = this.head.next;
			change();
			return out;
		}

		// 如果指定下标是最后一个元素
		if (index == MyLinked.index) {
			out = this.foot.data;
			this.foot.index = (this.foot.index - 1);
			change();
			return out;
		}

		// 创建新的节点
		Node n = this.head;

		// 循环,找出指定下标的前一个元素、后一个元素
		for (int i = 1; i <= MyLinked.index; i++) {
			if (i == index - 1) {
				out = n.next.data;
				n.next = n.next.next;
				break;
			}
			n = n.next;
		}

		this.foot.index = this.foot.index - 1;
		change();
		return out;
	}

	/**
	 * 链表的修改功能:修改指定位置元素的值
	 * 
	 * @param index
	 *            被修改元素的下标
	 * @param value
	 *            被修改元素的新值
	 * @return 被修改元素的旧值
	 */
	public int edit(int index, int value) {

		checkIndex(index);

		Node n = this.head;
		int out = -1;

		for (int i = 1; i < MyLinked.index; i++) {
			if (i == index) {
				out = n.data;
				n.data = value;
				break;
			}
			n = n.next;
		}
		return out;
	}

	/**
	 * 展示链表里的所有值和下标
	 */
	public void show() {
		Node n = this.head;
		for (int i = 1; i <= MyLinked.index; i++) {
			System.out.println("下标:" + i + ",值:" + n.data);
			n = n.next;
		}
	}

	/**
	 * 修改节点下标
	 */
	public void change() {
		MyLinked.index--;
		Node n = this.head;
		for (int i = 1; i <= MyLinked.index; i++) {
			n.index = i;
			n = n.next;
		}
	}

}

节点类:Node

/**
 * Node的类设计
 * 
 * @author WTF
 *
 */
public class Node {
	int data = 0;// 存储数据
	Node next = null;// 下一个节点
	int index;// 节点下标

	/**
	 * 无参构造
	 */
	public Node() {

	}

	/**
	 * 带参构造
	 * 
	 * @param data
	 *            节点的的数据
	 * @param next
	 *            节点的下一个
	 */
	public Node(int data, Node next) {
		this.data = data;
		this.next = next;
		this.index = ++MyLinked.index;
	}
}

自定义异常类:MyListIndexOutOfBoundException

public class MyListIndexOutOfBoundException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public MyListIndexOutOfBoundException() {
		super();
	}

	public MyListIndexOutOfBoundException(String message) {
		super(message);
	}

}

测试类:TestMyLinked

public class TestMyLinked {
	public static void main(String[] args) {

		MyLinked ml = new MyLinked();

		ml.add(9);
		ml.add(7);
		ml.add(2);
		ml.add(5);
		ml.add(1);
		System.out.println("----------------");
		ml.show();
		System.out.println("----------------");
		System.out.println("链表大小:" + ml.size());
		System.out.println("----------------");
		System.out.println("链表下标为2的节点值:" + ml.get(2));
		System.out.println("----------------");
		System.out.println("删除链表下标为4的节点,其值为:" + ml.remove(4));
		System.out.println("----------------");
		System.out.println("修改链表下标为3的节点的值为4,修改前的值为:" + ml.edit(3, 4));
		System.out.println("----------------");
		ml.show();
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值