2011-06-16 55 views
3
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); 
} 

此例程以相反的顺序输出数组。遍历Javascript链接列表跳过最后一项

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

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

+2

我想你想一个do {} while循环,因为据推测,如果接下来的是空,你希望它中止循环之前仍然retreive价值? – NibblyPig 2011-06-16 13:52:40

回答

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

+1请注意,您需要在此循环中进行不移位。虽然 – cgp 2011-06-16 19:14:53

2

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

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

+1,这是最好最简洁的,但请注意,它不是“打印” – cgp 2011-06-16 13:58:58

+1

@altCognito:公平起见,OP的功能也不是“打印”;) – 2011-06-16 14:02:20

+0

大声笑,非常真实,非常真实,我真的想建议返回反向数组看起来更像是规范的“反向”实现。 (如,这是一件好事)无论如何,这是一个家庭作业问题。 (c){ar.unshift(c.data); – cgp 2011-06-16 18:29:34

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

+0

但递归如何在这里工作? console.log是否在下次调用reversePrint(ll.next)之前被执行?我以为console.log从来没有机会运行,因为reversePrint会不断调用它自己.. – DrStrangeLove 2011-06-18 00:22:51

1

其实I`ve实现的一个示例的链表在JavaScript这样更显示的应该LLIST什么是:

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(); 
0

一个简单的实现与穿越沿LinkedList可以这样用JavaScript来实现:

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