Moving a linked Javascript 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 = null;
    c = LList;

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

    console.log( ar );
}

This procedure displays the data in the array in reverse order.

The problem is that the loop is not getting data : 4.

How do I rewrite it to display all the data?

+3
source share
6 answers
for (var c = LList; c; c = c.next) {
    // do something with c.data
}
+6
source

Think about what would happen if you had only one item. Do you still want to add data to the array correctly? This means that you want to execute the loop body at least once. In this case, you should do...whileloop :

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

.

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

, , . , :

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

reversePrint(someArray);

: http://jsbin.com/ataji4

+2

I actually implemented one example for linked lists in javascript, which is more accessible for display:

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();
+1
source
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);
}
0
source

A simple implementation LinkedListalong with a workaround can be done as follows 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();
	
})()
Run code
0
source

All Articles