Java双向链表

本文介绍了链表作为数组替代品的原因,强调了其动态分配空间的优势。文章详细讲解了Java中链表节点的结构,并探讨了如何简化节点创建与连接,特别是对于双向链表的实现进行了讨论。
摘要由CSDN通过智能技术生成

链表的基本介绍

如果现在要保存多个对象,那么首先可以想到的就是对象数组;如果我们想要保存多个任意对象,那么可以想到的一定是Object型的数组。

Object[] data=new Object[4];

但是数组最大的缺点就是长度固定,在实际开发中我们并不知道自己到底要用多少个对象,所以如果我们使用对像数组申请的空间多或者少都有可能造成空间浪费。这个时候,我们就想定义一个动态申请空间有多少数据就存多少数据的结构——链表。
范例:Java链表结点结构

class Node{
    private Object data;//保存节点中的数据
    private Node next;//引用,指向下一个结点
    
    //构造函数
    public Node(Object data){
        this.data=data;
    }
    //得到对象数据
    public Object getData(){
        return this.data;
    }
    //得到对象的下一个结点
    public Node getNext(){
        return this.next;
    }
    //设置对象的下一个结点
    public void setNext(Node next){
        this.next=next;
    }
    //设置对象的数据
    public void setData(Object data){
        this.data=data;
    }

}
public class Test {
    public static void main(String[] args){
        //封装结点
        Node head=new Node("头结点");
        Node first=new Node("结点1");
        Node second=new Node("结点2");
        Node last=new Node("尾结点");
        //结点挂载
        head.setNext(first);
        first.setNext(second);
        second.setNext(last);
        //打印结点
        print(head);
    }
    public static void print(Node node){
        Node cur=node;
        while(cur!=null){
            System.out.println(node.getData());
            node=node.getNext();
        }
    }
}

观察上面链表的实现&#

Java中实现双向链表需要定义一个节点类,节点类中包含一个数据项和两个指针,分别指向前一个节点和后一个节点。下面是一个简单的Java代码示例: ```java class Node { int data; Node prev; Node next; public Node(int data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { Node head; public DoublyLinkedList() { this.head = null; } // 在链表头部插入一个节点 public void insertAtHead(int data) { Node newNode = new Node(data); newNode.next = head; if (head != null) { head.prev = newNode; } head = newNode; } // 在链表尾部插入一个节点 public void insertAtTail(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; newNode.prev = current; } // 在指定位置插入一个节点 public void insertAtPosition(int data, int position) { Node newNode = new Node(data); if (head == null || position == 1) { newNode.next = head; if (head != null) { head.prev = newNode; } head = newNode; return; } Node current = head; int currentPosition = 1; while (currentPosition < position - 1 && current.next != null) { current = current.next; currentPosition++; } newNode.next = current.next; newNode.prev = current; current.next = newNode; if (newNode.next != null) { newNode.next.prev = newNode; } } // 从链表中删除一个节点 public void deleteNode(int data) { if (head == null) { return; } Node current = head; while (current != null) { if (current.data == data) { if (current.prev != null) { current.prev.next = current.next; } else { head = current.next; } if (current.next != null) { current.next.prev = current.prev; } return; } current = current.next; } } // 输出链表中的所有节点 public void display() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } } ``` 然后可以通过以下方式进行测试: ```java public class Main { public static void main(String[] args) { DoublyLinkedList list = new DoublyLinkedList(); list.insertAtHead(3); list.insertAtHead(2); list.insertAtHead(1); list.insertAtTail(4); list.insertAtTail(5); list.insertAtPosition(6, 3); list.display(); // 输出:1 2 6 3 4 5 list.deleteNode(3); list.display(); // 输出:1 2 6 4 5 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值