马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
MyLinkedList类
IndexOfLinkedListException类
- public class IndexOfLinkedListException extends RuntimeException {
- public IndexOfLinkedListException() {
- }
- public IndexOfLinkedListException(String message) {
- super(message);
- }
- }
复制代码
Test类
- public class Test {
- public static void main(String[] args) {
- MyLinkedList linkedList = new MyLinkedList();
- // 头插
- linkedList.headInsert(1);
- linkedList.headInsert(2);
- linkedList.headInsert(3);
- // 打印链表
- linkedList.display();
- // 获取长度
- System.out.println(linkedList.size());
- // 尾插
- linkedList.tailInsert(4);
- linkedList.tailInsert(5);
- linkedList.tailInsert(6);
- linkedList.display();
- System.out.println(linkedList.size());
- // 任意位置插入
- linkedList.randomInsert(1, 7);
- linkedList.randomInsert(1, 7);
- linkedList.randomInsert(1, 7);
- linkedList.randomInsert(8, 8);
- linkedList.display();
- linkedList.randomInsert(8, 7);
- linkedList.display();
- linkedList.randomInsert(8, 8);
- linkedList.display();
- System.out.println(linkedList.size());
- linkedList.randomInsert(2, 111);
- linkedList.randomInsert(3, 111);
- linkedList.display();
- System.out.println(linkedList.size());
- // 删除所有和 val 相等的节点
- linkedList.removeAllVal(7);
- linkedList.display();
- System.out.println(linkedList.size());
- // 删除链表中第一个 val 相等的节点
- linkedList.remove(111);
- linkedList.remove(6);
- linkedList.remove(3);
- linkedList.display();
- System.out.println(linkedList.size());
- // 删除 index 处的节点
- linkedList.removeOfIndex(1);
- linkedList.removeOfIndex(6);
- linkedList.removeOfIndex(2);
- linkedList.display();
- System.out.println(linkedList.size());
- // 找到 val 第一次出现的位置
- System.out.println(linkedList.indexOf(2));
- System.out.println(linkedList.indexOf(8));
- // 找到val 最后一次出现的位置
- System.out.println(linkedList.lastOfIndex(2));
- System.out.println(linkedList.lastOfIndex(8));
- // 清空链表
- linkedList.clear();
- System.out.println(linkedList.size());
- }
- }
复制代码 |