Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Linked List

  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts

    Linked List

    I am trying to implement a linked list in javascript. So far i have:
    Code:
    function linkedList(){
                    var llNode=function(value){
                        this.next=null;
                        this.previues=null;
                        this.value=value;
                    }
                    var that=this;
                    this.length=0;
                    this.head=null;
                    this.tail=null;
                    this.add=function(value){
                        if(length==0){
                            var tmp=new llNode(value)
                            that.head=tmp;
                            that.tail=tmp;
                        }
                        else{
                            var tmp=new llNode(value);
                            tmp.previues=that.tail;
                            that.tail.next=tmp;
                            that.tail=that.tail.next;
                        }
                        that.length++;
                    }
           }
    }
    When I use:
    Code:
    var ll=new linkedList();
    ll.add(1);
    ll.add(2);
    both the head and tail of ll point to the node whose value is to, and the next and previues pointer on that node are both null.
    take a point P, now call it Q.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    you need to define length, at least...

    you can also simply use elements: they have nextSibling, firstChild, parentNode, insertBefore, appendChild, removeChild, and all the other methods you want in this sort of thing.
    plus, you can use all sort of dom methods to search nodes, including css queries...
    Last edited by rnd me; 07-31-2011 at 08:30 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    rnd me
    He uses the length property already.

    gizmo1650
    The problem is here -- missing keyword:
    Code:
                        if(this.length==0){

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    gizmo1650

    I would implement the add method simpler:
    Code:
    NodeList.prototype.append = function(node)
    {
    	node.prev = this.tail;
    	this.tail = node;
    	if ( this.length == 0 ) {
    		this.head = node;
    	} else {
    		node.prev.next = node;
    	}
    	return this.length++;
    };

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by gizmo1650 View Post
    I am trying to implement a linked list in javascript. So far i have:
    Code:
    function linkedList(){
                    var llNode=function(value){
                        this.next=null;
                        this.previues=null;
                        this.value=value;
                    }
                    var that=this;
                    this.length=0;
                    this.head=null;
                    this.tail=null;
                    this.add=function(value){
                        if(length==0){
                            var tmp=new llNode(value)
                            that.head=tmp;
                            that.tail=tmp;
                        }
                        else{
                            var tmp=new llNode(value);
                            tmp.previues=that.tail;
                            that.tail.next=tmp;
                            that.tail=that.tail.next;
                        }
                        that.length++;
                    }
           }
    }
    When I use:
    Code:
    var ll=new linkedList();
    ll.add(1);
    ll.add(2);
    both the head and tail of ll point to the node whose value is to, and the next and previues pointer on that node are both null.
    Here is a quick off the cuff example
    of one way to implement an
    doulble linked list in javascript ....


    Code:
    <script>
    
    (function(){
    	var length = 0;
    	var prev;
    	var Node = function(a){
    		this.getLength = function(){return length};
    		this.prev = prev;
    		this.next;		
    		this.data = a;
    		this.addToTail = function(a){
    			length++;
    			return new Node(a)};
    		this.addBefore = function(a,b){
    			length++;
    			a.prev.next = new Node(b);
    			a.prev = a.prev.next;
    			a.prev.next = a;
    			return a.prev;
    			}
    		if(prev)prev.next = this;
    		prev = this;		
    		}
    	LInkedList = function(a){
    		length++;
    		return new Node(a);
    		}
    })()
    n = new LInkedList('myData');
    n1 = n.addToTail('myData2');
    n2 = n.addToTail('myData3');
    n3 = n1.addBefore(n1,"myData1")
    //alert(n.data);
    //alert(n1.prev.data);
    //alert(n1.data);
    //alert(n1.next.data);
    //alert(n.getLength())
    for(var i = n.getLength(); i--;){
    	alert(n.data);
    	n = n.next;
    	}
    
    </script>
    of course it needs an addAfter and an addToHead
    and others
    well the way it is you can only have
    one LinkedList cause of the vars length and prev
    Last edited by DaveyErwin; 08-01-2011 at 03:58 AM.

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by rnd me View Post
    you need to define length, at least...

    you can also simply use elements: they have nextSibling, firstChild, parentNode, insertBefore, appendChild, removeChild, and all the other methods you want in this sort of thing.
    plus, you can use all sort of dom methods to search nodes, including css queries...
    What if there is no dom ?
    Like maybe if you are not
    running in a browser ?

    < ^ ;
    Last edited by DaveyErwin; 08-01-2011 at 02:56 AM. Reason: forgot smilely face

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by DaveyErwin View Post
    What if there is no dom ?
    Like maybe if you are not
    running in a browser ?

    < ^ ;
    then you can still use a dom library. i use node.js and libxml-js provides those methods...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •