链表的C#代码实现
题目: 1.在尾部增加元素
2. 删除值为value的元素
3.反转链表
思路:
增加和反转都没啥说的,就删除的时候需要考虑到的情况比较多,直接上代码,回头再分析
1 class LinkNode<T> 2 { 3 public LinkNode<T> Next { get; set; } 4 public T Value { get; set; } 5 6 public LinkNode<T> AddNodeToEnd(T value, LinkNode<T> head) 7 { 8 if (value != null) 9 { 10 if (head == null) 11 { 12 head = new LinkNode<T>(); 13 head.Value = value; 14 head.Next = null; 15 } 16 else 17 { 18 LinkNode<T> temp = head; 19 while (temp != null) 20 { 21 if (temp.Next == null) 22 { 23 temp.Next = new LinkNode<T>(); 24 temp.Next.Value = value; 25 break; 26 } 27 temp = temp.Next; 28 } 29 } 30 } 31 return head; 32 } 33 34 //public LinkNode<T> Remove(T value, LinkNode<T> head) 35 //{ 36 // LinkNode<T> pre = null; 37 // LinkNode<T> now = null; 38 // if (value != null) 39 // { 40 // if (head != null) 41 // { 42 // pre = head; 43 // if (head.Value.Equals(value) && head.Next == null) 44 // { 45 // head = null; 46 // return head; 47 // } 48 // if (head.Next != null) { now = head.Next; } 49 // while (now != null) 50 // { 51 // if (head.Value.Equals(value)) 52 // { 53 // head.Next = null; 54 // head = now; 55 // pre = head; 56 // if (head.Next != null) { now = head.Next; } 57 // else now = head; 58 // } 59 // if (now.Value.Equals(value)) 60 // { 61 // if (now.Next != null) 62 // { 63 // LinkNode<T> next = now.Next; 64 // pre.Next = next; 65 // now.Next = null; 66 // pre = pre.Next; 67 // now = pre.Next; 68 // continue; 69 // } 70 // else 71 // { 72 // if (pre == now) return null; 73 // pre.Next = null; 74 // break; 75 // } 76 // } 77 // pre = now; 78 // now = now.Next; 79 80 // } 81 // } 82 // } 83 // return head; 84 //} 85 86 public LinkNode<T> Remove(T value, LinkNode<T> head) 87 { 88 LinkNode<T> pre = null; 89 LinkNode<T> now = null; 90 if (value != null) 91 { 92 if (head != null) 93 { 94 //如果只有一个元素head 95 if (head.Next == null) 96 { 97 //如果head就是要找的那个 98 if (head.Value.Equals(value)) 99 { 100 head = null; 101 } 102 } 103 //如果有两个以上的元素 104 else 105 { 106 pre = head; 107 now = head.Next; 108 while (now != null) 109 { 110 if (pre.Value.Equals(value)) 111 { 112 //如果第一个就遇到了value 113 if (pre == head) 114 { 115 head = head.Next; 116 pre = head; 117 if (pre == null) break; 118 if (pre.Next != null) now = pre.Next; 119 else now = pre; 120 } 121 else 122 { 123 if (now.Next != null) 124 { 125 pre.Next = now.Next; 126 now = now.Next; 127 } 128 else break; 129 } 130 } 131 else 132 { 133 if (now.Value.Equals(value)) 134 { 135 if (now.Next != null && pre == head) 136 { 137 pre.Next = now.Next; 138 now = pre.Next; 139 continue; 140 } 141 else if (now.Next != null) 142 { 143 now = now.Next; 144 } 145 else pre.Next = null; 146 } 147 if (now.Next != null) 148 { 149 now = now.Next; 150 pre = pre.Next; 151 } 152 else break; 153 } 154 155 } 156 } 157 } 158 } 159 return head; 160 } 161 public LinkNode<T> Revert(LinkNode<T> head) 162 { 163 LinkNode<T> pre = null; 164 LinkNode<T> now = null; 165 LinkNode<T> next = null; 166 167 if (head != null && head.Next != null) 168 { 169 pre = head; 170 now = head.Next; 171 while (now != null) 172 { 173 if (pre == head && now.Next == null) 174 { 175 pre = head; 176 now.Next = pre; 177 head = now; 178 pre.Next = null; 179 return head; 180 } 181 else 182 { 183 if (pre == head) 184 { 185 pre.Next = null; 186 } 187 next = now.Next; 188 now.Next = pre; 189 pre = now; 190 now = next; 191 } 192 } 193 head = pre; 194 } 195 return head; 196 } 197 }
主函数:
1 static void Main(string[] args) 2 { 3 LinkNode<string> s = new LinkNode<string>(); 4 s = s.AddNodeToEnd("1", null); 5 s = s.AddNodeToEnd("2", s); 6 s = s.AddNodeToEnd("3", s); 7 s = s.AddNodeToEnd("5", s); 8 s = s.AddNodeToEnd("1", s); 9 s = s.Remove("3", s); 10 s = s.Revert(s); 11 Console.ReadKey(); 12 }