双链表 [英] Double Linked List

查看:76
本文介绍了双链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

///我有一个看起来像这样的链表

//I have a single linked list that looks like this

node head = new Node ();
head.info = 3;
head.next = new Node ();
head.next.info = 6 ();
head.next.next = new Node ();
head.next.next.info = 9;
head.next.next.next = null;

///我将如何编写双链表?

//How would I write a double linked list?

class Double Node
{
    info;
    doubleNode prev;
    doubleNode next;
}


推荐答案

这是创建的部分双重链接列表并向其中添加节点。
您可以根据需要添加删除和插入功能来对其进行更新。

Here is the part for creating a double linked list and adding nodes to it. You can update it by adding remove and insert functions as you want.

class Node{
    Node prev;
    Node next;
    int value;

    public Node(int value) {
        this.value = value;
    }
}

class DoubleLinkedList{
    Node head;
    Node tail;

    public DoubleLinkedList(Node head) {
        this.head = head;
        this.tail = head;
        head.next = null;
        head.prev = null;
    }

    public void addNode(Node node){
        tail.next = node;
        node.prev = tail;
        tail = node;
    }
}

public class Main {
    public static void main(String args[]){
        Node head= new Node(3);
        DoubleLinkedList list = new DoubleLinkedList(head);
        list.addNode(new Node(5));
        list.addNode(new Node(6));
        list.addNode(new Node(7));
        list.addNode(new Node(8));
    }
 }

这篇关于双链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆