开发者

Traversing a Javascript Linked list skips the last item

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

var ar = []; 

function reversePrint( LList )
{
    var c = nu开发者_开发问答ll;
    c = LList;

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

    console.log( ar );
}

This routine outputs data in array in reverse order.

The problem is : the loop doesn't get data : 4.

How do I rewrite it to output all of the data?


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


Think about what would happen if you only had one element. You would still want to add the data to the array right? That means you want to execute the loop body at least once. In this case you should a do...while loop:

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


I'll throw another one in.

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

Recursively, very, very small. But heavy on the stack, which I am not fond of:

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

reversePrint(someArray);

See them at work: http://jsbin.com/ataji4


Actually I`ve implemented one example for linked lists in javascript which is more displayable of what should llist be:

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


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


A simple implementation of LinkedList along with traversal can be done like this in JavaScript:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜