数据结构之单链表(java实现)

数据结构之单链表(java实现)

单链表是一种查询满增删快的数据结构。本文实现了数据结构中单链表结构。实现了单链表中的添加、读取、插入、删除和打印常见操作。
代码:

结点类:

public class Node {//创建Node类,成员变量包含数据和next node;
     public Object data;
     public Node next = null;

    public Node(Object data){
        this.data = data;
    }
}

链表类:

public class SingleLinkedList {
    private int size = 0;
    private Node head = null;
    public void AddNode(Object data) {//添加
        Node NewNode = new Node(data);
        if (head == null) {
            head = NewNode;
            size++;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = NewNode;
        size++;
    }
    public Object Get(int index)throws Exception{//读取
        if (index<1||index>size){
            throw new Exception("error!");
        }
        int i = 1;
        Node CurNode = head;
        while(index != i){
            CurNode = CurNode.next;
            i++;
        }
        return CurNode.data;
    }
    public void insert(int index,Object elem) throws Exception {//插入
        if(index<1||index>size)
            throw new Exception("error!");
        if(index == 1){
            Node NewNode = new Node(elem);
            NewNode.next = head;
            head = NewNode;
            size++;
        }
        int i = 2;
        Node PreNode = head;
        Node CurNode = PreNode.next;
        while(CurNode != null){
            if(i == index){
                Node NewNode = new Node(elem);
                NewNode.next = CurNode;
                PreNode.next = NewNode;
            }
            PreNode = PreNode.next;
            CurNode = CurNode.next;
            i++;
        }
    }
    public void Delet(int index)throws Exception{//删除
        if(index<1||index>size)
            throw new Exception("error!");
        Node CurNode = head;
        Node BehindNode = CurNode.next;
        int i = 1;
        while(CurNode != null){
            if(index == i){
                CurNode.next=BehindNode.next ;
                size--;
            }
            CurNode= CurNode.next ;
            BehindNode = BehindNode.next ;
            i++;
        }
    }
    public void display(){//打印
        Node CuNode = head;
        System.out.println("CurrentSize: "+size);
        while(CuNode != null){
            System.out.print(CuNode.data+" ");
            CuNode = CuNode.next;
        }
        System.out.println();
    }
}

测试类:

**public class test {
    public static void main(String[] args) throws Exception{
        SingleLinkedList LinkedList = new SingleLinkedList();
       /*********
		执行操作
       *********/
    }
}**
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值