数据结构(C#)--双向链表
双向链表的定义以及常用的操作
1namespace DounlyLinkedlist
2{
3 //定义双向链表的结点
4 public class Node
5 {
6 public Object Element;
7 public Node FLink;
8 public Node BLink;
9
10 public Node()
11 {
12 Element = null;
13 FLink = null;
14 BLink = null;
15 }
16
17 public Node(Object element)
18 {
19 Element = element;
20 FLink = null;
21 BLink = null;
22 }
23
24 }
25
26 //链表操作的类
27 public class LinkedList
28 {
29 //此处和原书也不一致
30 public Node Header;
31
32 public LinkedList()
33 {
34 Header = new Node("Header");
35 Header.FLink = null;
36 Header.BLink = null;
37 }
38
39 //查找结点
40 private Node Find(Object item)
41 {
42 Node Current = new Node();
43 Current = Header;
44 while (Current.Element != item)
45 {
46 Current = Current.FLink;
47 }
48 return Current;
49 }
50
51 //插入结点
52 public void InsertNode(Object item,Object postionItem)
53 {
54 Node Current = new Node();
55 Node NewItem = new Node(item);
56 Current = Find(postionItem);
57 if (Current != null)
58 {
59 NewItem.FLink = Current.FLink;
60 NewItem.BLink = Current;
61 Current.FLink = NewItem;
62 }
63 }
64
65 //删除结点
66 public void Remove(Object item)
67 {
68 Node P = Find(item);
69 if (P.FLink != null)
70 {
71 P.BLink.FLink = P.FLink;
72 P.FLink.BLink = P.BLink;
73 P.BLink = null;
74 P.FLink = null;
75 }
76
77 }
78
79
80 //查找双向链表最后一个结点元素
81 private Node FindLast()
82 {
83 Node Current = new Node();
84 Current = Header;
85 while (!(Current.FLink == null))
86 {
87 Current = Current.FLink;
88 }
89 return Current;
90 }
91
92
93 //逆向打印双向链表
94 public void PrintReverse()
95 {
96 Node Current = new Node();
97 Current = FindLast();
98 while (!(Current.BLink == null))
99 {
100 Console.WriteLine(Current.Element);
101 Current = Current.BLink;
102 }
103 }
104
105 //打印双向链表
106 public void Print()
107 {
108 Node Current = new Node();
109 Current = Header;
110 while (!(Current.FLink == null))
111 {
112 Console.WriteLine(Current.FLink.Element);
113 Current = Current.FLink;
114 }
115 }
116 }
117}
2{
3 //定义双向链表的结点
4 public class Node
5 {
6 public Object Element;
7 public Node FLink;
8 public Node BLink;
9
10 public Node()
11 {
12 Element = null;
13 FLink = null;
14 BLink = null;
15 }
16
17 public Node(Object element)
18 {
19 Element = element;
20 FLink = null;
21 BLink = null;
22 }
23
24 }
25
26 //链表操作的类
27 public class LinkedList
28 {
29 //此处和原书也不一致
30 public Node Header;
31
32 public LinkedList()
33 {
34 Header = new Node("Header");
35 Header.FLink = null;
36 Header.BLink = null;
37 }
38
39 //查找结点
40 private Node Find(Object item)
41 {
42 Node Current = new Node();
43 Current = Header;
44 while (Current.Element != item)
45 {
46 Current = Current.FLink;
47 }
48 return Current;
49 }
50
51 //插入结点
52 public void InsertNode(Object item,Object postionItem)
53 {
54 Node Current = new Node();
55 Node NewItem = new Node(item);
56 Current = Find(postionItem);
57 if (Current != null)
58 {
59 NewItem.FLink = Current.FLink;
60 NewItem.BLink = Current;
61 Current.FLink = NewItem;
62 }
63 }
64
65 //删除结点
66 public void Remove(Object item)
67 {
68 Node P = Find(item);
69 if (P.FLink != null)
70 {
71 P.BLink.FLink = P.FLink;
72 P.FLink.BLink = P.BLink;
73 P.BLink = null;
74 P.FLink = null;
75 }
76
77 }
78
79
80 //查找双向链表最后一个结点元素
81 private Node FindLast()
82 {
83 Node Current = new Node();
84 Current = Header;
85 while (!(Current.FLink == null))
86 {
87 Current = Current.FLink;
88 }
89 return Current;
90 }
91
92
93 //逆向打印双向链表
94 public void PrintReverse()
95 {
96 Node Current = new Node();
97 Current = FindLast();
98 while (!(Current.BLink == null))
99 {
100 Console.WriteLine(Current.Element);
101 Current = Current.BLink;
102 }
103 }
104
105 //打印双向链表
106 public void Print()
107 {
108 Node Current = new Node();
109 Current = Header;
110 while (!(Current.FLink == null))
111 {
112 Console.WriteLine(Current.FLink.Element);
113 Current = Current.FLink;
114 }
115 }
116 }
117}
具体调用代码:
Code