<div class="article_title">
<div class="cnblogs_code">
Node next; data;<span style="color: #0000ff;">public</span> Node( <span style="color: #0000ff;">int</span><span style="color: #000000;"> data) { </span><span style="color: #0000ff;">this</span>. data =<span style="color: #000000;"> data; } </span><span style="color: #008000;">//</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>此节点</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> display() { System. out.print( data </span>+ " "<span style="color: #000000;">); }
}
<span style="color: #0000ff;">package<span style="color: #000000;"> com.tyxh.link;
<span style="color: #008000;">//<span style="color: #008000;">单链表
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> LinkList {
<span style="color: #0000ff;">public Node first; <span style="color: #008000;">//<span style="color: #008000;"> 定义一个头结点
<span style="color: #0000ff;">private <span style="color: #0000ff;">int pos = 0;<span style="color: #008000;">//<span style="color: #008000;"> 节点的位置<span style="color: #0000ff;">public</span><span style="color: #000000;"> LinkList() { </span><span style="color: #0000ff;">this</span>. first = <span style="color: #0000ff;">null</span><span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 插入一个头节点</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> addFirstNode( <span style="color: #0000ff;">int</span><span style="color: #000000;"> data) { Node node </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Node(data); node. next </span>=<span style="color: #000000;"> first; first </span>=<span style="color: #000000;"> node; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>一个头结点,并返回头结点</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> Node deleteFirstNode() { Node tempNode </span>=<span style="color: #000000;"> first; first </span>=<span style="color: #000000;"> tempNode. next; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> tempNode; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 在任意位置插入节点 在index的后面插入</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> add(<span style="color: #0000ff;">int</span> index,<span style="color: #0000ff;">int</span><span style="color: #000000;"> data) { Node node </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Node(data); Node current </span>=<span style="color: #000000;"> first; Node prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"> first; </span><span style="color: #0000ff;">while</span> ( pos !=<span style="color: #000000;"> index) { prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"> current; current </span>=<span style="color: #000000;"> current. next; pos</span>++<span style="color: #000000;">; } node. next </span>=<span style="color: #000000;"> current; prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s. next </span>=<span style="color: #000000;"> node; pos </span>= 0<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>任意位置的节点</span> <span style="color: #0000ff;">public</span> Node deleteByPos( <span style="color: #0000ff;">int</span><span style="color: #000000;"> index) { Node current </span>=<span style="color: #000000;"> first; Node prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"> first; </span><span style="color: #0000ff;">while</span> ( pos !=<span style="color: #000000;"> index) { pos</span>++<span style="color: #000000;">; prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"> current; current </span>=<span style="color: #000000;"> current. next; } </span><span style="color: #0000ff;">if</span>(current ==<span style="color: #000000;"> first) { first </span>=<span style="color: #000000;"> first. next; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { pos </span>= 0<span style="color: #000000;">; prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s. next </span>=<span style="color: #000000;"> current. next; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> current; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 根据节点的data<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>节点(仅仅<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>第一个)</span> <span style="color: #0000ff;">public</span> Node deleteByData( <span style="color: #0000ff;">int</span><span style="color: #000000;"> data) { Node current </span>=<span style="color: #000000;"> first; Node prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>= first; <span style="color: #008000;">//</span><span style="color: #008000;">记住上一个节点</span> <span style="color: #0000ff;">while</span> (current. data !=<span style="color: #000000;"> data) { </span><span style="color: #0000ff;">if</span> (current. next == <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">; } prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"> current; current </span>=<span style="color: #000000;"> current. next; } </span><span style="color: #0000ff;">if</span>(current ==<span style="color: #000000;"> first) { first </span>=<span style="color: #000000;"> first. next; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s. next </span>=<span style="color: #000000;"> current. next; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> current; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>出所有的节点信息</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> displayAllNodes() { Node current </span>=<span style="color: #000000;"> first; </span><span style="color: #0000ff;">while</span> (current != <span style="color: #0000ff;">null</span><span style="color: #000000;">) { current.display(); current </span>=<span style="color: #000000;"> current. next; } System. out.println(); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 根据位置查找节点信息</span> <span style="color: #0000ff;">public</span> Node findByPos( <span style="color: #0000ff;">int</span><span style="color: #000000;"> index) { Node current </span>=<span style="color: #000000;"> first; </span><span style="color: #0000ff;">if</span> ( pos !=<span style="color: #000000;"> index) { current </span>=<span style="color: #000000;"> current. next; pos</span>++<span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> current; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 根据数据查找节点信息</span> <span style="color: #0000ff;">public</span> Node findByData( <span style="color: #0000ff;">int</span><span style="color: #000000;"> data) { Node current </span>=<span style="color: #000000;"> first; </span><span style="color: #0000ff;">while</span> (current. data !=<span style="color: #000000;"> data) { </span><span style="color: #0000ff;">if</span> (current. next == <span style="color: #0000ff;">null</span><span style="color: #000000;">) </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">; current </span>=<span style="color: #000000;"> current. next; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> current; }
}
<span style="color: #0000ff;">package<span style="color: #000000;"> com.tyxh.link;
<span style="color: #008000;">//<span style="color: #008000;">测试类
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> TestLinkList {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
LinkList linkList = <span style="color: #0000ff;">new<span style="color: #000000;"> LinkList();
linkList.addFirstNode(20<span style="color: #000000;">);
linkList.addFirstNode(21<span style="color: #000000;">);
linkList.addFirstNode(19<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;">19,21,20
linkList.add(1,22); <span style="color: #008000;">//<span style="color: #008000;">19,22,20
linkList.add(2,23); <span style="color: #008000;">//<span style="color: #008000;">19,23,20
linkList.add(3,99); <span style="color: #008000;">//<span style="color: #008000;">19,99,20
<span style="color: #000000;"> linkList.displayAllNodes();
<span style="color: #008000;">//<span style="color: #008000;"> Node node = linkList.deleteFirstNode();
<span style="color: #008000;">//<span style="color: #008000;"> System.out.println("node : " + node.data);
<span style="color: #008000;">//<span style="color: #008000;"> linkList.displayAllNodes();
<span style="color: #008000;">//<span style="color: #008000;"> node = linkList.deleteByPos(2);
<span style="color: #008000;">//<span style="color: #008000;"> System.out.println("node : " + node.data);
<span style="color: #008000;">//<span style="color: #008000;"> linkList.displayAllNodes();
<span style="color: #008000;">//<span style="color: #008000;"> linkList.deleteFirstNode();
Node node = linkList.deleteByData(19<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;"> Node node = linkList.deleteByPos(0);
System. out.println( "node : " +<span style="color: #000000;"> node. data);
linkList.displayAllNodes();
Node node1 = linkList.findByPos(0<span style="color: #000000;">);
System. out.println( "node1: " +<span style="color: #000000;"> node1. data);
Node node2 = linkList.findByData(22<span style="color: #000000;">);
System. out.println( "node2: " +<span style="color: #000000;"> node2. data);
}
}