本文将模拟实现java集合框架中的LinkedList并实现其中的增添(插入),删除,查找,获取大小,展示,清空方法。
一、LinkedList
(注:图片来自本站作者薛定谔的喜欢——“java中set集合的特点”,若有侵权必删)
LinkedList作为集合框架中特殊的类,它同时实现了Deque以及List接口,意味着它不仅可以向List发生向上转型实现线性表操作,也可以向Deque发生向上转型实现队列操作。
LinkedList底层由不带头非循环双向链表实现,其中size指示当前链表大小,first指向头节点,last指向尾节点,节点类ListNode包括一个数值域和两个指针域,其中prev指向上一节点,next指向下一节点。
二、代码结构
1.AddIndexPosIlegalException:自定义运行时异常类,用于在输入非法下表时抛出异常。
2.MyLinkedList(包含内部类ListNode:定义节点的数值域和两个指针(引用)域):包含头尾节点引用以及各种链表方法。
3.Test:其main方法用于测试链表各种操作。
三、程序源码
1.AddIndexPosIlegalException类:
//定义添加下标异常类
public class AddIndexPosIlegalException extends RuntimeException{
public AddIndexPosIlegalException() {
super();
}
public AddIndexPosIlegalException(String message) {
super(message);
}
}
注意该异常属于运行时异常,应当继承于RuntimeException。
2.MyLinkedList类:
(1)定义:
public class MyLinkedList
(2)成员变量:
//头节点
public ListNode head;
//尾节点
public ListNode last;
(3)定义节点内部类(类外定义也可)
//内部类定义节点
static class ListNode{
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val){
this.val=val;
}
}
(4)头插方法
//头插方法
public void addFirst(int data){
ListNode listNode=new ListNode(data);
//判断链表是否为空,为空则直接设置head与last
if(head==null){
head=last=listNode;
return;
}
listNode.next=head;
head.prev=listNode;
head=listNode;
}
(5)尾插方法
//尾插方法
public void addLast(int data){
ListNode listNode=new ListNode(data);
//判断链表是否为空,为空则直接执行头插方法
if(head==null){
addFirst(data);
return;
}
last.next=listNode;
listNode.prev=last;
last=listNode;
}
(6)在指定下标之前插入数据的方法(包含判断下标是否合法以及寻找该节点的两个底层方法)
//底层方法,检查下标是否越界
private void checkAddPos(int pos){
if(pos<0||pos>getSize())
throw new AddIndexPosIlegalException("双向链表插入位置不合法");
}
//底层方法,找到下标为pos的直接前驱节点
private ListNode findIndex(int pos){
ListNode cur=head;
while(pos!=0){
cur=cur.next;
pos--;
}
return cur;
}
//在指定下标之前插入节点的方法
public void addIndex(int data,int pos){
//try,catch语句处理下标越界异常
try{
checkAddPos(pos);
}catch (AddIndexPosIlegalException e){
e.printStackTrace();
}
//判断是否为头插
if(pos==0){
addFirst(data);
return;
}
//判断是否为尾插
if(pos==getSize()){
addLast(data);
return;
}
//获取下标为pos的节点
ListNode cur=findIndex(pos);
ListNode listNode=new ListNode(data);
//依次改变四个指针域
listNode.next=cur;
cur.prev.next=listNode;
listNode.prev=cur.prev;
cur.prev=listNode;
}
注:getSize()方法即为获取链表大小的方法,见下文。
(7)删除第一出现值为key的节点的方法
//删除第一个值为key的节点的方法
public void toRemove(int key){
//判断是否为空或单一节点链表
if(head==last){
head=last=null;
return;
}
ListNode cur=head;
while(cur!=null){
if(cur.val==key){
//判断是否为头节点
if(cur==head){
head=head.next;
head.prev=null;
return;
}
//判断是否为尾节点
if(cur==last){
last=last.prev;
last.next=null;
return;
}
//处理一般节点
cur.prev.next=cur.next;
cur.next.prev=cur.prev;
}
cur=cur.next;
}
}
(8)删除所有值为key的节点的方法
删除所有值为key的节点的方法同删除第一次出现值为key的方法,只需把return修改为break即可。
//删除所有值为key的节点的方法(同上即可)
public void removeAll(int key){
ListNode cur=head;
while(cur!=null){
if(cur.val==key){
if(head==last){
head=last=null;
break;
}
if(cur==head){
head=head.next;
head.prev=null;
break;
}
if(cur==last){
last=last.prev;
last.next=null;
break;
}
cur.prev.next=cur.next;
cur.next.prev=cur.prev;
}
cur=cur.next;
}
}
(9)查找链表中是否包含某值的方法
//在链表中查找是否包含值为key的节点的方法
public boolean contains(int key){
ListNode cur=head;
while(cur!=null){
if(cur.val==key){
return true;
}
cur=cur.next;
}
return false;
}
(10)获取链表大小(长度)的方法
//获取链表的大小的方法
public int getSize(){
ListNode cur=head;
int count=0;
while(cur!=null){
count++;
cur=cur.next;
}
return count;
}
(11)展示整个链表的方法
//打印整个链表的方法
public void display(){
ListNode cur=head;
while(cur!=null){
System.out.print(cur.val+" ");
cur=cur.next;
}
System.out.println();
}
(12)清空整个链表的方法
//清空整个链表的方法
public void clear(){
ListNode cur=head;
ListNode suc=cur.next;
while(suc!=null){
cur=null;
cur=suc;
suc=suc.next;
}
cur=null;
head=last=null;
}
四、操作示例
Test类中源码为
public class Test {
public static void main(String[] args) {
//创建链表
MyLinkedList myLinkedList=new MyLinkedList();
//头插演示
myLinkedList.addFirst(1);
myLinkedList.addFirst(2);
myLinkedList.addFirst(3);
myLinkedList.addFirst(4);
//尾插演示
myLinkedList.addLast(1);
myLinkedList.addLast(1);
myLinkedList.addLast(1);
myLinkedList.addLast(1);
myLinkedList.display();
System.out.println("****************");
//删除第一个值为key的节点
myLinkedList.toRemove(2);
myLinkedList.display();
System.out.println("****************");
//删除所有值为key的节点
myLinkedList.removeAll(1);
myLinkedList.display();
System.out.println("****************");
//指定位置插入演示
myLinkedList.addIndex(1,0);
myLinkedList.display();
System.out.println("****************");
//获取大小演示
System.out.println("链表大小为"+myLinkedList.getSize());
System.out.println("****************");
//清空演示
myLinkedList.clear();
myLinkedList.display();
}
}
运行结果如下:
注:最后一行打印为空说明链表已经被清空。