Data structure-4 双向链表 DoubleLinkedList--Java语言实现

本文介绍了双向链表的概念,包括链表链接、前驱和后继节点的定义,并给出了Java语言实现的双向链表demo,展示了双向链表在数据结构中的应用。
摘要由CSDN通过智能技术生成

1. 双向链表简介

Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list.

Link − Each link of a linked list can store a data called an element.

Next − Each link of a linked list contains a link to the next link
called Next.

Prev − Each link of a linked list contains a link to the previous link
called Prev.

LinkedList − A Linked List contains the connection link to the first
link called First and to the last link called Last.

双向链表是一种单链表的变体,双向链表可以双向遍历一个链表,维护一个head和tail节点实现,其实现细节和单链表相似度较高。
这里写图片描述

2. 双向链表demo

//DoubleList.java
package com.fqyuan.doublelinklist;

public class DoubleList<T> {
    private Node head;
    private Node tail;
    private int num;

    public DoubleList() {
        head = new Node();
        tail = new Node();
        head.next = tail;
        tail.prev = head;
    }

    public boolean isEmpty() {
        return num == 0;
    }

    public int getNum() {
        return num;
    }

    // Insert before the head node.
    public boolean insertAtStart(T item) {
        if (isEmpty()) {
            head.value = item;
            head.prev = null;
            head.next = tail;
            num++;
            return true;
        } else {
            Node newNode = new Node();
            newNode.value = item;
            newNode.prev = null;
            newNode.next = head;

            head.prev = newNode;
            head = newNode;
            num++;
            return true;
        }
    }

    // Insert after the end node.
    public boolean insertAtEnd(T item) {
        if (isEmpty()) {
            tail.value = item;
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值