码迷,mamicode.com
首页 > 编程语言 > 详细

JAVAA数据结构实现

时间:2018-10-30 17:18:49      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:span   public   linked   头部   ISE   list   设定   指针   添加   

JAVA单链表实现

#单链表的数据结构
public singleLinkedList(){
    private int size;
    private Node head;
    
    public singleLInkedList(){
        this.size = 0;
        this.head = Null;
    }
    private class Node(Object data){
        Object data;
        Node next;
        public Node(Object data){
            this.data = data;
        }
        
    }
    //添加头节点
    public Object addHeadNode(Object data){
        Node newHead = new Node(data);
        if(size == 0) head = newHead;
        else{
            newHead.next = head;
            head = neaHead;
        }
        size++;
        return data;
    }
    //在头部删除元素
    public Object deleteHeadNode(){
        Object data = head.data;
        head = head.next;
        size--;
        return data;
    }
    //查找指定元素,找到了返回Node,找不到返回Null
    public Node find(Object data){
        //设定指针遍历
        Node current = head;
        while(current != null){
            if(current.next.data == data) return curren
            else current = current.next;
        }
        return null;
    }
    //删除指定的元素,删除成功返回true
    public boolean delete(Object data){
        Node curren = head;
        while(current.next.data!=data&&current.next!=null){
            current = current.next;
        }
        if(current.next != null){
            current.next = current.next.next;
            size--;
            return true;
        }
        else return false;
    }
    //判断链表是否未空
    public boolean isEmpty(){
        if(size>0) return false;
        else return true;
    }
}

 

JAVAA数据结构实现

标签:span   public   linked   头部   ISE   list   设定   指针   添加   

原文地址:https://www.cnblogs.com/jellyj/p/9876668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!