链表
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
| package com.xiaohu.suanfa.linkList;
import java.util.concurrent.atomic.AtomicInteger;
public class SimpleLinkListDemo1 {
class HeroNode { private int nodeId; private String name; private String nickName; private HeroNode next;
public HeroNode(int nodeId, String name, String nickName) { this.nodeId = nodeId; this.name = name; this.nickName = nickName; }
@Override public String toString() { return "HeroNode{" + "nodeId=" + nodeId + ", name='" + name + '\'' + ", nickName='" + nickName + '\'' +
'}'; } }
class LinkListManger { private AtomicInteger size = new AtomicInteger();
public LinkListManger(AtomicInteger size) { this.size = size; }
public LinkListManger() {
}
HeroNode head = new HeroNode(0, "", "");
HeroNode temp = head;
public void addNode(HeroNode heroNode) {
while (true) { if (temp.next == null) { temp.next = heroNode; size.incrementAndGet(); temp = temp.next; System.out.println("添加成功"); break; } } }
public void addNodeByOrder(HeroNode heroNode) { boolean flag = false; HeroNode temp = head; while (true) { if (temp.next == null) { break; } if (!isNodeId(heroNode.nodeId) && heroNode.nodeId > temp.next.nodeId) { throw new RuntimeException("添加失败,编号有误");
} if (temp.next.nodeId > heroNode.nodeId) { break; } if (temp.next.nodeId == heroNode.nodeId) { flag = true; break; }
temp = temp.next; } if (flag) { System.out.println("链表元素编号已存在,添加失败"); return; }
heroNode.next = temp.next; temp.next = heroNode; size.incrementAndGet(); System.out.println("添加成功"); }
public HeroNode getNode(int heroId) { HeroNode temp = head.next; if (!isNodeId(heroId)) { throw new RuntimeException("数组索引越界,id有误"); } HeroNode heroNode; while (true) { if (temp.nodeId == heroId) { heroNode = temp;
return heroNode;
} temp = temp.next;
} }
public void setHead(HeroNode heroNode) { HeroNode temp = head; int nodeId = heroNode.nodeId;
if (!isNodeId(nodeId)) { throw new RuntimeException("数组索引越界,id有误"); } while (true) { if (temp.next == null && temp.next.nodeId != nodeId) { throw new RuntimeException("查无此人");
} if (temp.next.nodeId == nodeId) { temp.next.name = heroNode.name; temp.next.nodeId = nodeId; temp.next.nickName = heroNode.nickName; heroNode.next = temp.next.next; System.out.println("修改成功"); break; } temp = temp.next;
} }
public void deleteNode(int nodeId) { HeroNode temp = head;
while (true) { if (temp.next == null) { System.out.println("链表为空"); } if (temp.next == null && temp.next.nodeId != nodeId) { throw new RuntimeException("查无此人");
}
if (temp.next.nodeId == nodeId) { HeroNode node1 = temp.next; temp.next = null; temp.next = node1.next;
size.decrementAndGet(); System.out.println("删除成功"); node1 = null ; break; } temp = temp.next; } }
public void showListNode() { if (head.next == null) { System.out.println("链表为空"); } HeroNode tmp = head.next; while (true) { if (tmp == null) { break; } System.out.println("元素为:" + tmp); tmp = tmp.next; } }
public boolean isNodeId(int index) { return index >= 0 && index <= size.get() + 1; } }
public static void main(String[] args) {
LinkListManger linkListManger = new SimpleLinkListDemo1().new LinkListManger(); HeroNode heroNode1 = new SimpleLinkListDemo1().new HeroNode(1, "林冲1", "豹子头"); HeroNode heroNode2 = new SimpleLinkListDemo1().new HeroNode(2, "林冲2", "豹子头"); HeroNode heroNode3 = new SimpleLinkListDemo1().new HeroNode(3, "林冲3", "豹子头"); HeroNode heroNode4 = new SimpleLinkListDemo1().new HeroNode(4, "林冲4", "豹子头"); HeroNode heroNode5 = new SimpleLinkListDemo1().new HeroNode(2, "林冲5", "豹子头"); linkListManger.addNodeByOrder(heroNode4); linkListManger.addNodeByOrder(heroNode3); linkListManger.addNodeByOrder(heroNode1); linkListManger.addNodeByOrder(heroNode2); System.out.println(linkListManger.size); System.out.println(linkListManger.getNode(1)); System.out.println(linkListManger.getNode(2)); System.out.println(linkListManger.getNode(3));
System.out.println("============="); linkListManger.setHead(heroNode5); System.out.println(linkListManger.getNode(2)); linkListManger.showListNode();
linkListManger.deleteNode(1); linkListManger.deleteNode(4); linkListManger.deleteNode(2); linkListManger.deleteNode(3); System.out.println(linkListManger.size); linkListManger.showListNode();
}
}
|