Java实现单链表

今天把数据结构的书翻了翻,以前没想过用java实现链表玩玩,现在针对单链表进行简单的实现以及测试,小白代码比较粗糙


package cn.dtstructure;

/*
 * 单链表
 * 外部类可以直接访问内部类的私有成员变量
 * */

//定义泛型类(节点数据类型) 以便扩展
public class SingleLinkList<E> {

    private Node head_point;//头指针
    private Node head_node;//头节点
    private Node rear;//尾指针
    private Node point;//临时指针
    private Integer length;//长度
    
    private class Node{
        private E data;//数据域
        private Node next;//指针域
        //未带参数的构造函数用于初始化头结点
        public  Node(){
            this.data = null;
        }
        //带参数的构造函数用于初始化节点数据
        public Node(E data){
            this.data = data;
        }
    }
    
    //初始化链表 只创建不含数据的头结点
    public SingleLinkList(){        
        this.head_point = new Node();//头指针
        this.head_node = new Node();//头结点
        this.rear = head_node;
        //头结点指向null
        head_node.next = null;
        this.length = 0;    
        System.out.println("链表进行初始化");
        show();
    }
    
    //从尾部添加节点 传入数据
    public void insert(E data){
        //临时指针指向新建节点
        point = new Node(data);
        //尾指针的下一指针指向新节点
        rear.next = point;
        //尾指针指向新节点
        rear = point;
        point.next = null;
        length++;
    }
    
    //在指定位置添加节点 默认第一个节点位置为1
    public void insertInPosition(E data,Integer position){
        if (position>=1 && position<=length) {
            //将临时指针移动到操作位置的前一个位置
            move(position);    
            Node tmp = new Node(data);
            tmp.next = point.next;
            point.next = tmp;
            length++;
        }else if (position == length+1) {
            insert(data);
        }else {
            System.out.println("插入位置不正确");
        }
        
    }
    
    //删除指定位置节点
    public void delete(Integer position){
        if (position>=1 && position<=length) {
            //将临时指针移动到操作位置的前一个位置
            move(position);    
            //若是删除最后一个节点 则point.next = null;
            point.next = point.next.next;
            //判断最后一个节点是否改变 若改变移动尾指针到对应位置
            if (point.next == null) {
                rear = point;
            }
            length--;
        }else{
            System.out.println("删除位置不正确");
        }

    }
    
    //修改指定位置节点
    public void update(Integer position,E data){
        if (position>=1 && position<=length) {
            //将临时指针移动到操作位置的前一个位置
            move(position);        
            point.next.data = data;
        }else{
            System.out.println("更新位置不正确");
        }
    }
    
    //查询指定位置节点
    public void select(Integer position){
        if (position>=1 && position<=length) {
            //将临时指针移动到操作位置的前一个位置
            move(position);        
            System.out.println("当前位置节点的数据为:"+point.next.data);
        }else{
            System.out.println("查询位置不正确");
        }
    }
    
    //显示链表当前信息
    public void show(){
        String result="head_point->head_node->";
        point = head_node;
        while(point.next!=null){
            result = result + point.next.data + "->";
            point = point.next;
        }
        result += "null";
//        result += point.next.data;
        System.out.println(result);
    }
    
    //将临时指针移动到带操作节点的前一个位置
    public void move(Integer position){
        point = head_node;
        while(--position>0){
            point = point.next;
        }
    }
    
    
    
}



测试代码

package cn.dtstructure;

public class SingleLinkListTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SingleLinkList<Integer> singleLinkList = new SingleLinkList<>();
         
        //在末尾增加节点
        singleLinkList.insert(2);
        System.out.println("insert(2)");
        singleLinkList.show();
        
        singleLinkList.insert(3);
        System.out.println("insert(3)");
        singleLinkList.show();
        
        singleLinkList.insert(5);
        System.out.println("insert(5)");
        singleLinkList.show();    
        
        //在指定位置增加节点
        singleLinkList.insertInPosition(1, 1);
        System.out.println("insertInPosition(1, 1)");
        singleLinkList.show();
        
        singleLinkList.insertInPosition(4, 4);
        System.out.println("insertInPosition(4, 4)");
        singleLinkList.show();
        
        singleLinkList.insertInPosition(6, 6);
        System.out.println("insertInPosition(6, 6)");
        singleLinkList.show();
        
        //在指定位置删除节点
        singleLinkList.delete(1);
        System.out.println("delete(1)");
        singleLinkList.show();
        
        singleLinkList.delete(3);
        System.out.println("delete(3)");
        singleLinkList.show();
        
        singleLinkList.delete(4);
        System.out.println("delete(4)");
        singleLinkList.show();
        
        //在指定位置更新节点
        singleLinkList.update(1, 1);
        System.out.println("update(1, 1)");
        singleLinkList.show();
        
        singleLinkList.update(2, 2);
        System.out.println("update(2, 2)");
        singleLinkList.show();
        
        singleLinkList.update(3, 3);
        System.out.println("update(3, 3)");
        singleLinkList.show();
        
        //查询指定位置节点
        singleLinkList.select(1);
        System.out.println("select(1)");
        singleLinkList.show();
        
        singleLinkList.select(2);
        System.out.println("select(2)");
        singleLinkList.show();
        
        singleLinkList.select(3);
        System.out.println("select(3)");
        singleLinkList.show();
        
        //判错
        System.out.println("select(4)");
        singleLinkList.select(4);
        
        System.out.println("insertInPosition(5, 5)");
        singleLinkList.insertInPosition(5, 5);
        
        System.out.println("delete(4)");
        singleLinkList.delete(4);
        
        System.out.println("update(0, 0)");
        singleLinkList.update(0, 0);
        
        System.out.println("*****最后结果*****");
        singleLinkList.show();
    }

}





输出结果

链表进行初始化
head_point->head_node->null
insert(2)
head_point->head_node->2->null
insert(3)
head_point->head_node->2->3->null
insert(5)
head_point->head_node->2->3->5->null
insertInPosition(1, 1)
head_point->head_node->1->2->3->5->null
insertInPosition(4, 4)
head_point->head_node->1->2->3->4->5->null
insertInPosition(6, 6)
head_point->head_node->1->2->3->4->5->6->null
delete(1)
head_point->head_node->2->3->4->5->6->null
delete(3)
head_point->head_node->2->3->5->6->null
delete(4)
head_point->head_node->2->3->5->null
update(1, 1)
head_point->head_node->1->3->5->null
update(2, 2)
head_point->head_node->1->2->5->null
update(3, 3)
head_point->head_node->1->2->3->null
当前位置节点的数据为:1
select(1)
head_point->head_node->1->2->3->null
当前位置节点的数据为:2
select(2)
head_point->head_node->1->2->3->null
当前位置节点的数据为:3
select(3)
head_point->head_node->1->2->3->null
select(4)
查询位置不正确
insertInPosition(5, 5)
插入位置不正确
delete(4)
删除位置不正确
update(0, 0)
更新位置不正确
*****最后结果*****
head_point->head_node->1->2->3->null




猜你喜欢

转载自blog.csdn.net/qq646040754/article/details/79719809