遍历Javascript链接列表会跳过最后一项

时间:2011-06-16 13:49:41

标签: javascript linked-list

var someList = {
                   data : 1,
                   next : {
                              data : 2,
                                  next : {
                                             data : 3,
                                             next : {
                                                        data : 4,
                                                        next : null
                                                    }
                                         }
                          }
               };

var ar = []; 

function reversePrint( LList )
{
    var c = null;
    c = LList;

    while ( c.next != null )
    {
        ar.unshift( c.data );
        c = c.next;
    }

    console.log( ar );
}

此例程以相反的顺序输出数组中的数据。

问题是:循环没有得到data : 4

如何重写它以输出所有数据?

6 个答案:

答案 0 :(得分:6)

for (var c = LList; c; c = c.next) {
    // do something with c.data
}

答案 1 :(得分:2)

想想如果你只有一个元素会发生什么。您仍然希望将数据添加到数组中吗?这意味着你想至少执行一次循环体 。在这种情况下,您应该do...while loop

function reversePrint(c){
    var ar = [];
    do {
        ar.unshift(c.data);
    } while (c = c.next);
    console.log(ar) // print ;)
    return ar;
}

答案 2 :(得分:2)

我会再扔一个。

function reverse(ll) {
  var ar = [];
  while (ll) {
    ar.unshift(ll.data);
    ll = ll.next;
  }
  return ar; 
}

var newList = reverse(someList);
for(var x=0;x<newList.length;x++) {
  console.log(newList[x]);
}

OR

递归地,非常非常小。但在堆栈上很重,我不喜欢:

function reversePrint(ll) {
  if (ll.next) reversePrint(ll.next);
  console.log(ll.data);
}

reversePrint(someArray);

在工作中查看它们:http://jsbin.com/ataji4

答案 3 :(得分:1)

实际上我已经为javascript中的链表实现了一个例子,它更能显示应该列出的内容:

function Link(k, d) {
    this.obj = {
        'key': k,
        'data' : d,
        'next' : null 
        };
    return this.obj;
}
function List() {
    this.firstLink = new Link();
    this.insertFirst = function(key, data) {
        this.newLink = new Link(key, data);
        this.newLink.next = this.firstLink;
        this.firstLink = this.newLink;
        }
    this.getFirst = function() {
        return this.firstLink;
        }
    this.removeFirst=function() {
        var temp = this.firstLink;
        this.firstLink = this.firstLink.next;
        delete temp;
        }
    this.displayList=function() {
        this.current = this.firstLink;
        while ( this.current != null ) {
            console.log(this.current);
            this.current = this.current.next;
            }
        }

    }
var lst = new List();
lst.insertFirst(22, 'ilian');
lst.insertFirst(55, 'xoxo');
lst.insertFirst(77, 'fefe');

lst.displayList();

答案 4 :(得分:0)

var ar = []; 

function reversePrint(LList){
  var c = null;
  c = LList;
  while (c.next != null) {
    ar.unshift(c.data);
    c = c.next;
  }
  ar.unshift(c.data); //gets that last element
  c=c.next; //c now equals null

  console.log(ar);
}

答案 5 :(得分:0)

LinkedList的简单实现以及遍历可以在JavaScript中完成:

&#13;
&#13;
(function(){
	'use strict';
	var LinkedList = function(){
		this.head = null;
	}
	
	LinkedList.prototype.appendToTail = function(data){
		var node = {
			"data":data,
			"next":null
		};
		
		if(this.head == null){
			this.head = node;
		}else{
			var current = this.head;
			while( current.next != null){
				current = current.next;
			}
			current.next = node;
		}
	}
  
  LinkedList.prototype.traverseList = function(){
      var current = this.head;
      while(current != null){
          console.log(current.data);
          current = current.next;
      }
  
  }
	
  var linkedList = new LinkedList();
  linkedList.appendToTail(20);
  linkedList.appendToTail(30);
  linkedList.appendToTail(40);
     
  linkedList.traverseList();
	
})()
&#13;
&#13;
&#13;