-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-doubly-linked-list.js
More file actions
107 lines (90 loc) · 1.95 KB
/
Copy path02-doubly-linked-list.js
File metadata and controls
107 lines (90 loc) · 1.95 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
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
class Node{
constructor(value=null){
this.data = value
this.prev = null
this.next = null
}
}
class DoublyLL{
constructor(){
this.head = null
}
InsertAtEnd(value){
let temp = new Node(value)
if(this.head == null){
this.head = temp
return
}
let t = this.head
while(t.next !== null){
t = t.next
}
t.next = temp
temp.prev = t
}
InsertAtBeg(value){
let temp = new Node(value)
if(this.head == null){
this.head = temp
return
}
temp.next = this.head
this.head.prev = temp
this.head = temp
}
InsertAtMid(value,x){
let t = this.head
while(t.next !== null){
if(t.data == x){
break
}else{
t = t.next
}
}
let temp = new Node(value)
temp.next = t.next
t.next.prev = temp
t.next = temp
temp.prev = t
}
deleteLL(value){
if(this.head == null){
console.log('List is empty')
return
}
let t = this.head
if(t.data == value){
this.head = t.next
this.head.prev = null
return
}
while(t.next !== null){
if(t.data == value){
t.prev.next = t.next
t.next.prev = t.prev
return
}else{
t = t.next
}
}
if(t.data == value){
t.prev.next = null
}
}
printLL(){
let t1 = this.head
while(t1 !== null){
console.log(t1.data)
t1 = t1.next
}
}
}
let list = new DoublyLL()
list.InsertAtEnd(10)
list.InsertAtEnd(20)
list.InsertAtEnd(50)
list.InsertAtBeg(5)
list.InsertAtMid(40,20)
list.deleteLL(5)
list.deleteLL(50)
list.printLL()