数据结构----链表的逆置及链表逆置和删除极小值
顺序表的逆置:
主要方法(代码):
1 public void Reverse() //顺序表反转
2 {
3 T temp=default(T);
4 for (int i = 0; i < _flag/2; i++)
5 {
6 temp =_ints[i];
7 _ints[i] = _ints[_flag - i - 1];
8 _ints[_flag - i - 1] = temp;
9 }
10 }
链表的逆置:
主要方法(代码):
1 public void Reverse()//链表的反转
2 {
3 if (_count <1)
4 {
5 Console.WriteLine("链表长度不足");
6 return;
7 }
8 Node<T> X1, X2;
9 X2 = _head.Next;
10 _head.Next = null;
11 while (X2!=null)
12 {
13 //注意_head.next一直在变,相当于一个变量
14 X1 = X2.Next;
15 X2.Next = _head.Next;
16 _head.Next = X2;
17 X2 = X1;
18 }
19 }
链表删除极小值:
主要方法(代码):
1 public T DeleteMinInLianBiao(Func <Node<T>,Node<T>,bool> _func)//链表删除最小值
2 {
3 Node<T> deletePreMin, deleteMin, PreMin, Min;
4 deletePreMin = PreMin = _head;
5 deleteMin = Min = _head.Next;
6 while(Min!=null)
7 {
8 //查找最小值
9 if(_func( Min,PreMin))
10 {
11 deletePreMin = PreMin;
12 deleteMin = Min;
13 }
14 PreMin = PreMin.Next;
15 Min = Min.Next;
16 }
17 //删除最小值
18 deletePreMin.Next = deletePreMin.Next.Next;
19 deleteMin.Next = null;
20 _count--;
21
22 return deleteMin.Data;
23 }
附加委托方法:
1 static bool shows(Node<double> a, Node<double> b)
2 {
3 if(a.Data>b.Data)
4 {
5 return false;
6 }
7 return true;
8 }