gpt4 book ai didi

java - 排序链表的基于数组的实现

转载 作者:搜寻专家 更新时间:2023-11-01 03:40:48 24 4
gpt4 key购买 nike

我需要编写一个 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;
}
}

最佳答案

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

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

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;
}
}

关于java - 排序链表的基于数组的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14926738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com