zoukankan      html  css  js  c++  java
  • Java|单向链表的实现

    package testOffer.linkedList;
    
    import org.w3c.dom.Node;
    
    public class SingleLinkedList {
    
        //测试用例
        public static void main(String[] args) {
            SingleLinkedList singleLinkedList = new SingleLinkedList();
            singleLinkedList.addHead("A");
            singleLinkedList.addHead("B");
            singleLinkedList.display();
        }
        
        
        /********实现代码**********/
    
        private int size; //链表节点的个数
        private Node head; //头节点
    
        public SingleLinkedList(){
            size = 0;
            head = null;
        }
    
        //链表的每个节点类
        private class Node{
            private Object data; //每个节点的数据
            private Node next; //每个节点指向下一个节点的连接
    
            public Node(Object data){
                this.data = data;
            }
        }
    
        //在链表头添加元素
        private Object addHead(Object obj){
            Node newHead = new Node(obj);
            if (size == 0){
                head = newHead;
            }else {
                newHead.next = head;
                head = newHead;
            }
            size++;
            return obj;
        }
    
        //在链表头删除元素
        private Object deleteHead(){
            Object obj = head.data;
            head = head.next;
            size--;
            return obj;
        }
    
        //查找指定元素,找到了返回节点Node,找不到返回null
        public Node find(Object obj){
            Node current = head;
            int tempSize = size;
            while(tempSize > 0){
                if (obj.equals(current.data)){
                    return current;
                }else {
                    current = current.next;
                }
                tempSize--;
            }
            return null;
        }
    
        //删除指定元素,删除成功返回true
        public boolean delete(Object value){
            if (size == 0)  return false;
            Node current = head;
            Node previous = head;
            while(current.data!=value){
                if (current.next == null ) return false;
                else {
                    previous = current;
                    current = current.next;
                }
            }
    
            //如果删除的节点是第一个节点
            if (current == head ){
                head = current.next;
                size--;
            }else {
                //删除的不是第一个
                previous.next = current.next;
                size--;
            }
            return true;
        }
    
        //判断链表是否为空
        public boolean isEmpty(){
            return (size==0);
        }
    
        //显示节点信息
        public void display(){
            if (size>0){
                Node node = head;
                int tempSize = size;
                if (tempSize==1){
                    System.out.print("["+node.data+"]");
                    return;
                }
                while(tempSize>0){
                    if (node.equals(head)){
                        System.out.print("["+node.data+"->");
                    }else if (node.next == null){
                        System.out.print(node.data+"]");
                    }else {
                        System.out.print(node.data+"->");
                    }
                    node = node.next;
                    tempSize--;
                }
                System.out.println();
            }else {
                System.out.println("[]");
            }
        }
    }
  • 相关阅读:
    如何通过setTimeout理解JS运行机制详解
    css3系列之伪类选择器
    css3系列之属性选择器
    css3系列之伪元素选择器
    css3系列之兄弟选择器
    作为了解系列之 预处理器 和 后处理器
    网络系列之跨域
    网络系列之 cookie增删改查(封装)
    网络系列之 jsonp 百度联想词
    网络系列之 什么是ajax 封装ajax
  • 原文地址:https://www.cnblogs.com/jlutiger/p/10647213.html
Copyright © 2011-2022 走看看