-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsLinkedList.js
executable file
·155 lines (134 loc) · 3.14 KB
/
dsLinkedList.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
152
153
154
155
var node = function(value, next, prev) {
this.data = value,
this.next = next,
this.prev = prev
};
var LinkedList = function() {
this.head = null;
this.tail = null;
this.size = 0;
};
var utility = {
indexOutOfBound: function(index, size) {
if (index < 0 && index > size + 1) {
console.log("Index given is out of bounds");
return true;
}
return false;
},
goToIndexNode: function(head, index) {
if (index === 0) {
return head;
};
var current = head;
for (var i = 0; i < index + 1; i++) {
current = current.next
};
return current;
}
};
// Adds a node to Linked List
LinkedList.prototype.add = function(val) {
var current;
if (this.head === null) {
this.head = new node(val, null, null);
this.tail = this.head;
} else {
current = this.tail;
current.next = new node(val, null, current);
this.tail = current.next;
};
this.size++;
};
// Removes node from Linked List
LinkedList.prototype.remove = function(index){
if (utility.indexOutOfBound(index,this.size)) {
return null;
}
var current;
var prevNode;
var nextNode;
if (index === this.size - 1) {
current = this.tail;
prevNode = current.prev;
this.tail = prevNode;
current = null;
prevNode.next = null;
this.size--;
return;
} else if(index === 0) {
current = this.head;
nextNode = current.next;
this.head = nextNode;
current = null;
nextNode.prev = null;
this.size--;
return;
} else {
current = utility.goToIndexNode(this.head,index);
prevNode = current.prev;
nextNode = current.next;
current = null;
prevNode.next = nextNode;
nextNode.prev = prevNode;
this.size--;
return;
};
};
// Get value at index
LinkedList.prototype.getValue= function(index) {
if (utility.indexOutOfBound(index,this.size)) {
return null;
}
var current = utility.goToIndexNode(this.head,index)
return current.data;
};
/** Get all values and returns an object
* that has key/value pairs
* O(n) runtime
*/
LinkedList.prototype.getAllValues = function() {
if (this.head === null) {
return "List is empty";
}
var result = {};
var current = this.head;
for (var i = 0; i < this.size; i++) {
result[i] = current.data;
current = current.next;
}
return result;
};
/** Reverse Linked List
* O(n/2) runtime
*/
LinkedList.prototype.reverse = function() {
if (this.head === null) {
return "No values to reverse";
}
if (this.size === 1) {
return;
}
var nodeOne = this.head;
var nodeTwo = this.tail;
var helperNodeData = nodeOne.data;
nodeOne.data = nodeTwo.data;
nodeTwo.data = helperNodeData;
this.head.data = nodeOne.data;
this.tail.data = nodeTwo.data;
if (this.size > 2) {
var endingIndex;
if (this.size % 2 === 1) {
endingIndex = this.size / 2 + 1.5;
} else {
endingIndex = this.size / 2 + 1;
}
for (var i = this.size -1; i > endingIndex - 1; i--) {
nodeOne = nodeOne.next;
nodeTwo = nodeTwo.prev;
helperNodeData = nodeOne.data;
nodeOne.data = nodeTwo.data;
nodeTwo.data = helperNodeData;
}
}
};↵