Javascript 列表迭代器中的无限循环

Javascript 列表迭代器中的无限循环,javascript,arrays,list,for-loop,listiterator,Javascript,Arrays,List,For Loop,Listiterator,我创建了一个列表迭代器,但当尝试向后遍历列表时,循环无限运行。我做错了什么 function List() { this.listSize=0; this.pos=0; this.dataStore =[]; this.append = append; this.currPos = currPos; this.end = end; this.front = front; this.length = length;

我创建了一个列表迭代器,但当尝试向后遍历列表时,循环无限运行。我做错了什么

    function List() {
    this.listSize=0;
    this.pos=0;
    this.dataStore =[];
    this.append = append;
    this.currPos = currPos;
    this.end = end; 
    this.front = front;
    this.length = length;
    this.moveTo = moveTo;
    this.next = next;
    this.prev = prev;
    }

    function append(element) {this.dataStore[this.listSize++]=element;}
    function currPos() {return this.pos;}
    function end() {this.pos = this.listSize-1;}
    function front() {this.pos =0;}
    function length() {return this.listSize;}
    function moveTo(position) {this.pos = position;}
    function prev() {if(this.pos > 0) --this.pos;}
    function next() {if(this.pos < this.listSize) ++this.pos;}


    var names = new List();
    names.append("A"); names.append("B"); names.append("C");
    for(names.end(); names.currPos() >= 0; names.prev()) {console.log(names.getElement());}
函数列表(){
这个.listSize=0;
这是pos=0;
this.dataStore=[];
this.append=append;
this.currPos=currPos;
this.end=end;
this.front=front;
这个长度=长度;
this.moveTo=moveTo;
this.next=next;
this.prev=prev;
}
函数append(element){this.dataStore[this.listSize++]=element;}
函数currPos(){返回this.pos;}
函数end(){this.pos=this.listSize-1;}
函数front(){this.pos=0;}
函数长度(){返回this.listSize;}
函数moveTo(position){this.pos=position;}
函数prev(){if(this.pos>0)--this.pos;}
函数next(){if(this.pos=0;names.prev()){console.log(names.getElement());}

您的循环仅在当前列表位置小于零时终止,但您的
.prev()
函数不允许这种情况发生

要修好它吗?好吧,这是一个意见问题,但是如果您在实现list类时遇到麻烦,那么您最好创建一个本机的
。forEach
函数:

function forEach(callback) {
  for (var i = 0; i < this.listSize; ++i)
    callback(this.dataStore[i], i);
}

循环仅在当前列表位置小于零时终止,但是
.prev()
函数不允许这种情况发生

要修好它吗?好吧,这是一个意见问题,但是如果您在实现list类时遇到麻烦,那么您最好创建一个本机的
。forEach
函数:

function forEach(callback) {
  for (var i = 0; i < this.listSize; ++i)
    callback(this.dataStore[i], i);
}

您需要将for循环更改为:

for(names.end(); names.currPos() > 0; names.prev())

您需要将for循环更改为:

for(names.end(); names.currPos() > 0; names.prev())

在尝试实现《数据结构和算法》一书中的列表ADT时,我遇到了类似的问题,并发现作者在以后的版本中重新编写了该部分,如下所示:

module.exports = List;

function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = [];
    this.clear = clear;
    this.find = find;
    this.toString = toString;
    this.insert = insert;
    this.append = append;
    this.remove = remove;
    this.front = front;
    this.end = end;
    this.prev = prev;
    this.next = next;
    this.length = length;
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
    this.length = length;
    this.contains = contains;
    this.hasNext = hasNext;
    this.hasPrevious = hasPrevious;
    this.insertIf = insertIf;
}

function append(element) {
    this.dataStore[this.listSize++] = element;
}

function find(element) {
    for (var i = 0; i < this.dataStore.length; ++i) {
        if (this.dataStore[i] === element) {
            return i;
        }
    }
    return -1;
}

function remove(element) {
    var foundAt = this.find(element);
    if (foundAt > -1) {
        this.dataStore.splice(foundAt, 1);
        --this.listSize;
        return true;
    }
    return false;
}

function length() {
    return this.listSize;
}

function toString() {
    return this.dataStore;
}

function insert(element, after){
    var insertPos = this.find(after);
    if(insertPos > -1){
        this.dataStore.splice(insertPos+1, 0, element);
        ++this.listSize;
        return true;
    }
    return false;
}

function clear() {
    delete this.dataStore;
    this.dataStore = [];
    this.listSize = this.pos = 0;
}

function contains(element) {
    for (var i = 0; i < this.dataStore.length; ++i) {
        if(this.dataStore[i] === element) {
            return true;
        }
    }
    return false;
}

function front() {
    this.pos = 0;
}

function end() {
    this.pos = this.listSize-1;
}

function prev() {
    return this.dataStore[--this.pos];
}

function next(){
    return this.dataStore[this.pos++];
}

function hasNext(){
    if (this.pos > this.listSize -1) {
        return false;
    } else {
        return true;
    }
}

function hasPrevious() {
    if(this.pos <= 0) {
        return false;
    } else {
        return true;
    }
}

function currPos() {
    return this.pos;
}

function moveTo(position) {
    this.pos = position;
}

function getElement() {
    return this.dataStore[this.pos];
}

function insertIf(element) {
    var greaterThan = true;
    for(this.front(); this.hasNext(); ){
        if(this.next() > element) {
            greaterThan = false;
            break;
        }
    }
    console.log(element);
    if(greaterThan){
        this.append(element);
        return true;
    } else {
        return false;
    }
}
for (list.front(); list.hasNext();) {
    var listItem = list.next();
    if(listItem instanceof Customer) {
        console.log(listItem.name + ", " + listItem.movie);
    } else {
        console.log(listItem);
    }
}

这已被证明是一种更可靠的实现,因此您可能需要考虑切换到这个。 我在尝试实现《数据结构和算法》一书中的列表ADT时遇到了类似的问题,我发现作者在以后的版本中重新编写了该部分,如下所示:

module.exports = List;

function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = [];
    this.clear = clear;
    this.find = find;
    this.toString = toString;
    this.insert = insert;
    this.append = append;
    this.remove = remove;
    this.front = front;
    this.end = end;
    this.prev = prev;
    this.next = next;
    this.length = length;
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
    this.length = length;
    this.contains = contains;
    this.hasNext = hasNext;
    this.hasPrevious = hasPrevious;
    this.insertIf = insertIf;
}

function append(element) {
    this.dataStore[this.listSize++] = element;
}

function find(element) {
    for (var i = 0; i < this.dataStore.length; ++i) {
        if (this.dataStore[i] === element) {
            return i;
        }
    }
    return -1;
}

function remove(element) {
    var foundAt = this.find(element);
    if (foundAt > -1) {
        this.dataStore.splice(foundAt, 1);
        --this.listSize;
        return true;
    }
    return false;
}

function length() {
    return this.listSize;
}

function toString() {
    return this.dataStore;
}

function insert(element, after){
    var insertPos = this.find(after);
    if(insertPos > -1){
        this.dataStore.splice(insertPos+1, 0, element);
        ++this.listSize;
        return true;
    }
    return false;
}

function clear() {
    delete this.dataStore;
    this.dataStore = [];
    this.listSize = this.pos = 0;
}

function contains(element) {
    for (var i = 0; i < this.dataStore.length; ++i) {
        if(this.dataStore[i] === element) {
            return true;
        }
    }
    return false;
}

function front() {
    this.pos = 0;
}

function end() {
    this.pos = this.listSize-1;
}

function prev() {
    return this.dataStore[--this.pos];
}

function next(){
    return this.dataStore[this.pos++];
}

function hasNext(){
    if (this.pos > this.listSize -1) {
        return false;
    } else {
        return true;
    }
}

function hasPrevious() {
    if(this.pos <= 0) {
        return false;
    } else {
        return true;
    }
}

function currPos() {
    return this.pos;
}

function moveTo(position) {
    this.pos = position;
}

function getElement() {
    return this.dataStore[this.pos];
}

function insertIf(element) {
    var greaterThan = true;
    for(this.front(); this.hasNext(); ){
        if(this.next() > element) {
            greaterThan = false;
            break;
        }
    }
    console.log(element);
    if(greaterThan){
        this.append(element);
        return true;
    } else {
        return false;
    }
}
for (list.front(); list.hasNext();) {
    var listItem = list.next();
    if(listItem instanceof Customer) {
        console.log(listItem.name + ", " + listItem.movie);
    } else {
        console.log(listItem);
    }
}

这已被证明是一种更可靠的实现,因此您可能需要考虑切换到这个。 我不知道这是否是问题所在,但不要自己保持列表大小。数组将保留其自己的

.length
属性。我不知道这是否是问题所在,但不要自己维护列表大小。数组将保留其自己的
.length
属性。但这种方式不显示第一个项。但这种方式不显示第一个项。深入的解决方案。谢谢,有见地的解决方案。非常感谢。