用java简单的实现单链表的基本操作

前端之家收集整理的这篇文章主要介绍了用java简单的实现单链表的基本操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="article_title">
<div class="cnblogs_code">


  Node next; 
       data;
 <span style="color: #0000ff;"&gt;public</span> Node( <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; data) {
       </span><span style="color: #0000ff;"&gt;this</span>. data =<span style="color: #000000;"&gt; data;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>此节点</span>
 <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; display() {
      System. out.print( data </span>+ " "<span style="color: #000000;"&gt;);
 }

}

<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;"&gt;public</span><span style="color: #000000;"&gt; LinkList() {
       </span><span style="color: #0000ff;"&gt;this</span>. first = <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 插入一个头节点</span>
 <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span> addFirstNode( <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; data) {
      Node node </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; Node(data);
      node. next </span>=<span style="color: #000000;"&gt; first;
       first </span>=<span style="color: #000000;"&gt; node;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>一个头结点,并返回头结点</span>
 <span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; Node deleteFirstNode() {
      Node tempNode </span>=<span style="color: #000000;"&gt; first;
       first </span>=<span style="color: #000000;"&gt; tempNode. next;
       </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; tempNode;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 在任意位置插入节点 在index的后面插入</span>
 <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span> add(<span style="color: #0000ff;"&gt;int</span> index,<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; data) {
      Node node </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; Node(data);
      Node current </span>=<span style="color: #000000;"&gt; first;
      Node prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"&gt; first;
       </span><span style="color: #0000ff;"&gt;while</span> ( pos !=<span style="color: #000000;"&gt; index) {
          prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"&gt; current;
          current </span>=<span style="color: #000000;"&gt; current. next;
           pos</span>++<span style="color: #000000;"&gt;;
      }
      node. next </span>=<span style="color: #000000;"&gt; current;
      prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s. next </span>=<span style="color: #000000;"&gt; node;
       pos </span>= 0<span style="color: #000000;"&gt;;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>任意位置的节点</span>
 <span style="color: #0000ff;"&gt;public</span> Node deleteByPos( <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; index) {
      Node current </span>=<span style="color: #000000;"&gt; first;
      Node prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"&gt; first;
       </span><span style="color: #0000ff;"&gt;while</span> ( pos !=<span style="color: #000000;"&gt; index) {
           pos</span>++<span style="color: #000000;"&gt;;
          prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"&gt; current;
          current </span>=<span style="color: #000000;"&gt; current. next;
      }
       </span><span style="color: #0000ff;"&gt;if</span>(current ==<span style="color: #000000;"&gt; first) {
           first </span>=<span style="color: #000000;"&gt; first. next;
      } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {
           pos </span>= 0<span style="color: #000000;"&gt;;
          prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s. next </span>=<span style="color: #000000;"&gt; current. next;
      }
       </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; current;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 根据节点的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;"&gt;public</span> Node deleteByData( <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; data) {
      Node current </span>=<span style="color: #000000;"&gt; first;
      Node prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>= first; <span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;记住上一个节点</span>
       <span style="color: #0000ff;"&gt;while</span> (current. data !=<span style="color: #000000;"&gt; data) {
           </span><span style="color: #0000ff;"&gt;if</span> (current. next == <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) {
                </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;;
          }
          prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s </span>=<span style="color: #000000;"&gt; current;
          current </span>=<span style="color: #000000;"&gt; current. next;
      }
       </span><span style="color: #0000ff;"&gt;if</span>(current ==<span style="color: #000000;"&gt; first) {
           first </span>=<span style="color: #000000;"&gt; first. next;
      } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {
          prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s. next </span>=<span style="color: #000000;"&gt; current. next;
      }
       </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; current;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; <a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>出所有的节点信息</span>
 <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; displayAllNodes() {
      Node current </span>=<span style="color: #000000;"&gt; first;
       </span><span style="color: #0000ff;"&gt;while</span> (current != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) {
          current.display();
          current </span>=<span style="color: #000000;"&gt; current. next;
      }
      System. out.println();
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 根据位置查找节点信息</span>
 <span style="color: #0000ff;"&gt;public</span> Node findByPos( <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; index) {
      Node current </span>=<span style="color: #000000;"&gt; first;
       </span><span style="color: #0000ff;"&gt;if</span> ( pos !=<span style="color: #000000;"&gt; index) {
          current </span>=<span style="color: #000000;"&gt; current. next;
           pos</span>++<span style="color: #000000;"&gt;;
      }
       </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; current;
 }

 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 根据数据查找节点信息</span>
 <span style="color: #0000ff;"&gt;public</span> Node findByData( <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; data) {
      Node current </span>=<span style="color: #000000;"&gt; first;
       </span><span style="color: #0000ff;"&gt;while</span> (current. data !=<span style="color: #000000;"&gt; data) {
           </span><span style="color: #0000ff;"&gt;if</span> (current. next == <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
                </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;;
          current </span>=<span style="color: #000000;"&gt; current. next;
      }
       </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; 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);
}
}

参考原文  http://blog.csdn.net/tayanxunhua/article/details/11100097

原文链接:https://www.f2er.com/java/238779.html

猜你在找的Java相关文章