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

    package list;
    
    /**
     * 节点
     * 
     * @author DeepSleeping
     *
     */
    public class ListNode {
    
        Object data;
        ListNode next;
    
        public ListNode(Object data) {
            super();
            this.data = data;
        }
    }
    package list;
    
    /**
     * 单链表
     * 
     * @author DeepSleeping
     *
     */
    public class SingleList implements MyList {
    
        private ListNode first;
        private ListNode last;
        private int size;
        
        @Override
        public void add(Object element) {
            if (first == null) {
                first = new ListNode(element);
                last = first;
            }else{
                last.next = new ListNode(element);
                last = last.next;
            }
            size++;
        }
    
        @Override
        public void delete(Object element) {
            ListNode p = first;
            ListNode pre = null;
            while(p != null){
                if (p.data.equals(element)) {
                    //如果删除的是第一个元素
                    if (p == first) {
                        first = first.next;
                    }else{
                        pre.next = p.next;
                    }
                    break; //注意,删除了第一个相同的元素之后应该跳出循环
                }
                pre = p;
                p = p.next;
            }
            size--;
        }
    
        @Override
        public void delete(int index) {
            if (index < 0 || index >= size) {
                return;
            }
            int i = 0;//指针指向的节点的索引
            ListNode p = first;
            ListNode pre = null;
            while(p != null){
                if (i == index) {
                    if (p == first) {
                        first = first.next;
                    }
                    else{
                        pre.next = p.next;
                    }
                    break;
                }
                pre = p;
                p = p.next;
                i++;
            }
            size--;
        }
    
        @Override
        public void update(int index, Object newElement) {
            int i = 0;//指针指向的节点的索引
            ListNode p = first;
            while(p != null){
                if (i == index) {
                    p.data = newElement;
                }
                p = p.next;
                i++;
            }
        }
    
        @Override
        public boolean contains(Object target) {
            ListNode p = first;
            while(p != null){
                if (p.data.equals(target)) {
                    return true;
                }
                p = p.next;
            }
            return false;
        }
    
        @Override
        public Object at(int index) {
            if (index < 0 || index >= size) {
                return null;
            }
            int i = 0;//指针指向的节点的索引
            ListNode p = first;
            while(p != null){
                if (i == index) {
                    return p.data;
                }
                p = p.next;
                i++;
            }
            return null;
        }
    
        @Override
        public int indexOf(Object element) {
            int i = 0;
            ListNode p = first;
            
            while(p != null){
                if (p.data.equals(element)) {
                    return i;
                }
                p = p.next;
                i++;
            }
            return -1;
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("[");
            ListNode p = first;
            while(p != null){
                sb.append(p.data);
                if (p.next != null) {
                    sb.append(",");
                }
                
                p = p.next;    
            }
            sb.append("]");
            return sb.toString();
        }
    }
  • 相关阅读:
    python常识系列17-->利用Faker模块造测试数据
    python常识系列16-->python自带的Mock模块使用
    python常识系列15-->python利用xlrd处理合并单元格
    python常识系列14-->python通过jpype模块调用jar包
    杂七杂八的问题处理01--mac下的eclipse默认不提供代码联想功能
    httprunner踩坑记03-->debugtalk.py中的方法传参
    httprunner踩坑记02-->利用parameters参数进行参数化
    vue新建项目一直在downloading template转,最后超时
    vue图片加载出错显示默认占位图片
    修改input复选框样式
  • 原文地址:https://www.cnblogs.com/deepSleeping/p/10268089.html
Copyright © 2011-2022 走看看