-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathslist.js
167 lines (141 loc) · 3.15 KB
/
slist.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
156
157
158
159
160
161
162
163
164
165
166
167
function Node(val){
this.val = val
this.next = null
}
function Slist(){
this.head = null
this.add = function(val){
if(this.head == null){
this.head = new Node(val)
return this.head
}
runner = this.head
while(runner.next!= null){
runner = runner.next
}
runner.next = new Node(val)
return this.head
}
this.display = function(runner){
runner = this.head
while(runner!=null){
console.log(runner.val)
runner=runner.next
}
}
this.removefront = function(){
this.head = this.head.next
}
this.getlength = function(){
runner = this.head
count = 0
while(runner!=null){
count += 1
runner=runner.next
}
return count
}
this.remove = function(value){
if (this.head.val == value){
this.head = this.head.next
return
}
runner = this.head
while(runner.next!=null){
if(runner.next.val == value){
runner.next = runner.next.next
return
}
runner = runner.next
}
}
}
list = new Slist()
list.add(27)
list.add(94)
list.add(200)
list.add(56)
length = list.getlength()
console.log("Length of the list is:", length)
console.log("\nElements of the list are")
list.display()
list.removefront()
console.log("\nElements of the list are")
list.display()
list.remove(56)
console.log("\nElements of the list are")
list.display()
list.remove(200)
console.log("\nElements of the list are")
list.display()
list.remove(94)
console.log("\nElements of the list are")
list.display()
//ptr is the reference to the first node
function add(ptr, val){
if(ptr == null){
ptr = new Node(val)
return ptr
}
runner = ptr
while(runner.next!= null){
runner = runner.next
}
runner.next = new Node(val)
return ptr
}
function display(ptr){
runner = ptr
while(runner!=null){
console.log(runner.val)
runner=runner.next
}
}
function removefront(ptr){
ptr = ptr.next
return ptr
}
function getlength(ptr){
runner = ptr
count = 0
while(runner!=null){
count += 1
runner=runner.next
}
return count
}
function remove(ptr, value){
if (ptr.val == value){
ptr = ptr.next
return ptr
}
runner = ptr
while(runner.next!=null){
if(runner.next.val == value){
runner.next = runner.next.next
return ptr
}
runner = runner.next
}
return ptr
}
list2 = add(null, 5)
list2 = add(list2, 7)
list2 = add(list2, 9)
list2 = add(list2, 11)
console.log("\nList2 Elements:")
display(list2)
len=getlength(list2)
console.log("\nLength of list2:", len)
list2 = removefront(list2)
console.log("\nList2 Elements:")
display(list2)
list2 = remove(list2, 7)
console.log("\nList2 Elements:")
display(list2)
list2 = remove(list2, 9)
console.log("\nList2 Elements:")
display(list2)
list2 = remove(list2, 11)
console.log("\nList2 Elements:")
display(list2)