使用插入排序对Java中的双链表进行排序

时间:2019-03-16 15:27:14

标签: java doubly-linked-list insertion-sort

我正在尝试使用插入排序对双向链表进行排序。在排序之前,双向链表按以下顺序具有以下元素:4、1、7、10。其输出应为1、4、7、10(基本上使用insertSort方法对元素进行升序排序) )。

删除-删除节点并返回其值

insertAfter(Node n,val v)-在节点n之后插入一个值为v的新节点

我已经尝试进行研究,但没有找到任何解决方法。

有人可以帮助我吗?

import java.util.ArrayList;

public class DLinkedList {

    private class Node {
        private int value;
        private Node nextNode;
        private Node prevNode;

        public Node(int v) {
            value = v;
            nextNode = null;
            prevNode = null;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int v) {
            value = v;
        }

        public Node getNextNode() {
            return nextNode;
        }

        public void setNextNode(Node n) {
            nextNode = n;
        }

        public Node getPrevNode() {
            return prevNode;
        }

        public void setPrevNode(Node n) {
            prevNode = n;
        }

    }

    // Holds a reference to the head and tail of the list
    private Node headNode;
    private Node tailNode;

    public DLinkedList() {
        headNode = null;
        tailNode = null;
    }

    public void addAtHead(int o) {
        Node newNode = new Node(o); 
        newNode.setNextNode(headNode); 
        if (headNode != null)
            headNode.setPrevNode(newNode);
        headNode = newNode; 
        // special case for empty list
        if (tailNode == null)
            tailNode = newNode;
    }

    public void addAtTail(int o) {
        Node newNode = new Node(o);
        // this means that headNode == null too!
        if(tailNode == null){
            tailNode = newNode;
            headNode = newNode;
        }else{
            newNode.setPrevNode(tailNode);
            tailNode.setNextNode(newNode);
            tailNode = newNode;
        }
    }

    public int deleteAtHead() {
        // list is empty 
        if(headNode == null){
            headNode = null;
            tailNode = null;
            return -1;
        }
        // singleton: must update tailnode too
        if(headNode == tailNode){
            int res = headNode.getValue();
            headNode = null;
            tailNode = null;
            return res;
        }

        int res = headNode.getValue();
        headNode = headNode.getNextNode();
        headNode.setPrevNode(null);
        return res;
    }

    public int deleteAtTail() {
        // list is empty 
        if(tailNode == null){
            headNode = null;
            tailNode = null;
            return -1;
        }
        // singleton: must update tailnode too
        if(headNode == tailNode){
            int res = tailNode.getValue();
            headNode = null;
            tailNode = null;
            return res;
        }
        int res = tailNode.getValue();
        tailNode = tailNode.getPrevNode();
        tailNode.setNextNode(null);
        return res;
    }

    public int delete(Node n) {
        if (n == null)
            return -1;
        Node next = n.getNextNode();
        Node prev = n.getPrevNode();
        int val = n.getValue();
        if (prev != null)
            prev.setNextNode(next);
        if (next != null)
            next.setPrevNode(prev);
        // deleting at the end
        if (n == tailNode)
            tailNode = prev;
        // deleteing at beginning
        if (n == headNode)
            headNode = next;
        return val;
    }

    public void insertAfter(Node n, int val) {
        if (n == null) { // this is the headNode
            addAtHead(val);
            return;
        }
        Node next = n.getNextNode();
        Node newNode = new Node(val);
        newNode.setPrevNode(n);
        newNode.setNextNode(next);
        n.setNextNode(newNode);
        if (next == null) { // insert at tail
            tailNode = newNode;
        } else {
            next.setPrevNode(newNode);
        }
    }

    // computes the size of the list
    public int size() {
        if (headNode == null)
            return 0;
        Node n = headNode;
        int size = 0;
        while (n != null) {
            size++;
            n = n.getNextNode();
        }
        return size;
    }

    // Predicate to check if the linked list is sorted
    public boolean isSorted() {
        if (headNode == null || headNode.nextNode == null)
            return true;
        Node i = headNode.nextNode;
        while (i != null) {
            if (i.getValue() < i.getPrevNode().getValue())
                return false;
            i = i.nextNode;
        }
        return true;
    }

    // toString methods to override printing of object
    public String toString() {
        Node n = headNode;
        StringBuffer buf = new StringBuffer();
        while (n != null) {
            buf.append(n.getValue());
            buf.append(" ");
            n = n.getNextNode();
        }
        return buf.toString();
    }


    public static void main(String[] args) {
        DLinkedList d = new DLinkedList();
        d.addAtHead(4);
        d.addAtHead(1);
        d.addAtHead(7);
        d.addAtHead(10);
        System.out.println("Before sorting: " + d); // this will call the toString method
        d.insertionSort();
        System.out.println("After sorting: " + d);
    }
}

1 个答案:

答案 0 :(得分:1)

因此,如果要对列表进行排序,为什么要在末尾进行排序?我更喜欢以排序方式插入它们。因此,每次插入元素时,其都会以排序形式插入。 这是工作代码:

ckeditor.js:98 GET http://127.0.0.1:8000/lib/static/ckeditor/ckeditor/plugins/utils/plugin.js?t=I3I8 net::ERR_ABORTED 404 (Not Found)
ckeditor.js:255 Uncaught Error: [CKEDITOR.resourceManager.load] Resource name "utils" was not found at "http://127.0.0.1:8000/lib/static/ckeditor/ckeditor/plugins/utils/plugin.js?t=I3I8".
    at CKEDITOR.resourceManager.<anonymous> (ckeditor.js:255)
    at e (ckeditor.js:250)
    at Array.z (ckeditor.js:250)
    at y (ckeditor.js:250)
    at ckeditor.js:251