单链表之Java实现

初次接触java,用java也写了一个链表。代码如下:

  1 import java.io.*;
  2 
  3 class Node{
  4     public int data; //数据域
  5     public Node next; //指针域,全局变量可以不用初始化
  6     public Node(int data1){
  7         data = data1;
  8         next = null;
  9     }
 10     
 11     public void display(){
 12         System.out.println("data: "+this.data);
 13     }
 14 }
 15 
 16 
 17 //定义单链表
 18 class LinkList{
 19     public int pos = 0; //结点的位置
 20     public Node first; //头结点
 21     public LinkList(){
 22         Node first1 = new Node(0);
 23         first = first1;
 24     }
 25     
 26     
 27     //插入结点(从尾部插入结点)
 28     public void addNode(int data){
 29         if(first == null){
 30             Node node = new Node(data);
 31             first = node;
 32             node.next = null;
 33         }
 34         else{
 35             Node node = new Node(data);
 36             Node current = first;
 37             while(current.next != null)
 38                 current = current.next;
 39             current.next = node;
 40             node.next = null;
 41         }
 42     }
 43     
 44     //删除尾部的结点
 45     public Node deleteNode(){
 46         Node current = first; 
 47         Node pre = current;
 48         while(current.next != null){
 49             pre = current;
 50             current = current.next;
 51         }
 52         Node temp = pre.next;
 53         pre.next = null;
 54         return temp;
 55     }
 56     
 57     //在任意位置插入节点
 58     public void insertNode(int index,int data){
 59         Node node = new Node(data);
 60         Node current = first;
 61         while(pos != (index-1)){
 62             current = current.next;
 63             pos++;
 64         }
 65         node.next = current.next;
 66         current.next = node;
 67         pos = 0;
 68     }
 69     
 70     //删除任意位置的结点
 71     public Node deleteNode(int index){
 72         Node current = first;
 73         while(pos != (index-1)){
 74             current = current.next;
 75             pos++;
 76         }
 77         Node temp = current.next;
 78         current.next = (current.next).next;
 79         pos = 0;
 80         return temp;
 81     }
 82     
 83     //显示出所有结点信息
 84     public void displayAllNodes(){
 85         Node current = first;
 86         while(current.next != null){
 87             current.display();
 88             current = current.next;
 89         }
 90     }
 91     
 92     //根据位置查找结点信息
 93     public Node findByPos(int index){
 94         Node current = first;
 95         while(pos != index){
 96             current = current.next;
 97             pos++;
 98         }
 99         pos = 0;
100         return current;
101     }
102     
103     //根据数据查找结点信息
104     public Node findByData(int data){
105         Node current = first;
106         while(current.data != data){
107             if(current.next == null)
108                 return null;
109             current = current.next;
110             }
111         return current;
112     }
113     
114 }
115 
116 public class test{
117     public static void main(String[] args){
118         LinkList link = new LinkList();
119         link.addNode(1);
120         link.addNode(2);
121         link.addNode(3);
122         link.addNode(4);
123         link.addNode(5);
124         link.insertNode(1,1000);
125         link.displayAllNodes();
126     }
127 }

下面是运行结果:

如有错误,欢迎交流指正。

原文地址:https://www.cnblogs.com/jeavenwong/p/8080814.html