@violangreg/

building a Doubly LinkedList

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class Node {
  constructor(v){
    this.value = v;
    this.next = null;
    this.prev = null;
  }
}

class MyDoublyLinkedList {
  constructor(v){
    this.head = new Node(v);
    this.tail = this.head;
    if(v === undefined)
      this.length = 0;
    else
      this.length = 1;
  }

  prepend(v){
    if(this.head.value === undefined){
      this.head.value = v;
      this.head.next = this.tail;
      this.head.prev = null;
      this.length++;
    }
    else{
      const node = new Node(v);
      node.next = this.head;
      this.head = node;
      node.prev = null;
      this.length++;
    }
    return this;
  }

  append(v){
    if(this.tail.value === undefined){
      this.prepend(v);
    }
    else{
      const node = new Node(v);
      this.tail.next = node;
      node.prev = this.tail;
      node.next = null;
      this.tail = node; // new node becomes the tail
      this.length++;
    }
    return this;
  }

  insert(i, v){
    if(i === 0)
      return this.prepend(v);
    if(i >= this.length)
      return this.append(v);
    if(this.inputChecker(i) === undefined) // checks for less than 0
      return;

    const node = new Node(v);
    const prevNode = this.traverseToIndex(i-1);
    const currentNode = prevNode.next; // since prevNode is already assigned the line before, the currentNode is the next of it

    node.next = currentNode;
    currentNode.prev = node;
    prevNode.next = node;
    node.prev = prevNode;
    this.length++;
  }

  remove(i){
    if(this.inputChecker(i) === undefined)
      return;
    if(i === 0)
      return this.head = this.head.next;

    const prevNode = this.traverseToIndex(i-1);
    const unwantedNode = prevNode.next;
    const nextNode = unwantedNode.next;
    prevNode.next = nextNode;
    nextNode.prev = prevNode;
    this.length--;
  }

  traverseToIndex(i){
    if(this.inputChecker(i) === undefined)
      return;
    let currentNode = this.head;
    for(let k = 0; k < i; k++)
      currentNode = currentNode.next;
    return currentNode;
  }

  get(i){
    return this.traverseToIndex(i).value;
  }

  reverseList(){
    if(!this.head.next)
      return this.head;
    const temp = this.head;
    this.head = this.tail;
    this.tail = temp;

    let p = this.head; 
    while(p != null){
      const temp = p.next;
      p.next = p.prev;
      p.prev = temp;
      p = p.next;
    }

    return this.printList();
  }

  printList(){
    const array = [];
    let currentNode = this.head;
    while(currentNode !== null){
      array.push(currentNode.value);
      currentNode = currentNode.next;
    }
    return array;
  }

  inputChecker(i){
    try{
      if(i > this.length || i < 0) 
        throw "Index OutOfBounds";
    } catch(e) {
      e = new Error();
      return console.log(e.stack);
    }
    return this;
  }
}

const myDLL = new MyDoublyLinkedList();
myDLL.append(2);
myDLL.append(3);
myDLL.append(4);
myDLL.append(5);
console.log(myDLL.length);
myDLL.insert(1,6)
console.log(myDLL.printList());
console.log(myDLL.reverseList());





node v10.16.0