-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubleLinkedList.js
More file actions
54 lines (53 loc) · 1.16 KB
/
Copy pathdoubleLinkedList.js
File metadata and controls
54 lines (53 loc) · 1.16 KB
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
// Implementation of double linked list
function doublyLinkedListNode(data)
{
this.data = data;
this.next = null;
this.prev = null;
}
function doublyLinkedList()
{
this.head = null;
this.tail = null;
this.size = 0 ;
}
doublyLinkedList.prototype.isEmpty = function()
{
return this.size == 0;
}
doublyLinkedList.prototype.addAtFront = function(value)
{
if(this.head == null)
{
this.head = new doublyLinkedListNode(value);
this.tail = this.head;
}
else
{
var temp = new doublyLinkedListNode(value);
temp.next = this.head;
this.head.prev = temp ;
this.head =temp;
}
this.size ++;
}
var dll1 = new doublyLinkedList();
dll1.addAtFront(10);
dll1.addAtFront(20);
dll1.addAtFront(30);
dll1.addAtFront(40);
console.log(dll1)
doublyLinkedList.prototype.findStartingHead = function(value)
{
var currentHead = this.head;
while(currentHead.next)
{
if(currentHead.data = value)
{
return true
}
currentHead = currentHead.next;
}
return false
}
console.log(dll1.findStartingHead(10))