2

I was building linked list in javascript. I dont understand one part.

function Node(element) {
	this.element = element;
	this.next = null;
	this.previous = null;
}

function LList() {
	this.head = new Node("head");
	
	this.find = find;
	this.findLast = findLast;

	this.remove = remove;
	this.insert = insert;
	this.display = display;
	this.dispReverse = dispReverse;
		
}

function find(item) {
	var currNode = this.head;
	while(currNode.element != item) {
		currNode = currNode.next;
	}

	return currNode;
}


function display(list) {
	var currNode = this.head.next;
	while (currNode != null) {
		console.log(currNode.element);
		currNode = currNode.next;
	}
}


function insert(newElement, item) {
	var newNode = new Node(newElement);
	var current = this.find(item);
	newNode.next = current.next;
	newNode.previous = current;
	current.next = newNode;


	// Why I dont need this part?
    // Since new node got inserted, on my thoughts,
    // the next node of the current node should point the new node as a previous one
    // current.next.previous = newNode;
	
}

function remove(item) {
	var currNode = this.find(item);
	if (currNode.next != null) {
		currNode.previous.next = currNode.next;
		currNode.next.previous = currNode.previous;
		currNode.next = null;
		currNode.previous = null;
	}
}

function findLast() {
	var currNode = this.head;
	while (currNode.next != null) {
		currNode = currNode.next;
	}

	return currNode;
}

function dispReverse() {

	var currNode = this.head;
	currNode = this.findLast();

	while(currNode.previous != null) {
		console.log(currNode.element);
		currNode = currNode.previous;
	}
}

var cities = new LList(); 
cities.insert("Conway", "head"); 
cities.insert("Russellville", "Conway"); 
cities.insert("Carlisle", "Russellville"); 
cities.insert("Alma", "Carlisle"); 
cities.display();

cities.remove("Carlisle");
cities.display();
cities.dispReverse();


/*
Output should look like this: 

Conway
Russellville
Carlisle
Alma

Conway
Russellville
Alma

Alma
Russellville
Conway
*/

Problem is the insert function!
Let's say if I have A B C node already.
And I want to insert K after B.

Currently, the next and previous of B are C and A for each.
The previous element of C is B.


Once I put K after B,
A B K C
(1) the next element of K will be C
(2) the previous element of K will be B.
(3) The next element of B is K
(4) previous element of C is K.

On the code I wrote in Insert function, each line of the codes below should process the upper statements.
(1) newNode.next = current.next;
(2) newNode.previous = current;
(3) current.next = newNode;
(4) current.next.previous = newNode;

But when I run this whole code including (4), error has occurred.
I don understand why...
Without the (4) line of codes, it works.

Is there anyone who can help me understand this?

6
  • 1
    Do you want to tell us what the error is? Commented Jul 8, 2015 at 6:26
  • 1
    Did you try to debug via some console.log ? Can you provide us the exact error ? Sounds like a reference problem to me Commented Jul 8, 2015 at 6:26
  • @FelixKling Oh I think I didnt explain clearly. Inside the insert function there is comment like this. // current.next.previous = newNode; I think this is a proper code to be included in the insert function but when I put that inside the function and run it, error happens! Commented Jul 8, 2015 at 10:08
  • @stephane-ruhlmann As I mentioned up there, the code above works just fine. What I wonder is I think [current.next.previous = newNode;] should be included inside the insert function. Now I made it as a comment with '//'. So it didn't make an error. Commented Jul 8, 2015 at 10:10
  • Ok I get it, still an object reference issue to me, see answer below Commented Jul 8, 2015 at 10:14

2 Answers 2

2

You need to do step 4 before step 3:

current.next.previous = newNode
current.next = newNode

As it is, the reference of current.next (C) is being set to newNode (K) before you lookup the "old" current.next's previous property ( current.next.previous when points to B). Your reference to the current node is changed as soon as you assign it a new value. This is why current.next.previous is actually returning newNode.previous instead of the node reference you expect.

Sign up to request clarification or add additional context in comments.

3 Comments

THANKS!! It really helped me to understand the problem!
@SunyoungKim You are welcome. If you found the answer particularly useful, you could mark it as your accepted answer. Check out the help page on how to respond to answers, which also mentions accepting answers and discourages using comments to leave 'thank you' notes.
Oh it was such a huge newbie's mistake! Thanks again!
2

Your insert logic seems wrong in the last line :

current.next = newNode;

current.next.previous = newNode;

This actually means

newNode.previous=newNode;

Since you are setting the value of current.next to newNode in the 3rd statement

It should be :

newNode.next.previous = newNode.

1 Comment

hi! Thanks a lot for your help!. I understood pretty much. But I got this error! - Uncaught TypeError: Cannot set property 'previous' of null. I have no idea how to deal with this. Still I am so thankful about your well-organized answer! THANKS!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.