数据结构与算法——双向链表的使用原理

双向链表是一种特殊链表,每个节点包含两个指针,一个指向前一个节点,一个指向后一个节点。双向链表可以支持双向遍历,插入和删除操作更加高效。双向链表的基本操作包括插入、删除、查找等。

双向链表的常见应用场景包括LRU缓存淘汰算法双向队列等需要频繁在两端进行操作的场景。

下面我们手写一个双向链表来看看它的用法。

代码如下:

1. 定义双向链表节点类

class ListNode {
    int val;
    ListNode prev;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}

2. 定义双向链表类

class MyDoublyLinkedList {
    private ListNode head;
    private ListNode tail;

    public MyDoublyLinkedList() {
        this.head = null;
        this.tail = null;
    }

    public void addAtHead(int val) {
        ListNode newNode = new ListNode(val);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }
    }

    public void addAtTail(int val) {
        ListNode newNode = new ListNode(val);
        if (tail == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
    }
	
	public void addAtIndex(int index, int val) {
    ListNode newNode = new ListNode(val);
    if (index <= 0) {
        addAtHead(val);
    } else if (index >= size()) {
        addAtTail(val);
    } else {
        ListNode current = head;
        for (int i = 0; i < index - 1; i++) {
            current = current.next;
        }
        newNode.next = current.next;
        current.next.prev = newNode;
        current.next = newNode;
        newNode.prev = current;
    }
}

	public void deleteAtIndex(int index) {
    	if (index < 0 || index >= size()) {
      	  return;
    	}
    	if (index == 0) {
        head = head.next;
        if (head != null) {
            head.prev = null;
        } else {
            tail = null;
        }
    	} else {
        ListNode current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        current.prev.next = current.next;
        if (current.next != null) {
            current.next.prev = current.prev;
        } else {
            tail = current.prev;
        	}
    	}
	}

	public int get(int index) {
    if (index < 0 || index >= size()) {
        return -1;
    }
    ListNode current = head;
    for (int i = 0; i < index; i++) {
        current = current.next;
    }
    return current.val;
}
}

3. 测试双向链表

public class Main {
    public static void main(String[] args) {
        MyDoublyLinkedList dll = new MyDoublyLinkedList();

        dll.addAtHead(1);
        dll.addAtTail(3);
        dll.addAtHead(2);

      
        ListNode current = dll.head;
        while (current != null) {
            System.out.print(current.val + " ");
            current = current.next;
        }
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值