Java 链接圆形双链接结构中的节点
我已经在这个循环双链接结构上工作了一个星期了,我一直在使用这个add方法。我无法使节点正确链接,因为当我运行程序打印列表时,它不是我所期望的。下面我附加了我的DNode(只是一个普通的节点类)代码、ListCDLS(循环双链接结构)代码以及我收到的意外输出。我在add方法中尝试了许多不同的算法,但都没有用。唯一正确工作的是第一个if语句块Java 链接圆形双链接结构中的节点,java,data-structures,doubly-linked-list,Java,Data Structures,Doubly Linked List,我已经在这个循环双链接结构上工作了一个星期了,我一直在使用这个add方法。我无法使节点正确链接,因为当我运行程序打印列表时,它不是我所期望的。下面我附加了我的DNode(只是一个普通的节点类)代码、ListCDLS(循环双链接结构)代码以及我收到的意外输出。我在add方法中尝试了许多不同的算法,但都没有用。唯一正确工作的是第一个if语句块 public class DNode { private Object item; private DNode next; priv
public class DNode {
private Object item;
private DNode next;
private DNode back;
public DNode(Object item) {
this.item = item;
next = this;
back = this;
}
public DNode(Object item, DNode nextNode, DNode backNode) {
this.item = item;
this.next = nextNode;
this.back = backNode;
}
\\omitted getters and setters for all data fields
公共类ListCDLS实现ListInterface{
私有DNode尾;
私有内部网络;
公共列表CDLS(){
tail=null;
}
@凌驾
公共布尔值为空(){
if(this.size()==0){
返回true;
}
否则{
返回false;
}
}
@凌驾
公共整数大小(){
返回numItems;
}
public void add(int索引,对象项)引发ListIndexOutOfBoundsException{
if(isValidIndex(索引)){
if(this.isEmpty()){
DNode头=新的DNode(项目);
尾=头;
}
//这就是我正在努力解决的问题
如果(index==0&&!this.isEmpty()){
DNode nextIndex=tail.getNext();
DNode newHead=新DNode(项目、下一个索引、尾部);
尾。下一个(新头);
尾缩位(find(numItems-2));
nextIndex.倒退(新头);
setNext(查找(索引+2));
}
否则{
//对于在索引1、2、3、4、5等处添加的情况,代码我还没有开始。
}
numItems++;
}
否则{
抛出新的ListIndexOutOfBoundsException(“索引超出范围!”);
}
}
私有DNode查找(int索引){
D极电流;
int-possibleIndicies=numItems-1;
如果((可能指标/2)<指数){
current=tail.getNext();
对于(int i=0;i索引;i--){
current=current.getBack();
}
}
回流;
}
因此,我要做的输入是:
项目:6,索引:0
项目:5,索引:0
项目:4,索引:0
项目:3,索引:0
项目:2,索引:0
项目:1,索引:0
项目:0,索引:0
因此,预期产出为:
0,1,2,3,4,5,6
尽管如此,我收到:
6,0,1,2,4,5,6
任何帮助都将不胜感激。我不知道该怎么做,因为我觉得我已经用尽了所有的可能性,尽管我知道我的假设是错误的。这有帮助吗?如果没有,您是否尝试过搜索术语循环双链表java?在编写类似数据结构的操作时,是什么帮助了我uch是用来绘制代表指针的列表和箭头,并直观地执行多个操作。这比试图想象你头脑中的一切或通过查看代码更容易。如果你还没有这样做,请尝试一下。
public class ListCDLS implements ListInterface {
private DNode tail;
private int numItems;
public ListCDLS() {
tail = null;
}
@Override
public boolean isEmpty() {
if(this.size() == 0) {
return true;
}
else {
return false;
}
}
@Override
public int size() {
return numItems;
}
public void add(int index, Object item) throws ListIndexOutOfBoundsException {
if(isValidIndex(index)) {
if(this.isEmpty()) {
DNode head = new DNode(item);
tail = head;
}
//this is the one i am struggling with
else if(index == 0 && !this.isEmpty()) {
DNode nextIndex = tail.getNext();
DNode newHead = new DNode(item, nextIndex, tail);
tail.setNext(newHead);
tail.setBack(find(numItems-2));
nextIndex.setBack(newHead);
nextIndex.setNext(find(index+2));
}
else {
//code I didn't start yet for the case of adding at index 1,2,3,4,5 etc.
}
numItems++;
}
else {
throw new ListIndexOutOfBoundsException("The index is out of bounds!");
}
}
private DNode find(int index) {
DNode current;
int possibleIndicies = numItems - 1;
if( (possibleIndicies/2) < index ) {
current = tail.getNext();
for(int i = 0; i < index; i++) {
current = current.getNext();
}
}
else {
current = tail;
for(int i = possibleIndicies; i > index; i--) {
current = current.getBack();
}
}
return current;
}