Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions chapter05/05-CircularLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@ function CircularLinkedList() {
index = 0;

if (position === 0){ //add on first position

node.next = current;

//update last element
while(current.next !== head){ //last element will be head instead of NULL
current = current.next;

if(!head){ // if no node in list
head = node;
node.next = head;
}else{
node.next = current;

//update last element
while(current.next !== head){ //last element will be head instead of NULL
current = current.next;
}

head = node;
current.next = head;
}

head = node;
current.next = head;


} else {
while (index++ < position){
Expand Down Expand Up @@ -180,4 +186,5 @@ function CircularLinkedList() {
this.print = function(){
console.log(this.toString());
};
}
}

21 changes: 12 additions & 9 deletions chapter05/05-CircularLinkedList2.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ let CircularLinkedList2 = (function () {

if (position === 0) { //add on first position

node.next = current;

//update last element
while (current.next !== this.getHead()) { //last element will be head instead of NULL
current = current.next;
}

head.set(this, node);
current.next = this.getHead();
if(!this.getHead()) { // if no node in list
head.set(this, node);
node.next = this.getHead();
} else {
node.next = current;
//update last element
while(current.next !== this.getHead()) { //last element will be head instead of NULL
current = current.next;
}
head.set(this, node);
current.next = this.getHead();
}

} else {
while (index++ < position) {
Expand Down