-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.js
More file actions
84 lines (77 loc) · 1.57 KB
/
List.js
File metadata and controls
84 lines (77 loc) · 1.57 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
function Node(k)
{
this.key = k;
this.next = null;
}
function List()
{
this.len = 0;
this.head = new Node(null);
this.insert = function(x)
{
var p = new Node(x);
if(this.head.key == null)
{
this.head.key = x;
this.len++;
}
else
{
var n = this.head;
for(var i=0; i < this.len; i++)
{
if(n.next == null)
{
n.next = p;
this.len++;
}
else n = n.next;
}
}
return this;
};
this.search = function(x)
{
var n = this.head;
for(var i=0; i < this.len; i++)
{
if(x == n.key) return 1;
else n = n.next;
}
return 0;
};
this.print = function()
{
var n = this.head;
var tmp = "[]-";
for (var i = 0; i < this.len; i++)
{
tmp += ">"+n.key+"-";
n = n.next;
}
/*console.log(tmp+"//");*/
};
this.del = function(x)
{
var n = this.head;
if (!this.search(x)) return this;
if (this.head.key == x)
{
this.head = this.head.next;
this.len--;
return this;
}
else
{
var p = n.next;
if (p.key == x) n.next = p.next;
else
{
n = p;
p = p.next;
}
this.len--;
return this;
}
};
}