双向链表的定义
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表任意一个结点开始,都可以很方便的访问它的前驱结点和后继结点。
简易的双向链表模型
目录
模拟实现简单的双向链表
定义结点类
初始化
打印双向链表
头插法创建双向链表
尾插法创建双向链表
判断双向链表是否包含关键字key
求双向链表的长度
寻找待插入的位置
双向链表的插入
按值查找待删除的位置
双向链表的删除(第一次出现的key)
双向链表的删除(删除所有的key值)
双向链表的清空操作
完整代码
模拟实现简单的双向链表
在自己实现的双向链表中需要包括以下功能:增加元素(头插、尾插)、查找元素、修改元素、删除元素、获取链表的长度、清空链表.
定义结点类
每个结点包括两个指针、数据域还有结点数据域的构造方法,则根据双向链表的定义,可以建立如下结点类
class ListNode {
public int val;
public ListNode next;//前驱信息
public ListNode prev;//后继信息
public ListNode(int val) {
this.val = val;
}
}
初始化
last用来标记双向链表的尾结点,head用来标记它的头结点.
public class MyLinkedList {
public ListNode head;//标记双向链表的头结点
public ListNode last;//标记双向链表的尾结点
}
打印双向链表
双向链表的打印可以有两种方法,一种是根据head来正向打印链表,另一种是根据last标记的尾结点来反向打印.
①正向打印
//利用head正向打印双向链表
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
}
②反向打印
//利用last反向打印双向链表
public void display1() {
ListNode cur = this.last;
while(cur != null) {
System.out.print(cur.val+" ");
cur = cur.prev;
}
}
头插法创建双向链表
在对双向链表进行头插法插入时,需要考虑到该结点的prev和next域,相较于单链表来说比较麻烦,具体插入细节如下图所示:
假设目前双向链表有四个结点,现要头插法插入一个结点,则:
在进行插入时,需要使head的prev指向新建的node结点,node结点的next指向当前的head即可完成头插法插入.
具体代码如下:
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(this.head == null) {
this.head = node;
this.last = node;
} else {
node.next = this.head;
this.head.prev = node;
this.head = node;
}
}
尾插法创建双向链表
在使用尾插法插入时,可以直接利用last标记的尾结点来实现尾插插入,将last.next指向node,并将node.prev指向当前的last,最后再令last = node,即可完成尾插法插入,最终last依旧保存链表最后一个结点的信息
//尾插法
public void addLast (int data) {
ListNode node = new ListNode(data);
if(this.head == null) {
this.head = node;
this.last = node;
} else {
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
判断双向链表是否包含关键字key
在进行判断时,有两种方式,一种是利用head从头到尾遍历链表来判断是否包含关键字key,另一种方式是利用last从尾到头来遍历链表判断是否包含关键字key.下述代码为从头到尾遍历:
//判断双向链表是否包含关键字key
public boolean contains(int key) {
ListNode cur = this.head;
while(cur != null) {
cur = cur.next;
if(cur.val == key) {
return true;
}
}
return false;
}
求双向链表的长度
在求长度时,也是两种方法,一种从头到尾的遍历方式,另一种为从尾到头的头的方式,下述代码列举从尾到头的遍历求长度方式.
//得到单链表的长度
public int size() {
ListNode cur = this.last;
int size = 0;
while(cur != null) {
cur = cur.prev;
size++;
}
return size;
}
寻找待插入的位置
根据给定的插入位置,来寻找到要插入位置现在的结点
//寻找要插入的位置
public ListNode findIndex(int index) {
int pos = 0;
ListNode cur = this.head;
while(pos != index) {
cur = cur.next;
pos++;
}
return cur;
}
双向链表的插入
在进行插入前,首先要对特殊情况进行单独处理:
①插入位置是否合法;
②如果是插入在第一个位置,则直接利用头插法插入;
③如果插入在最后一个位置,则直接利用尾插法插入.
如果不满足上述三种情况,则进行正常插入,如下图所示,假设要将新建的node结点插入在第二个位置
要插入在第二个位置,所以定义cur使其处在地址为0x111的位置,之后再对该结点的前驱和后继进行处理即可.
具体代码如下
//指定位置插入key
public void addIndex(int index, int data) {
if(index < 0 || index > size()) {
throw new RuntimeException("操作位置不合法");
}
if(index == 0) {
addFirst(data);
return;
}
if(index == size()) {
addLast(data);
return;
}
ListNode node = new ListNode(data);
ListNode cur = findIndex(index);
cur.prev.next = node;
node.prev = cur.prev;
node.next = cur;
cur.prev = node;
}
按值查找待删除的位置
遍历链表来寻找等于key的结点即可
//按值查找结点位置
public ListNode findKeyIndex(int key) {
ListNode cur = this.head;
while(cur != null) {
if(cur.val == key) {
return cur;
}
cur = cur.next;
}
return null;
}
双向链表的删除(第一次出现的key)
在进行删除操作之前,需要对以下特殊情况进行处理:
①判断链表是否为空,若为空则不能进行删除操作,抛出异常;
②判断待删除的位置是否是头结点,如果是,则直接特殊处理;
如若不满足上述特殊情况,则进行正常的删除操作,假设目前的链表如下图所示,需要删除key == 4的结点,则首先使cur处在val值为4的结点处:
则利用cur对其前驱和后继进行操作即可实现删除操作
具体代码如下:
//删除第一次出现关键字为key的节点
public void remove(int key) {
if(this.head == null) {
throw new RuntimeException("链表为空,无法进行删除操作");
}
if(this.head.val == key) {
this.head = this.head.next;
if(head != null) {
this.head.prev = null;
}
return;
}
ListNode cur = findKeyIndex(key);
if(cur == last) {
this.last = this.last.prev;
if(head != null) {
this.last.next = null;
}
return;
}
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
双向链表的删除(删除所有的key值)
在进行删除操作前首先需要判断链表是否空,如果为空则直接抛出异常,停止程序;
首先令cur = head.next,将头结点和尾结点空出来,在程序的最后对这两个结点进行特殊处理,利用cur遍历链表,如果遇到结点的值等于key值的情况,则进行删除,删除流程与删除单一删除基本一致,cur遍历的停止条件是 cur == last.
再对头结点和尾结点进行判断即可完成该操作.
//删除所有值为key的节点
public void removeAll(int key) {
if(this.head == null) {
throw new RuntimeException("链表为空,无法进行删除操作");
}
ListNode cur = this.head.next;
while(cur != this.last) {
if(cur.val == key) {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
cur = cur.next;
}
if(this.head.val == key) {
this.head = this.head.next;
if(head != null) {
this.head.prev = null;
}
}
if(this.last.val == key) {
this.last = this.last.prev;
if(head != null) {
this.last.next = null;
}
}
}
双向链表的清空操作
双向链表的清空操作需要同时将head和last置为null,如果只将head置为null,则该双链表正向的断开了,但是反向链接还在,例如:
因此需要同时进行置空操作
//清空单链表
public void clear(){
this.head = null;
this.last = null;
}
完整代码
package doublelinkedlist;
class ListNode {
public int val;
public ListNode next;//前驱信息
public ListNode prev;//后继信息
public ListNode(int val) {
this.val = val;
}
}
public class MyLinkedList {
public ListNode head;//标记双向链表的头结点
public ListNode last;//标记双向链表的尾结点
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(this.head == null) {
this.head = node;
this.last = node;
} else {
node.next = this.head;
this.head.prev = node;
this.head = node;
}
}
//尾插法
public void addLast (int data) {
ListNode node = new ListNode(data);
if(this.head == null) {
this.head = node;
this.last = node;
} else {
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
//寻找要插入的位置
public ListNode findIndex(int index) {
int pos = 0;
ListNode cur = this.head;
while(pos != index) {
cur = cur.next;
pos++;
}
return cur;
}
//指定位置插入key
public void addIndex(int index, int data) {
if(index < 0 || index > size()) {
throw new RuntimeException("操作位置不合法");
}
if(index == 0) {
addFirst(data);
return;
}
if(index == size()) {
addLast(data);
return;
}
ListNode node = new ListNode(data);
ListNode cur = findIndex(index);
cur.prev.next = node;
node.prev = cur.prev;
node.next = cur;
cur.prev = node;
}
//判断双向链表是否包含关键字key
public boolean contains(int key) {
ListNode cur = this.head;
while(cur != null) {
cur = cur.next;
if(cur.val == key) {
return true;
}
}
return false;
}
//打印双向链表,正向打印
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
}
//打印双向链表,反向打印
public void display1() {
ListNode cur = this.last;
while(cur != null) {
System.out.print(cur.val+" ");
cur = cur.prev;
}
}
//按值查找结点位置
public ListNode findKeyIndex(int key) {
ListNode cur = this.head;
while(cur != null) {
if(cur.val == key) {
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if(this.head == null) {
throw new RuntimeException("链表为空,无法进行删除操作");
}
if(this.head.val == key) {
this.head = this.head.next;
this.head.prev = null;
return;
}
ListNode cur = findKeyIndex(key);
if(cur == last) {
this.last = this.last.prev;
this.last.next = null;
return;
}
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
//删除所有值为key的节点
public void removeAll(int key) {
if(this.head == null) {
throw new RuntimeException("链表为空,无法进行删除操作");
}
ListNode cur = this.head.next;
while(cur != this.last) {
if(cur.val == key) {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
cur = cur.next;
}
if(this.head.val == key) {
this.head = this.head.next;
this.head.prev = null;
}
if(this.last.val == key) {
this.last = this.last.prev;
this.last.next = null;
}
}
//得到单链表的长度
public int size() {
ListNode cur = this.last;
int size = 0;
while(cur != null) {
cur = cur.prev;
size++;
}
return size;
}
//清空单链表
public void clear(){
this.head = null;
this.last = null;
}
}