1.用js寫一個類似Dom的getElementByTagName的函數。
試驗了一下,js的childNodes屬性有相容性和準确度的問題,經常會多一些空白标簽,是以用深搜的方法根本行不通,沒法建樹,沒法準确擷取節點樹資訊。
是以,想到了正規表達式,隻用幾行代碼即可搞定。
1 window.onload = function () {
2 var reg = new RegExp("<span.+/span>", "g");
3 var element = new Array();
4 var temp;
5 while ((temp = reg.exec(document.body.innerHTML)) != null) {
6 element.push(temp);
7 }
8 while ((temp = element.pop()) != null) {
9 alert(temp);
10 }
11 }
之後,又想到另一種DFS深搜解法
1 class Program
2 {
3 private static XmlElement _root;
4 public static XmlElement Root
5 {
6 get { return _root; }
7 set { _root = value; }
8 }
9
10 private static List<XmlNode> list = new List<XmlNode>();
11 static void Main(string[] args)
12 {
13 WebClient wc = new WebClient();
14 string htmlStr = wc.DownloadString("http://localhost:30907/test1.html");
15 XmlDocument doc = new XmlDocument();
16 doc.LoadXml(htmlStr);
17 Root = doc.DocumentElement;
18
19 DFS(Root);
20 foreach (var n in list)
21 {
22 Console.WriteLine(n.OuterXml);
23 }
24
25 Console.ReadKey();
26 }
27
28 private static void DFS(XmlNode parent)
29 {
30
31 if (!parent.HasChildNodes)
32 {
33 return;
34 }
35 else if (parent.HasChildNodes)
36 {
37 for (int j = 0; j < parent.ChildNodes.Count; j++)
38 {
39 if (parent.ChildNodes[j].Name == "span")
40 {
41 list.Add(parent.ChildNodes[j]);
42 }
43 DFS(parent.ChildNodes[j]);
44 }
45 }
46 }
47 }
如果用寬搜BFS也可以,時間複雜度都是o(n)
1 class Program
2 {
3 private static List<XmlNode> list = new List<XmlNode>();
4 private static Queue<XmlNode> queue = new Queue<XmlNode>();
5 static void Main(string[] args)
6 {
7 WebClient wc = new WebClient();
8 string htmlStr = wc.DownloadString("http://localhost:30907/test1.html");
9 XmlDocument doc = new XmlDocument();
10 doc.LoadXml(htmlStr);
11 XmlElement root = doc.DocumentElement;
12 BFS(root);
13
14 foreach (var n in list)
15 {
16 Console.WriteLine(n.OuterXml);
17 }
18
19 Console.ReadKey();
20 }
21
22 public static void BFS(XmlNode parent)
23 {
24 queue.Enqueue(parent);
25 while (queue.Count != 0)
26 {
27 XmlNode n = queue.Dequeue();
28 if (n.Name == "span")
29 {
30 list.Add(n);
31 }
32 for (int i = 0; i < n.ChildNodes.Count; i++)
33 {
34 queue.Enqueue(n.ChildNodes[i]);
35 }
36 }
37 }
38 }
2.用js寫一個連結清單,或者用其他語言。
我比較熟C#是以就用C#寫,類似List。
1 public class Node
2 {
3 public int Num{get;set;}
4 public Node Next{get;set;}
5 }
6
7 public class MyList
8 {
9 public Node Head { get; set; }
10 public int Length { get; set; }
11
12 public MyList()
13 {
14 Head = new Node();
15 Head.Next = null;
16 Length = 1;
17 }
18
19 public void Add(int num)
20 {
21 Node n = new Node();
22 n.Num = num;
23 Node node = Head;
24 while (node.Next != null)
25 {
26 node = node.Next;
27 }
28 node.Next = n;
29 Length++;
30 }
31
32 public void Delete(int index)
33 {
34 Node n = Head;
35 if (index == 0)
36 {
37 Head = n.Next;
38 }
39 else if(Length-1==index)
40 {
41 for (int i = 0; i < index - 1; i++)
42 {
43 n = n.Next;
44 }
45 n.Next = null;
46 }
47 else
48 {
49 for (int i = 0; i < index - 1; i++)
50 {
51 n = n.Next;
52 }
53 n.Next = n.Next.Next;
54 }
55 Length--;
56 }
57
58 public int this[int index]
59 {
60 get {
61 Node n = Head;
62 for (int i = 0; i < index; i++)
63 {
64 n = n.Next;
65 }
66 return n.Num;
67 }
68 set {
69 Node n = Head;
70 for (int i = 0; i < index; i++)
71 {
72 n = n.Next;
73 }
74 n.Num = value;
75 }
76 }
77 }