双链表 有三个属性,一个数值,一个next域,一个prev域。在构造时只需要传递一个数值即可。双链表还有头head和尾last
public class DoubleLinkedlist {
static class ListNode{
public Integer val;
public ListNode prev;
public ListNode next;
public ListNode(Integer val) {
this.val = val;
}
}
public ListNode head;//标记头结点
public ListNode last;//标记尾结点
}
在主函数里面创建一个链表的引用
public class MyDoubleLinkedlist {
public static void main(String[] args) {
DoubleLinkedlist doubleLinkedlist = new DoubleLinkedlist();
}
}
下面实现双链表的功能
1.遍历链表功能
和单链表一样,遇到空时停止遍历
//遍历双链表
public void display() {
ListNode temp = head;
while (temp != null) {
System.out.print(temp.val + " ");
temp = temp.next;
}
}
2.头插法创建双链表
* 思路: * 1.创建节点 * 2.首先先判断链表是否为空,是,则直接让head = node,last = node; * 不为空,则让node.next = head;head.prev = node;head = node;
public class DoubleLinkedlist {
static class ListNode{
public Integer val;
public ListNode prev;
public ListNode next;
public ListNode(Integer val) {
this.val = val;
}
}
public ListNode head;//标记头结点
public ListNode last;//标记尾结点
public void headInsert(int num){
ListNode node = new ListNode(num);
if(head==null){
head = node;
last = node;
}else{
node.next = head;
head.prev = node;
head = node;
}
}
//遍历双链表
public void display() {
ListNode temp = head;
while (temp != null) {
System.out.print(temp.val + " ");
temp = temp.next;
}
}
}
在主函数里面实现一下
public static void main(String[] args) {//验证头插
DoubleLinkedlist doubleLinkedlist = new DoubleLinkedlist();
doubleLinkedlist.headInsert(12);
doubleLinkedlist.headInsert(1);
doubleLinkedlist.headInsert(2);
doubleLinkedlist.display();
}
结果
3.尾插法创建双链表
* 思路: * 1.创建节点 * 2.首先先判断链表是否为空,是,则直接让head = node,last = node; * 不为空,则让last.next = node;node.prev = last;last = node;
public void finallInsert(int num){
ListNode node = new ListNode(num);
if(head==null){
head = node;
last = node;
}else{
last.next = node;
node.prev = last;
last = node;
}
}
//遍历双链表
public void display() {
ListNode temp = head;
while (temp != null) {
System.out.print(temp.val + " ");
temp = temp.next;
}
}
在主函数里面实现一下
public static void main(String[] args) {//验证尾插
DoubleLinkedlist doubleLinkedlist = new DoubleLinkedlist();
doubleLinkedlist.finallInsert(10);
doubleLinkedlist.finallInsert(14);
doubleLinkedlist.finallInsert(19);
doubleLinkedlist.display();
}
结果
4.指定下表插入创建双链表
* 思路 * 1.创建节点 * 2.判断节点的合法性,当节点为0时,直接头插法;当节点为size()时,直接尾插法 * 3.当插入的位置在中间某个位置时,遍历找到要插入的节点位置 * 然后执行插入: * 将node与cur前一个节点相连接:cur.prev.next = node;node.prve = cur.prev;
public void indexInsert(int index,int num){
ListNode node = new ListNode(num);
if(index<0||index>Listsize()){
System.out.println("输入的位置不合法,无法插入");
return;
}
if(index==0){//当输入的位置在头结点位置时,执行头插法
headInsert(num);
return;
}
if(index==Listsize()){//当输入的位置在尾结点时,执行尾插法
finallInsert(num);
return;
}
ListNode cur = serchindex(index);
cur.prev.next = node;
node.prev = cur.prev;
node.next = cur;
cur.prev = node;
}
//求双链表长度
public int Listsize(){
ListNode temp = head;
int len = 0;
while(temp!=null){
len++;
temp = temp.next;
}
return len;
}
主函数里面实现一下
public static void main(String[] args) {//验证头插、尾插、指定下表插入
DoubleLinkedlist doubleLinkedlist = new DoubleLinkedlist();
doubleLinkedlist.headInsert(12);
doubleLinkedlist.headInsert(1);
doubleLinkedlist.headInsert(2);
doubleLinkedlist.finallInsert(10);
doubleLinkedlist.finallInsert(14);
doubleLinkedlist.finallInsert(19);
doubleLinkedlist.indexInsert(3,100);
doubleLinkedlist.display();
}
结果
5.查找双链表中是否包含某个关键数据key
* 思路:首先判断链表是否为空,是则返回false。不为空则遍历,判断节点的val是否与Key相等,相等则返回true;遍历完以后都没有找到,就返回false
public boolean serchNode(int data){
if(head==null){
return false;
}
ListNode temp = head;
while (temp != null) {
if (temp.val==data) {
return true;
}
temp = temp.next;
}
return false;
}
主函数里面实现一下
public static void main(String[] args) {//验证头插、尾插、指定下表插入、查找关键数据
DoubleLinkedlist doubleLinkedlist = new DoubleLinkedlist();
doubleLinkedlist.headInsert(12);
doubleLinkedlist.headInsert(1);
doubleLinkedlist.headInsert(2);
doubleLinkedlist.finallInsert(10);
doubleLinkedlist.finallInsert(14);
doubleLinkedlist.finallInsert(19);
doubleLinkedlist.indexInsert(3,100);
doubleLinkedlist.display();
System.out.println();
if(doubleLinkedlist.serchNode(1)){
System.out.println("存在");
}else{
System.out.println("不存在");
}
}
结果
6.删除第一次出现关键字key的节点
* 思路: * 1.首先先判断链表是否为空,是则返回 * 2.遍历要删除的节点 * 3.如果删除的是头结点,则让head.next.prev = null;head = head.next; * 4.如果删除的是尾结点,则让last.prev.next = null;last = last.prev; * 5.如果删除的是中间某个节点,则cur.prev.next = cur.next;cur.next.prev = cur.prev;
public void deletefristKey(int key){
if(head==null) return;
ListNode cur = head;
while(cur!=null) {
if (cur.val == key) {//说明找到了要删除的节点
//判断是不是要删除头节点
if (head.val == key) {
head = head.next;
if (head != null) {//说明链表不只有一个节点
head.prev = null;
}
}
else{//判断中间节点和尾结点
cur.prev.next = cur.next;
if (cur.next != null) {//说明不是最后一个节点
cur.next.prev = cur.prev;
} else {
last = last.prev;
}
}
return;
}
cur = cur.next;
}
}
主函数里面实现一下
public static void main(String[] args) {//验证头插、尾插、指定下表插入、查找关键数据、删除第一次出现关键字key的节点
DoubleLinkedlist doubleLinkedlist = new DoubleLinkedlist();
doubleLinkedlist.headInsert(12);
doubleLinkedlist.headInsert(1);
doubleLinkedlist.headInsert(2);
doubleLinkedlist.finallInsert(10);
doubleLinkedlist.finallInsert(14);
doubleLinkedlist.finallInsert(19);
doubleLinkedlist.indexInsert(3,100);
doubleLinkedlist.display();
System.out.println();
if(doubleLinkedlist.serchNode(1)){
System.out.println("存在");
}else{
System.out.println("不存在");
}
doubleLinkedlist.deletefristKey(12);
doubleLinkedlist.display();
}
结果