1

我正在处理一项任务,我必须对数组进行排序并将索引存储在我构建的双向链表 ADT 中。

Example:
{"A","B","C","A"} -> {0,3,1,2}

我目前的方法是将索引带入我的 ADT,然后在对数组进行排序时对 ADT 进行排序。据我所知,每次执行任一循环时,我都会对数组和双向链表执行完全相同的操作,但我的输出不匹配。

public static void main(String[] args ) {
    List A = new List();
    String[] inputArray = {"A","C","B","A"};
    int i,j;
    String key;
    //load indices into doubly-linked list
    for (int x = 0; x < inputArray.length; x++ ) {
        A.append(x);
    }
    //begin insertion sort
    for (j = 1; j < inputArray.length; j++) {
        System.out.println(A);
        System.out.println(Arrays.toString(inputArray));
        key = inputArray[j];
        i = j - 1;
        while (i >= 0) {
            if (key.compareTo(inputArray[i]) > 0) {
                break;
            }
            inputArray[i+1] = inputArray[i];
            //moveTo moves the cursor to <int>
            A.moveTo(i);
            //make sure we aren't trying to insert before first node
            if (i > 0) { A.insertBefore(i+1); }
            else { A.prepend(i+1); }
            //remove node at cursor
            A.delete();
            i--;
        }
        inputArray[i+1] = key;
        A.moveTo(i+1);
        if (i > 0) { A.insertBefore(i+1); }
        else { A.prepend(i+1); }
        A.delete();
    }
    System.out.println(A);
    System.out.println(Arrays.toString(inputArray));
}

上面的代码给出了以下输出:

运行:
0 1 2 3
[A, C, B, A]
1 0 2 3
[A, C, B, A]
1 1 2 3
[A, B, C, A]
0 2 3 3
[A, A, B, C]
构建成功(总时间:0 秒)

编辑:List.java

public class List {

    private class Node{
        //Fields
        int data;
        Node next, previous;
        //Constructor
        Node(int data) {
            this.data = data;
            next = null;
            previous = null;
        }
        public String toString() { return String.valueOf(data); }
    }

    //Fields
    private Node frontNode, backNode, cursorNode;
    private int totalSize, cursorPosition;

    //Constructor
    List() {
        frontNode = backNode = cursorNode = null;
        totalSize = 0;
        cursorPosition = -1;
    }
    //moveTo(int i): If 0<=i<=length()-1, moves the cursor to the element 
    // at index i, otherwise the cursor becomes undefined.
    void moveTo(int i) {
        if (i == 0) {
            cursorPosition = i;
            cursorNode = frontNode;
        }
        else if (i == length() - 1) {
            cursorPosition = i;
            cursorNode = backNode;
        }
        else if (i > 0 && i < length() - 1 ) {
            cursorNode = frontNode;
            cursorPosition = i;
            for (int x=0; x < i; x++) {
                cursorNode = cursorNode.next;
            }
        }
        else {
            cursorPosition = -1; 
        }
    }

//prepend(int data): Inserts new element before front element in this List.
    void prepend(int data) {
        Node node = new Node(data);
        if ( this.length() == 0 ) {
            frontNode = backNode = node;
        }
        else {
            frontNode.previous = node;
            node.next = frontNode;
            frontNode = node;
        }
        totalSize++;
        //cursorPosition might change?
    }
    //insertBefore(int data): Inserts new element before cursor element in this
    // List. Pre: length()>0, getIndex()>=0
    void insertBefore(int data) {
        Node node = new Node(data);
        if ( this.length() > 0 && this.getIndex() >= 0 ) {
            node.previous = cursorNode.previous;
            node.next = cursorNode;
            cursorNode.previous.next = node;
            cursorNode.previous = node;
            totalSize++;
        }
        else if ( this.length() <= 0 )
        {
           throw new RuntimeException
                    ("Error: insertBefore called on empty list");
        }
        else {
            throw new RuntimeException
                    ("Error: insertBefore called without cursor set");
        }
    }
4

1 回答 1

1

好,谢谢。我并不确切知道 insertBefore() 和 prepend() 打算做什么(我本可以猜到,但编程不应该是猜测应该做什么方法;查看文档要好得多)。

由于这是一项任务,我不会给你答案。但线索是这样的:在第一次通过循环后,A 的转储为您提供与开始时相同的索引,但重新排列。我认为这就是每次循环迭代后应该采用的方式。但在第二次通过循环后情况并非如此(1 出现两次,0 消失了)。想想看。当你调用 A.insertBefore() 来重新排列 A 的元素时,你应该告诉 insertBefore() 插入什么数据,你实际插入了什么?

于 2013-07-02T23:44:24.747 回答