C \ # 단 방향 링크 데이터 구조 (1)

30631 단어 데이터 구조
 
   

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


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 }

좋은 웹페이지 즐겨찾기