【代码】java双向链表

package test;

import java.util.ArrayList;
import java.util.List;

public class Linked {
    public static void main(String[] args) {
        MyLinked myLinked = new MyLinked();


        myLinked.add(10);
        myLinked.add(20);
        myLinked.add(30);
        myLinked.add(40);
        myLinked.add(50);
        myLinked.add(60);
        myLinked.add(70);
        myLinked.add(80);
        myLinked.add(90);


        myLinked.delete(3);
        myLinked.println();

//        System.out.println(myLinked.getfist());
//        myLinked.half();

//        myLinked.reciprocal(2);

//        System.out.println(myLinked.size);

//        myLinked.delete(3);
//        myLinked.println();
//        MyLinked remove = myLinked.remove(3);
//        remove.println();
    }
}


//双向链表
class MyLinked {

    private Node first;
    private Node curr;
    public int size;

    

    /**
     * 节点中存储三个数据
     * prev是上一个节点的地址
     * next是下一个节点的地址
     * data是数据
     */
    class Node {
        private Node prev;
        private Node next;
        private int data;

        public Node() {

        }

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

    }


    //添加方法
    public void add(int data) {
        Node node = new Node(data);

        //首先判断第一个节点是否有值
        if (first == null) {
            first = node;
            curr = node;
        } else {
            //如果第一个节点有值的话
            //需要先将当前节点的next记录node的地址
            //再将node的prev记录当前节点的地址
            //再将当前节点替换成node
            curr.next = node;
            node.prev = curr;
            curr = node;
        }
        size++;
    }


    public void println() {
        Node node = first;
        List list = new ArrayList();

//        while (node != null) {
//            int value = node.data;
//            list.add(value);
//            node = node.prev;
//        }

        while (node != null) {
            int value = node.data;
            list.add(value);
            node = node.next;
        }

        System.out.println(list);

    }


    //取中间值
    public void half() {
        try {
            //中间值的话需要从第一个节点和最后一个节点往中间查找
            Node node = first;
            Node node1 = curr;
            //定义个依循环,当这两个节点相等则表示去到了中间节点
            while (node != node1) {
                //如果没有相等
                //就需要将头节点开始的下一个节点赋值给node
                //将尾节点的上一个节点赋值给node1
                node = node.next;
                node1 = node1.prev;
            }

            System.out.println(node.data);
        } catch (Exception e) {
            System.out.println("长度是偶数,无中间值");
        }
    }

    public int getfist() {
        return first.data;
    }


    //查找倒数第几个数据的话可以使用节点的上一个节点查找
    public void reciprocal(int K) {
        Node node = curr;
        int value = 0;
        for (int i = 0; i < K; i++) {
            value = node.data;
            node = node.prev;
        }

        System.out.println(value);
    }


//    public MyLinked remove(int index) {
//
//        MyLinked myLinked = new MyLinked();
//
//        Node node = first;
//
//        for (int i = 0; i < this.size; i++) {
//            int value = node.data;
//            node = node.next;
//            if ((i + 1) == index) {
//                continue;
//            }
//            myLinked.add(value);
//
//
//        }
//
//        return myLinked;
//    }


    //删除节点
    public void delete(int value) {
        /**
         * 删除节点的思路:
         * 需要一直查找到当前节点
         * 让后将当前节点的上一个节点的下一个节点修改成当前节点的下一个节点
         * 再将当前节点的下一个节点的上一个节点修改成当前节点的上一个节点
         */
        Node node = first;
        for (int i = 0; i < value - 1; i++) {
            node = node.next;
        }
        node.prev.next = node.next;
        node.next.prev = node.prev;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值