《Java数据结构与算法》笔记-CH5-链表-9实现带迭代器的链表

本文介绍了一种简单的链表实现方法,并在此基础上实现了迭代器功能。通过具体代码示例展示了如何进行节点插入、删除等操作,以及如何利用迭代器遍历链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 节点类
 */
class LinkNode {
	private long data;
	public LinkNode next;

	public LinkNode(long d) {
		this.data = d;
	}

	public String toString() {
		return String.valueOf(this.data);
	}
}

/**
 * 带迭代器的链表
 */
class LinkListWithIterator {
	private LinkNode first;//链表头结点

	public LinkListWithIterator() {
		first = null;
	}

	public LinkNode getFirst() {
		return first;
	}

	public void setFirst(LinkNode ln) {
		this.first = ln;
	}

	public boolean isEmpty() {
		return first == null;
	}
	/**
	 * 得到迭代器
	 * @return
	 */
	public ListIterator getIter() {
		return new ListIterator(this);
	}
	
	@Override
	public String toString() {
		if (isEmpty())
			return "[]";
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		LinkNode current = first;
		while (current != null) {
			sb.append(current.toString()).append(",");
			current = current.next;
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("]");
		return sb.toString();
	}
}
/**
 * 迭代器
 */
class ListIterator {
	private LinkListWithIterator ourList;
	private LinkNode previous;
	private LinkNode current;
	/**
	 * 初始化时传入相应的链表
	 * @param list
	 */
	public ListIterator(LinkListWithIterator list) {
		this.ourList = list;
		reset();
	}
	/**
	 * 重置迭代器
	 */
	public void reset() {
		current = ourList.getFirst();
		previous = null;
	}
	/**
	 * 判断迭代器是否到了链表末尾
	 * @return
	 */
	public boolean atEnd() {
		return current.next == null;
	}
	/**
	 * 迭代器指针向下移动
	 */
	public void nextLink() {
		previous = current;
		current = current.next;
	}
	/**
	 * 获取迭代器当前节点
	 * @return
	 */
	public LinkNode getCurrent() {
		return current;
	}
	/**
	 * 从迭代器当前current处向后面插入新节点
	 * @param node
	 */
	public void insertAfter(LinkNode node) {
		if (ourList.isEmpty()) {
			ourList.setFirst(node);
			current = node;
		} else {
			node.next = current.next;
			current.next = node;
			nextLink();
		}
	}
	/**
	 * 从迭代器当前current处向前面插入新节点
	 * @param node
	 */
	public void insertBefore(LinkNode node) {
		if (previous == null) {
			node.next = ourList.getFirst();
			ourList.setFirst(node);
			reset();
		} else {
			node.next = previous.next;
			previous.next = node;
			current = node;
		}
	}
	/**
	 * 删除迭代器当前节点
	 * @return
	 */
	public LinkNode deleteCurrent() {
		LinkNode temp = current;
		if (previous == null) {
			ourList.setFirst(current.next);
			reset();
		} else {
			previous.next = current.next;
			if (atEnd())
				reset();
			else
				current = current.next;
		}
		return temp;
	}
}

public class IteratorDemo {
	public static void main(String[] args) {
		LinkListWithIterator list = new LinkListWithIterator();
		ListIterator it = new ListIterator(list);
		for (int i = 0; i < 6; i++) {
			long rand = (long) (Math.random() * 100);
			LinkNode node = new LinkNode(rand);
			System.out.print("生成节点:" + node.toString() + ",");
			if (i % 2 == 0) {
				System.out.print("当前current为:" + it.getCurrent());
				it.insertBefore(node);
				System.out.println(",执行insertBefore:" + list.toString());
			} else {
				System.out.print("当前current为:" + it.getCurrent());
				it.insertAfter(node);
				System.out.println(",执行insertafter:" + list.toString());
			}
		}
		System.out.println("list最终为:" + list.toString());
		System.out.println("current节点为" + it.getCurrent() + ",删除节点" + it.deleteCurrent() + ",结果为:" + list.toString());
		System.out.println("此时current为:" + it.getCurrent());
		it.reset();
		System.out.println("重置后current为:" + it.getCurrent());
		while (!list.isEmpty()) {
			System.out.println("删除current:" + it.deleteCurrent() + ",结果为:" + list.toString());
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值