1

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

    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());}
4

3 回答 3

3

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

要解决这个问题?好吧,这是一个见仁见智的问题,但如果你要麻烦实现一个列表类,你不妨创建一个原生.forEach函数:

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

然后你可以这样做:

names.forEach(function(name) { console.log(name); });
于 2014-07-14T00:08:10.957 回答
1

我在尝试从“数据结构和算法”一书中实现列表 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);
    }
}

这已被证明是一个更可靠的实现,因此您可能需要考虑切换到此。

于 2015-05-10T21:56:23.697 回答
0

您需要将 for 循环更改为:

for(names.end(); names.currPos() > 0; names.prev())
于 2014-07-14T00:14:05.377 回答