java 双向链表排序及代码示例

java双向链表是一种物理存储单元上非线性的数据结构,它可以动态地进行数据插入和删除操作。它可以用来存储有序的元素集合,在某些特定的应用中,它的性能要优于数组和单链表。

在双向链表中,每个节点有两个链接:一个指向前一个节点(前驱),另一个指向下一个节点(后继)。

在Java中,双向链表可以通过定义一个节点类来实现,每个节点包含三个部分:数据域和两个指针域,一个指向前一个节点,另一个指向后一个节点。

package test;

/**
 * Java双向链表排序
 * 自定义双向Java双向链表类排序
 * @author Administrator
 *
 */
public class DoublyLinkedList {

    public static void main(String[] args) {
        //初始化双向Java双向链表类实例,并设置值
        DoublyLinkedList list = new DoublyLinkedList();
        list.insert(4);
        list.insert(2);
        list.insert(3);
        list.insert(1);
        list.insert(5);

        System.out.println("排序前:");
        list.print();

        list.sort();//双向链表类排序

        System.out.println("双向链表排序后:");
        list.print();

    }

    /**
     * 双向链表类排序
     */
    public void sort() {
        Node current = head;
        while (current.next != null) {
            Node nextNode = current.next;
            int nextData = nextNode.data;

            Node prevNode = current;
            while (prevNode != null && prevNode.data > nextData) {
                prevNode.next.data = prevNode.data;
                prevNode = prevNode.prev;
            }

            if (prevNode == null) {
                head.data = nextData;
            } else {
                prevNode.next.data = nextData;
            }

            current = nextNode;
        }
    }

    Node head;
    Node tail;

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

    // 在链表末尾插入一个节点
    public void insert(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.prev = tail;
            tail.next = newNode;
            tail = newNode;
        }
    }

    // 从链表中删除一个节点
    public void delete(int data) {
        Node current = head;
        while (current != null) {
            if (current.data == data) {
                if (current == head) {
                    head = current.next;
                }
                if (current == tail) {
                    tail = current.prev;
                }
                if (current.prev != null) {
                    current.prev.next = current.next;
                }
                if (current.next != null) {
                    current.next.prev = current.prev;
                }
                return;
            }
            current = current.next;
        }
    }

    // 遍历链表并打印节点的值
    public void print() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();//打印换行
    }

}

/**
 * 内部类
 * @author Administrator
 *
 */
class Node {
    int data;
    Node prev;
    Node next;
    
    public Node(int data) {
        this.data = data;
        this.prev = null;
        this.next = null;
    }
}

  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值