基于数组的已排序链表的实现

时间:2013-02-17 22:17:13

标签: java

我需要编写一个需要基于数组并进行排序的java链表。因此,数组包含具有2个字段的节点:数据和列表中下一个元素的索引。列表的最后一个元素需要索引为-1。

有人可以帮助如何将元素添加到此类列表中。这是我到目前为止编写的代码,但似乎不正确,因为我可以添加元素,但索引不正确。

package listpackage;

import java.io.IOException;

public class ArrayLL {
    private int MAX_CAP = 100;
    private ANode[] list;
    private int size;

    public ArrayLL(){
        list = new ANode[MAX_CAP];
        list[list.length-1] = new ANode(null, -1);
        for(int i = 0; i < list.length-1; i++){
            list[i] = new ANode(null, i+1);
        }
        size = 0;
    }

    public void addElem(String s) throws IOException{
        if(this.getSize() == 0){
            ANode a = new ANode(s, -1);
            list[0] = a;
        }else if(size == MAX_CAP + 1){
            throw new IOException("List is full");
        }else{
            int index = 0;
            for(int i=0; i < list.length; i++){
                if(list[i].getData() == null){
                    index = i;
                    break;
                }
            }
            ANode b = new ANode(s);
            list[index] = b;
            if(this.getSize()==1){
                if (list[index].getData().compareTo(list[0].getData()) < 0){
                    list[index].setLink(0);
                    list[0].setLink(-1);
                }else{
                    list[index].setLink(-1);
                    list[0].setLink(index);
                }
            }else{
                int i = 0;
                while(list[i].getData() != null){
                    if(list[index].getData().compareTo(list[i].getData()) < 0){
                        list[index].setLink(i);
                        if(i>0)
                            list[i-1].setLink(index);
                    }else{
                        i++;
                    }
                }
            }
        }
        size++;
    }

    public ANode[] getList(){
        return list;
    }

    public int getSize(){
        return size;
    }

}




class ANode{
    private String data;
    private int link;

    public ANode(String d){
        data = d;
        link = -1;
    }

    public ANode(String d, int l){
        data =  d;
        link = l;
    }

    public String getData(){
        return data;
    }

    public int getLink(){
        return link;
    }

    public void setLink(int l){
        link = l;
    }
}

2 个答案:

答案 0 :(得分:1)

解决这个棘手的程序很有趣......: - )...我很喜欢......这是工作解决方案......我使用各种场景进行测试......

为了确保我不会改变你的大部分代码...我没有优化代码......在很多地方代码可以更简单和可读...

import java.io.IOException;

public class ArrayLL {

    public static void main(String[] args) throws IOException {
        ArrayLL myList  = new ArrayLL();
        myList.addElem("c");
        myList.addElem("b");
        myList.addElem("a");
        myList.addElem("d");
        int i = myList.startOfListIndex;
        while(myList.list[i].getLink()!=-1)
        {
            System.out.println(myList.list[i].getData());
            i = myList.list[i].getLink();
        }
        System.out.println(myList.list[i].getData());
    }

    private int MAX_CAP = 100;
    private ANode[] list;
    private int size;
    private int startOfListIndex = 0;

    public ArrayLL() {
        list = new ANode[MAX_CAP];
        for (int i = 0; i < list.length; i++) {
            list[i] = new ANode(null);
        }
        size = 0;
    }

    public void addElem(String s) throws IOException {
        if (this.getSize() == 0) {
            ANode a = new ANode(s, -1);
            list[0] = a;
        } else if (size == MAX_CAP + 1) {
            throw new IOException("List is full");
        } else {
            int index = 0;
            for (int i = 0; i < list.length; i++) {
                if (list[i].getData() == null) {
                    index = i;
                    break;
                }
            }
            ANode b = new ANode(s);
            list[index] = b;
            if (this.getSize() == 1) {
                if (list[index].getData().compareTo(list[0].getData()) < 0) {
                    list[index].setLink(0);
                    list[0].setLink(-1);
                    startOfListIndex = index;
                } else {
                    list[index].setLink(-1);
                    list[0].setLink(index);
                }
            } else {
                int i = startOfListIndex;
                int prevIndex = -1;
                while (i!=-1 && list[i].getData() != null) {
                    if (list[index].getData().compareTo(list[i].getData()) < 0) {
                        list[index].setLink(i);
                        if(prevIndex!=-1)
                            list[prevIndex].setLink(index);
                        else
                            startOfListIndex = index;
                        break;
                    } else {
                        prevIndex = i;
                        i=list[i].getLink();
                    }
                }
                if(i==-1)
                {
                    list[prevIndex].setLink(index);
                }
            }
        }
        size++;
    }

    public ANode[] getList() {
        return list;
    }

    public int getSize() {
        return size;
    }

}

class ANode {
    private String data;
    private int link;

    public ANode(String d) {
        data = d;
        link = -1;
    }

    public ANode(String d, int l) {
        data = d;
        link = l;
    }

    public String getData() {
        return data;
    }

    public int getLink() {
        return link;
    }

    public void setLink(int l) {
        link = l;
    }
}

答案 1 :(得分:0)

这是我认为完整的解决方案,因为它适用于我所做的所有测试,但可能有一些我没有想到的东西。解决方案也包括removeElem()方法。

package listpackage;

import java.io.IOException;

public class ArrayLL {
private int MAX_CAP = 100;
private ANode[] list;
private int size;
private int startOfListIndex = 0;

public ArrayLL() {
    list = new ANode[MAX_CAP];
    list[list.length-1] = new ANode(null, -1);
    for(int i = 0; i < list.length-1; i++){
        list[i] = new ANode(null, i+1);
    }
    size = 0;
}

public void addElem(String s) throws IOException {
    if (this.getSize() == 0) {
        ANode a = new ANode(s, -1);
        list[0] = a;
    }else if (size == MAX_CAP + 1) {
        throw new IOException("List is full");
    }else{
        int index = 0;
        for (int i = 0; i < list.length; i++) {
            if (list[i].getData() == null) {
                index = i;
                break;
            }
        }
        ANode b = new ANode(s);
        list[index] = b;
        if (this.getSize() == 1) {
            if (list[index].getData().compareTo(list[0].getData()) < 0) {
                list[index].setLink(0);
                list[0].setLink(-1);
                startOfListIndex = index;
            } else {
                list[index].setLink(-1);
                list[0].setLink(index);
            }
        } else {
            int i = startOfListIndex;
            int prevIndex = -1;
            while (i!=-1 && list[i].getData() != null) {
                if (list[index].getData().compareTo(list[i].getData()) < 0) {
                    list[index].setLink(i);
                    if(prevIndex!=-1)
                        list[prevIndex].setLink(index);
                    else
                        startOfListIndex = index;
                    break;
                }else{
                    prevIndex = i;
                    i=list[i].getLink();
                }
            }
            if(i==-1)
            {
                list[prevIndex].setLink(index);
            }
        }
    }
    size++;
}

public void removeElem(String s) throws IOException {
    if (this.getSize() == 0) {
        throw new IOException("List is empty");
    }else{
        int firstEmpty = 0;
        for(int i = 0; i< list.length; i++){
            if(list[i].getData() == null){
                firstEmpty = i;
                break;
            }
        }
        int elemindex = 0;
        int prev = -1;
        int next=-1;
        for(int i = 0; i < list.length; i++){
            if(list[i].getData() != null && list[i].getData().compareTo(s) == 0){
                elemindex = i;
                next = list[i].getLink(); 
                for(int j = 0; j < list.length; j++){
                    if(list[j].getLink() == elemindex){
                        prev = j;
                        break;
                    }
                }
                list[elemindex].setDataNull();
                list[elemindex].setLink(firstEmpty);
                if(next != -1)
                    list[prev].setLink(next);
                else
                    list[prev].setLink(-1);
                size--;
            }else{
                elemindex++;
            }
        }
    }
}

public ANode[] getList() {
    return list;
}

public int getSize() {
    return size;
}

}

class ANode {
private String data;
private int link;

public ANode(String d) {
    data = d;
    link = -1;
}

public ANode(String d, int l) {
    data = d;
    link = l;
}

public String getData() {
    return data;
}
public void setDataNull(){
    this.data = null;
}

public int getLink() {
    return link;
}

public void setLink(int l) {
    link = l;
}
}