理论基础:
链表是用一组任意的存储单元来存储线性表中的数据元素。
如果结点的引用域只存储该结点直接后继结点的存储地址,则该链表叫单链表(Singly Linked List)。
单链表由头引用H唯一确定。头引用指向单链表的第一个结点,也就是把单链表第一个结点的地址放在H中。
C#实现:
1接口
引用线性表的接口IListDS<T>
2实现
首先,必须定义一个单链表的节点类。
Code
1 public class Node<T>
2 {
3 private T data; //数据域
4 private Node<T> next; //引用域
5
6
7 public Node(T val)
8 {
9 data = val;
10 next = null;
11 }
12
13 public Node()
14 {
15 data = default(T);
16 next = null;
17 }
18
19 public T Data
20 {
21 get
22 {
23 return data;
24 }
25 set
26 {
27 data = value;
28 }
29 }
30 public Node<T> Next
31 {
32 get
33 {
34 return next;
35 }
36 set
37 {
38 next = value;
39 }
40 }
41
42
43 }
1 public class Node<T>
2 {
3 private T data; //数据域
4 private Node<T> next; //引用域
5
6
7 public Node(T val)
8 {
9 data = val;
10 next = null;
11 }
12
13 public Node()
14 {
15 data = default(T);
16 next = null;
17 }
18
19 public T Data
20 {
21 get
22 {
23 return data;
24 }
25 set
26 {
27 data = value;
28 }
29 }
30 public Node<T> Next
31 {
32 get
33 {
34 return next;
35 }
36 set
37 {
38 next = value;
39 }
40 }
41
42
43 }
实现主体类
Append,Insert,InsertBack三个方法实质上都是插入操作,可以考虑用overload或者override来实现,有兴趣的朋友试试。
Code
1public class LinkList<T> : IListDS<T>
2 {
3 private Node<T> head;
4 public Node<T> Head
5 {
6 get
7 {
8 return head;
9 }
10 set
11 {
12 head = value;
13 }
14 }
15 public LinkList()
16 {
17 head = null;
18 }
19
20 /**//// <summary>
21 /// 获取长度
22 /// </summary>
23 /// <returns></returns>
24 public int GetLength()
25 {
26 Node<T> p = head;
27 int len = 0;
28 while (p != null)
29 {
30 ++len;
31 p = p.Next;
32 }
33 return len;
34 }
35
36 /**//// <summary>
37 /// 清空操作
38 /// </summary>
39 public void Clear()
40 {
41 head = null;
42 }
43
44 /**//// <summary>
45 /// 判断线性表是否为空
46 /// </summary>
47 /// <returns></returns>
48 public bool IsEmpty()
49 {
50 if (head == null)
51 {
52 return true;
53 }
54 else
55 {
56 return false;
57 }
58 }
59
60 /**//// <summary>
61 /// 附加操作,线性表未满,将值为item的新元素添加到末尾
62 /// </summary>
63 /// <param name="item"></param>
64 public void Append(T item)
65 {
66 Node<T> newNode = new Node<T>(item); //根据元素创建新的节点
67 Node<T> node = new Node<T>();
68
69 if (head == null)
70 {
71 head = newNode;
72 return;
73 }
74 node = head;
75 while (node.Next != null)
76 {
77 node = node.Next;
78 }
79 node.Next = newNode;
80
81 }
82
83 /**//// <summary>
84 /// 寻找节点
85 /// </summary>
86 /// <param name="i"></param>
87 /// <returns></returns>
88 public Node<T> FindNode(int i)
89 {
90 if (IsEmpty())
91 {
92 Console.Write("List is empty");
93 return null;
94 }
95 if (i < 1)
96 {
97 Console.Write("Index is error");
98 return null;
99 }
100 Node<T> current = head;
101 int j = 1;
102
103 while (current.Next != null && j < i)
104 {
105 ++j;
106 current = current.Next;
107 }
108 return current;
109 }
110
111 /**//// <summary>
112 /// 插入操作,在第i个节点前面插入item
113 /// </summary>
114 /// <param name="item"></param>
115 /// <param name="i"></param>
116 public void Insert(T item, int i)
117 {
118 Node<T> newNode = new Node<T>(item);
119 Node<T> node = new Node<T>();
120 Node<T> current = FindNode(i);
121 if (current != null)
122 {
123 node = current; //对目标节点备份
124 newNode.Next = current;
125 node.Next = newNode;
126 }
127 }
128
129 /**//// <summary>
130 /// 插入操作,在第i个节点后面插入item
131 /// </summary>
132 /// <param name="item"></param>
133 /// <param name="i"></param>
134 public void InsertBack(T item, int i)
135 {
136 Node<T> newNode = new Node<T>(item);
137 Node<T> current = FindNode(i);
138 if (current != null)
139 {
140 newNode.Next = current.Next;
141 current.Next = newNode;
142 }
143 }
144
145 /**//// <summary>
146 /// 删除操作
147 /// </summary>
148 /// <param name="i"></param>
149 /// <returns></returns>
150 public T Delete(int i)
151 {
152 Node<T> current = FindNode(i);
153 Node<T> node = new Node<T>();
154 if (current != null)
155 {
156 node = current; //对目标节点备份
157 node.Next = current.Next;
158 return current.Data;
159 }
160 else
161 {
162 Console.Write("the node is not exist!");
163 return default(T);
164 }
165 }
166
167 /**//// <summary>
168 /// 去表元
169 /// </summary>
170 /// <param name="i"></param>
171 /// <returns></returns>
172 public T GetElem(int i)
173 {
174 Node<T> current = FindNode(i);
175
176 if (current != null)
177 {
178 return current.Data;
179 }
180 else
181 {
182 Console.Write("the node is not exist!");
183 return default(T);
184 }
185 }
186
187 /**//// <summary>
188 /// 按值查找
189 /// </summary>
190 /// <param name="value"></param>
191 /// <returns></returns>
192 public int Locate(T value)
193 {
194 if (IsEmpty())
195 {
196 Console.WriteLine("List is Empty!");
197 return -1;
198 }
199 Node<T> current = new Node<T>();
200 current = head;
201 int i = 1;
202 while (current.Next != null && !current.Data.Equals(value))
203 {
204 current = current.Next;
205 ++i;
206 }
207 return i;
208 }
209 }
1public class LinkList<T> : IListDS<T>
2 {
3 private Node<T> head;
4 public Node<T> Head
5 {
6 get
7 {
8 return head;
9 }
10 set
11 {
12 head = value;
13 }
14 }
15 public LinkList()
16 {
17 head = null;
18 }
19
20 /**//// <summary>
21 /// 获取长度
22 /// </summary>
23 /// <returns></returns>
24 public int GetLength()
25 {
26 Node<T> p = head;
27 int len = 0;
28 while (p != null)
29 {
30 ++len;
31 p = p.Next;
32 }
33 return len;
34 }
35
36 /**//// <summary>
37 /// 清空操作
38 /// </summary>
39 public void Clear()
40 {
41 head = null;
42 }
43
44 /**//// <summary>
45 /// 判断线性表是否为空
46 /// </summary>
47 /// <returns></returns>
48 public bool IsEmpty()
49 {
50 if (head == null)
51 {
52 return true;
53 }
54 else
55 {
56 return false;
57 }
58 }
59
60 /**//// <summary>
61 /// 附加操作,线性表未满,将值为item的新元素添加到末尾
62 /// </summary>
63 /// <param name="item"></param>
64 public void Append(T item)
65 {
66 Node<T> newNode = new Node<T>(item); //根据元素创建新的节点
67 Node<T> node = new Node<T>();
68
69 if (head == null)
70 {
71 head = newNode;
72 return;
73 }
74 node = head;
75 while (node.Next != null)
76 {
77 node = node.Next;
78 }
79 node.Next = newNode;
80
81 }
82
83 /**//// <summary>
84 /// 寻找节点
85 /// </summary>
86 /// <param name="i"></param>
87 /// <returns></returns>
88 public Node<T> FindNode(int i)
89 {
90 if (IsEmpty())
91 {
92 Console.Write("List is empty");
93 return null;
94 }
95 if (i < 1)
96 {
97 Console.Write("Index is error");
98 return null;
99 }
100 Node<T> current = head;
101 int j = 1;
102
103 while (current.Next != null && j < i)
104 {
105 ++j;
106 current = current.Next;
107 }
108 return current;
109 }
110
111 /**//// <summary>
112 /// 插入操作,在第i个节点前面插入item
113 /// </summary>
114 /// <param name="item"></param>
115 /// <param name="i"></param>
116 public void Insert(T item, int i)
117 {
118 Node<T> newNode = new Node<T>(item);
119 Node<T> node = new Node<T>();
120 Node<T> current = FindNode(i);
121 if (current != null)
122 {
123 node = current; //对目标节点备份
124 newNode.Next = current;
125 node.Next = newNode;
126 }
127 }
128
129 /**//// <summary>
130 /// 插入操作,在第i个节点后面插入item
131 /// </summary>
132 /// <param name="item"></param>
133 /// <param name="i"></param>
134 public void InsertBack(T item, int i)
135 {
136 Node<T> newNode = new Node<T>(item);
137 Node<T> current = FindNode(i);
138 if (current != null)
139 {
140 newNode.Next = current.Next;
141 current.Next = newNode;
142 }
143 }
144
145 /**//// <summary>
146 /// 删除操作
147 /// </summary>
148 /// <param name="i"></param>
149 /// <returns></returns>
150 public T Delete(int i)
151 {
152 Node<T> current = FindNode(i);
153 Node<T> node = new Node<T>();
154 if (current != null)
155 {
156 node = current; //对目标节点备份
157 node.Next = current.Next;
158 return current.Data;
159 }
160 else
161 {
162 Console.Write("the node is not exist!");
163 return default(T);
164 }
165 }
166
167 /**//// <summary>
168 /// 去表元
169 /// </summary>
170 /// <param name="i"></param>
171 /// <returns></returns>
172 public T GetElem(int i)
173 {
174 Node<T> current = FindNode(i);
175
176 if (current != null)
177 {
178 return current.Data;
179 }
180 else
181 {
182 Console.Write("the node is not exist!");
183 return default(T);
184 }
185 }
186
187 /**//// <summary>
188 /// 按值查找
189 /// </summary>
190 /// <param name="value"></param>
191 /// <returns></returns>
192 public int Locate(T value)
193 {
194 if (IsEmpty())
195 {
196 Console.WriteLine("List is Empty!");
197 return -1;
198 }
199 Node<T> current = new Node<T>();
200 current = head;
201 int i = 1;
202 while (current.Next != null && !current.Data.Equals(value))
203 {
204 current = current.Next;
205 ++i;
206 }
207 return i;
208 }
209 }
碰到的问题:1 方法参数为int类型,改用object类型碰到一些问题,查找节点的时候,遍历整个链表,如何对比参数节点
对比参数会不会影响算法的时间复杂度
2 append,insert,insertback方法本质上都是插入操作,有没有更好的方法实现
代码没有经过测试,有兴趣的朋友可以试试,有问题,告知一下!