C# 单向链表数据结构 (一)


单向链表数据结构是有节点组成,每个节点包含两部分,第一部分为存储数据,
第二部分为指向下一个节点的指针。注意,有两个特色的节点,分别为“头节点”
和“尾节点”,头节点本身没有数据,只存储下一个节点的指针,尾节点只存数据


1
//节点类 2 public class ListNode 3 { 4 public ListNode(int NewValue) 5 { 6 Value = NewValue; 7 } 8 //前一个 9 public ListNode Previous; 10 //后一个 11 public ListNode Next; 12 // 13 public int Value; 14 15 16 } 17 //链表类 18 public class Clist 19 { 20 public Clist()//构造函数 21 { 22 //初始化 23 ListCountValue = 0; 24 Head = null; 25 Tail = null; 26 } 27 private ListNode Head;//头指针 28 private ListNode Tail;//尾指针 29 private ListNode Current;//当前指针 30 private int ListCountValue;//链表数据的个数 31 //向尾部添加数据方法 32 public void Append(int DataValue) 33 { 34 ListNode NewNode = new ListNode(DataValue); 35 36 if (IsNull())//如果头指针为空 37 { 38 Head = NewNode; 39 Tail = NewNode; 40 } 41 else 42 { 43 Tail.Next = NewNode; 44 NewNode.Previous = Tail; 45 Tail = NewNode; 46 } 47 Current = NewNode; 48 ListCountValue += 1;//链表个数加 1 49 } 50 public void Delete() 51 { 52 if (!IsNull())//若为空链表 53 { 54 if (!IsBof())//若删除头 55 { 56 Head = Current.Next; 57 Current = Head; 58 ListCountValue -= 1; 59 return; 60 } 61 if (!IsEof())//若删除尾 62 { 63 Tail = Current.Previous; 64 Current = Tail; 65 ListCountValue -= 1; 66 return; 67 } 68 Current.Previous.Next = Current.Next;//若删除中间数据 69 Current = Current.Previous; 70 ListCountValue -= 1; 71 return; 72 } 73 } 74 //向后移动一个数据的方法 75 public void MoveNext() 76 { 77 if (!IsEof()) Current = Current.Next; 78 } 79 //向前移动一个数据的方法 80 public void MovePrevious() 81 { 82 if (!IsBof()) Current = Current.Previous; 83 } 84 //移动到第一个数据方法 85 public void MoveFrist() 86 { 87 Current = Head; 88 } 89 //移动到最后一个数据方法 90 public void MoveLast() 91 { 92 Current = Tail; 93 } 94 //判断是否为空链表方法 95 public bool IsNull() 96 { 97 if (ListCountValue == 0) 98 return true; 99 return false; 100 } 101 //判断是否为到达尾部方法 102 public bool IsEof() 103 { 104 if (Current == Tail) 105 return true; 106 return false; 107 } 108 //判断是否为到达头部方法 109 public bool IsBof() 110 { 111 if (Current == Head) 112 return true; 113 return false; 114 } 115 //取得当前结点的值方法 116 public int GetCurrentValue() 117 { 118 return Current.Value; 119 } 120 //取得链表的数据个数方法 121 public int ListCount 122 { 123 get { return ListCountValue; } 124 } 125 //清空链表方法 126 public void Clear() 127 { 128 MoveFrist(); 129 while (!IsNull()) 130 { 131 Delete();//若不为空链表,从尾部删除 132 } 133 } 134 //在当前位置前输入数据方法 135 public void Insert(int DataValue) 136 { 137 ListNode NewNode = new ListNode(DataValue); 138 if (IsNull()) 139 { 140 Append(DataValue);//为空表,则添加 141 return; 142 } 143 if (IsBof()) 144 { 145 //为头部插入 146 NewNode.Next = Head; 147 Head.Previous = NewNode; 148 Head = NewNode; 149 Current = Head; 150 ListCountValue += 1; 151 return; 152 } 153 //中间插入 154 NewNode.Next = Current; 155 NewNode.Previous = Current.Previous; 156 Current.Previous.Next = NewNode; 157 Current = NewNode; 158 ListCountValue += 1; 159 } 160 //进行升序插入方法 161 public void InsertAscending(int InsertValue) 162 { 163 //参数:InsertValue 插入的数据 164 //为空链表 165 if (IsNull()) 166 { 167 //添加 168 Append(InsertValue); 169 return; 170 } 171 MoveFrist();//移动到头 172 if ((InsertValue < GetCurrentValue())) 173 { 174 Insert(InsertValue);//满足添加,则插入 175 return; 176 } 177 while (true) 178 { 179 if (InsertValue < GetCurrentValue()) 180 { 181 //满足条件,则插入,退出 182 Insert(InsertValue); 183 break; 184 } 185 if (IsEof()) 186 {//从尾部插入 187 Append(InsertValue); 188 break; 189 } 190 //移动到下一个指针 191 MoveNext(); 192 } 193 } 194 195 //进行降序插入方法 196 public void InsertUnAscending(int InsertValue) 197 { 198 //参数:InsertValue 插入的数据 199 //为空链表 200 if (IsNull()) 201 { 202 //添加 203 Append(InsertValue); 204 return; 205 } 206 //移动到头 207 MoveFrist(); 208 if (InsertValue > GetCurrentValue()) 209 { 210 //满足条件,则插入,退出 211 Insert(InsertValue); 212 return; 213 } 214 while (true) 215 { 216 if (InsertValue > GetCurrentValue()) 217 { 218 //满足条件,则插入,退出 219 Append(InsertValue); 220 break; 221 } 222 if (IsEof()) 223 { 224 //尾部添加 225 Append(InsertValue); 226 break; 227 } 228 //移动到下一个指针 229 MoveNext(); 230 } 231 232 } 233 }

 

posted @ 2014-01-16 17:36  yellowshorts  阅读(3721)  评论(1编辑  收藏  举报