Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rolling-scopes-school/doubly-linked-list
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: Derranion/doubly-linked-list
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Feb 3, 2017

  1. Copy the full SHA
    afe9440 View commit details
Showing with 153 additions and 11 deletions.
  1. +153 −11 src/linked-list.js
164 changes: 153 additions & 11 deletions src/linked-list.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,169 @@
const Node = require('./node');

class LinkedList {
constructor() {}
constructor() {
this.length = 0;
this._head = null;
this._tail = null;
}

append(data) {}
append(data) {
var currentNode = new Node(data);

head() {}
if (this._head == null){
this._head = currentNode;
this._tail = currentNode;
this.length++;

tail() {}
return this;
}

at(index) {}
this._tail.next = currentNode;
currentNode.prev = this._tail;
this._tail = currentNode;
this.length++;

insertAt(index, data) {}
return this;
}

isEmpty() {}
head() {
if (this._head != null){
return this._head.data
} else {
return null;
}
}

clear() {}
tail() {
if (this._tail != null){
return this._tail.data
} else {
return null;
}
}

deleteAt(index) {}
nodeAt(index){
if (index < 0 || index > this.length){
return null;
}

reverse() {}
var currentNode, i;
if (index > this.length / 2){
currentNode = this._tail;
i = this.length - 1;
while (i-- != index ){
currentNode = currentNode.prev;
}

indexOf(data) {}
} else {
currentNode = this._head;
i=0;
while (i++ != index){
currentNode = currentNode.next;
}
}
return currentNode;
}

at(index) {
return this.nodeAt(index).data;
}

insertAt(index, data) {
if (index < 0){
return null;
}
if (index >= this.length){
return this.append(data);
}

var currentNode = this.nodeAt(index);
var insertedNode = new Node(data, currentNode.prev, currentNode);
currentNode.prev = insertedNode;

if (index == 0){
this._head = insertedNode;
} else {
insertedNode.prev.next = insertedNode;
}
this.length++;

return this;
}

isEmpty() {
if (this.length == 0){
return true;
}
return false;
}

clear() {

this.length = 0;
this._head = null;
this._tail = null;

return this;
}

deleteAt(index) {
var nodeToDelete = this.nodeAt(index);

if (index < 0 || index > this.length){
return null;
}
if(this.length == 1){
this.clear();
}

if(nodeToDelete.next != null){
nodeToDelete.next.prev = nodeToDelete.prev;
}
if(nodeToDelete.prev != null){
nodeToDelete.prev.next = nodeToDelete.next;
}
this.length--;

return this;
}

reverse() {
if (this.length==1){
return this;
}
var easyData = this._head,
score = 0,
onePunch = [];

while(easyData != null){
onePunch.push(easyData.data);
easyData = easyData.next;
}

easyData = this._tail;

while(easyData != null){
easyData.data = onePunch[score++];
easyData = easyData.prev;
}
return this;
}

indexOf(data) {
var index = 0;
var currentNode = this._head;

while(index++ < this.length){
if(currentNode.data == data){
return index -1;
} else {
currentNode = currentNode.next;
}
}

return -1;
}
}

module.exports = LinkedList;