Skip to content

Commit

Permalink
Update linked-list.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ALX51 authored Sep 27, 2017
1 parent d8d967e commit ace33fa
Showing 1 changed file with 99 additions and 47 deletions.
146 changes: 99 additions & 47 deletions src/linked-list.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,126 @@
const Node = require('./node');
//не понял условие продерки метода append ( _head, _tail)

class LinkedList extends Node {
class LinkedList {
constructor() {
super()
this.arr = [];
this.length = this.arr.length;
this.list = [];
this.length = 0;
this._head = null;
this._tail = null;
}

append(data) {
this.arr.push(data);
this.length = this.arr.length;
return this;

append(data) {
if (this.length == 0) {
this.list[0] = new Node(data);
this.length = 1;
this._tail = this.list[0];
this._head = this.list[0];
} else {
this.list[this.length] = new Node(data, this.list[this.length -1].data);
this.list[this.length -1].next = this.list[this.length].data;
this.length = this.list.length;
this._tail = this.list[this.length - 1];
}
return this;
}

head() {
if (this.arr[0]) {
return this.arr[0];
} else {
return null;
}
head() {
if (this.length == 0) {
return null;
}
return this._head.data;
}

tail() {
if (this.arr[this.arr.length - 1]) {
return this.arr[this.arr.length - 1];
} else {
return null;
}
tail() {
if (this.length == 0) {
return null;
}
return this._tail.data;
}

at(index) {
return this.arr[index];
at(index) {
return this.list[index].data;
}

insertAt(index, data) {
this.arr[index] = data;
this.length = this.arr.length;
return this;
if (this.length == 0) {
this.append(data);
} else if ((this.length - 1 ) == index){
this.list[index].data = data;
this.list[index - 1].next = data;
} else {
this.list[index].data = data;
if ((this.list[index - 1].next) != null) {
this.list[index - 1].next = data;
}
if ((this.list[index + 1].prev) != null) {
this.list[index + 1].prev = data;
}
}
}

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

clear() {
this.length = 0;
this.arr.length = 0;
return this;
this.list = [];
this.length = 0;
this._head = null;
this._tail = null;
return this;
}

deleteAt(index) {
this.arr.splice(index, 1);
this.length = this.arr.length;
return this;
deleteAt(index) {
if ((this.length == 1) && (index == 0)) {
this.clear()
} else {
if (index == 0) {
this.list[index + 1].prev = null;
this.list.splice(index, 1);
this.length -= 1;
return this
}
if (index == this.list.length) {
this.list[index - 1].next = null;
this.list.splice(index, 1);
this.length -= 1;
return this
}
this.list[index - 1].next = this.list[index + 1].data;
this.list[index + 1].prev = this.list[index - 1].data;
this.list.splice(index, 1);
this.length -= 1;
}
return this
}

reverse() {
Array.prototype.reverse.call(this.arr);
return this;
reverse() {
if (this.length > 1) {
this.list[0].next = null;
this.list[0].prev = this.list[1].data;
this.list[this.length - 1].next = this.list[this.length - 2].data
this.list[this.length - 1].prev = null;
this._tail = this.list[0];
this._head = this.list[this.length - 1];
for (var i = 1; i < (this.list.length - 1); i++) {
this.list[i].next = this.list[i - 1].data;
this.list[i].prev = this.list[i + 1].data;
}
this.list.reverse();
}
return this
}

indexOf(data) {
return this.arr.indexOf(data);
indexOf(data) {
for (var i = 0; i < this.list.length; i++) {
if ( this.list[i].data == data ) {
return i;
}
}
return -1;
}
}
}

module.exports = LinkedList;

0 comments on commit ace33fa

Please sign in to comment.