双向链表
//双向链表实现:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SequenceList 8 { 9 public class DoubleLinkList<T> : IListDS<T> 10 { 11 12 private DoubleNode<T> head; 13 public DoubleNode<T> Head 14 { 15 get { return head; } 16 set { head = value; } 17 } 18 19 20 public DoubleLinkList() 21 { 22 head = null; 23 } 24 25 26 public int GetLength() 27 { 28 if (IsEmpty()) 29 { 30 return -1; 31 } 32 33 int i = 1; 34 35 DoubleNode<T> p = head; 36 while (p.Next != null) 37 { 38 p = p.Next; 39 ++i; 40 } 41 42 return i; 43 } 44 45 public void Clear() 46 { 47 head = null; 48 } 49 50 public bool IsEmpty() 51 { 52 //throw new NotImplementedException(); 53 return head == null; 54 } 55 56 public void Append(T item) 57 { 58 DoubleNode<T> target = new DoubleNode<T>(item); 59 60 if (IsEmpty()) 61 { 62 head = target; 63 return; 64 } 65 DoubleNode<T> p = head; 66 while (p.Next != null) 67 { 68 69 p = p.Next; 70 71 } 72 p.Next = target; 73 target.Prev = p; 74 } 75 76 77 //在第i个位置之前插入 78 public void Insert(T item, int i) 79 { 80 // throw new NotImplementedException(); 81 if (IsEmpty() || i < 1) 82 { 83 Console.WriteLine("Empty or position is error;"); 84 return; 85 } 86 87 DoubleNode<T> target = new DoubleNode<T>(item); 88 DoubleNode<T> p = head; 89 if (i == 1) 90 { 91 target.Next = p; 92 p.Prev = target; 93 head = target; 94 return; 95 } 96 97 int j = 1; 98 while (p.Next != null && j < i) 99 { 100 p = p.Next; 101 ++j; 102 } 103 104 if (j == i) 105 { 106 p.Prev.Next = target; 107 target.Next = p; 108 } 109 110 } 111 112 113 //在第i个位置之后插入 114 public void InsertAfter(T item,int i) 115 { 116 DoubleNode<T> target = new DoubleNode<T>(item); 117 118 if (i < 1) 119 { 120 return; 121 } 122 123 if (IsEmpty()) 124 { 125 head = target; 126 return; 127 } 128 129 DoubleNode<T> p = head; 130 131 int j = 1; 132 while(p.Next!=null && j<i) 133 { 134 ++j; 135 p = p.Next; 136 } 137 138 if (j == i) 139 { 140 Console.WriteLine(p.Next.Data+" -----"); 141 p.Next.Prev = target; 142 target.Next = p.Next; 143 p.Next = target; 144 target.Prev = p; 145 } 146 147 } 148 149 150 public T Delete(int i) 151 { 152 // throw new NotImplementedException(); 153 154 if (i < 1) 155 return default(T); 156 if (IsEmpty()) 157 return default(T); 158 159 DoubleNode<T> p = head; 160 int j = 1; 161 while (p.Next != null && j < i) 162 { 163 ++j; 164 p = p.Next; 165 } 166 167 if (j == i) 168 { 169 p.Prev.Next = p.Next; 170 p.Next.Prev = p.Prev; 171 return p.Data; 172 } 173 return default(T); 174 } 175 176 public T GetElem(int i) 177 { 178 // throw new NotImplementedException(); 179 180 if (i < 1) 181 return default(T); 182 if (IsEmpty()) 183 return default(T); 184 185 DoubleNode<T> p = head; 186 187 int j = 1; 188 189 while (p.Next != null && j < i) 190 { 191 ++j; 192 p = p.Next; 193 } 194 195 if (j == i) 196 { 197 return p.Data; 198 } 199 200 201 202 return default(T); 203 204 } 205 206 public int Locate(T value) 207 { 208 // throw new NotImplementedException(); 209 210 if (IsEmpty() || value == null) 211 { 212 return -1; 213 } 214 215 DoubleNode<T> p = head; 216 int j = 1; 217 218 while (p.Next!=null && !p.Data.Equals(value)) 219 { 220 p = p.Next; 221 ++j; 222 } 223 return j; 224 } 225 } 226 }
//双向结点:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SequenceList 8 { 9 public class DoubleNode<T> 10 { 11 private T data; //数据域 12 private DoubleNode<T> prev; //前驱引用 13 private DoubleNode<T> next; //后继引用 14 15 //构造器 16 public DoubleNode() 17 { 18 data = default(T); 19 next = null; 20 } 21 22 public DoubleNode(T val,DoubleNode<T> p) 23 { 24 data = val; 25 next = p; 26 } 27 28 public DoubleNode(DoubleNode<T> n) 29 { 30 data = default(T); 31 next = n; 32 } 33 34 public DoubleNode(T val) 35 { 36 data = val; 37 next = null; 38 } 39 40 41 42 //数据域属性: 43 public T Data 44 { 45 get { return data; } 46 set { data = value; } 47 } 48 49 public DoubleNode<T> Prev 50 { 51 get { return prev; } 52 set { prev = value; } 53 } 54 55 public DoubleNode<T> Next 56 { 57 set { next = value; } 58 get { return next; } 59 } 60 61 } 62 63 }
仰望梦想的姿势