• Java实现单向链表的增删改查


    class List<T>
    {
        private class Node
        {
            private T data;
            private Node next;
    
            private Node(T data)
            {
                if(data != null)
                    this.data = data;
            }
    
            private void add(T data)
            {
                if(this.next == null)
                    this.next = new Node(data);
                else
                    this.next.add(data);
            }
    
            private void remove(Node previous, int index)
            {
                if(List.this.foot++ == index)
                {
                    previous.next = this.next;
                    this.next = null;
                    List.this.count--;
                    return;
                }
                else
                {
                    this.next.remove(this,index);
                }
            }
    
            private void remove(Node previous, T data)
            {
                if(this.data.equals(data))
                {
                    previous.next = this.next;
                    this.next = null;
                    List.this.count--;
                    return;
                }
                
                
                if(this.next != null)
                    this.next.remove(this,data);
                else
                    return ;
            }
    
            private void replace(int index, T data)
            {
                if(List.this.foot++ == index)
                    this.data = data;
                else
                    this.next.replace(index,data);
            }
    
            private void replace(T oldData, T newData)
            {
                if(this.data.equals(oldData))
                    this.data = newData;
                else
                    this.next.replace(oldData,newData);
            }
    
            private T get(int index)
            {
                if(List.this.foot++ == index)
                    return this.data;
                else
                    return this.next.get(index);
            }
    
            private boolean contains(T data)
            {
                if(this.data.equals(data))
                    return true;
                if(this.next != null)
                    return this.next.contains(data);
                else
                    return false;
            }
        }
    
    
    //===============================================
        private Node root;
        private int count;
        private int foot;
    
    
        public List()
        {}
    
        public boolean isEmpty()
        {
            if(this.count == 0 && this.root == null)
                return true;
            else
                return false;
        }
    
        public int size()
        {
            return this.count;
        }
    
        public void add(T data)
        {
            if(this.isEmpty())
                this.root = new Node(data);
            else
                this.root.add(data);
    
    
            this.count++;
        }
    
        public void remove(int index)
        {
            if(this.isEmpty())
                return;
    
            if(index < 0 || this.count <= index)
                return;
    
            if(index == 0)
            {
                Node temp = this.root;
                this.root = this.root.next;
                temp.next = null;
                this.count--;
                return;
            }
            else
            {
                this.foot = 0;
                this.root.remove(this.root, index);
            }
        }
    
        public void remove(T data)
        {
            if(this.isEmpty())
                return ;
    
            if(this.root.data.equals(data))
            {
                Node temp = this.root;
                this.root = this.root.next;
                temp.next = null;
                this.count--;
                return;
            }
            else
            {
                this.root.next.remove(this.root, data);
            }
        }
    
        public void replace(int index, T data)
        {
            if(this.isEmpty())
                return;
    
            if(index < 0 || this.count<=index)
                return;
    
            this.foot = 0;
            this.root.replace(index,data);
        }
    
        public void replace(T oldData, T newData)
        {
            if(this.isEmpty())
                return;
            
            this.root.replace(oldData,newData);
        }
    
        public T get(int index)
        {
            if(this.isEmpty())
                return null;
    
            this.foot = 0;
            return this.root.get(index);
        }
    
        public boolean contains(T data)
        {
            if(this.isEmpty())
                return false;
    
            return this.root.contains(data);
        }
    
        public Object[] toArray()
        {
            if(this.isEmpty())
                return null;
            
            int count = this.count;
            Object[] retVal = new Object[count];
            for(int i = 0; i < count; i++)
            {
                retVal[i] = this.get(i);
            }
            return retVal;
        }
    }
    
    public class Hello
    {
        public static void main(String[] args)  throws Exception
        {
            List<String> myList = new List<String>();
            myList.add("Hello");
            myList.add("world");
            myList.add("Ni");
            myList.add("Hao");
            myList.add("HeHe");
    
            System.out.println(myList.contains(null));
    
    
    
            Object[] result = myList.toArray();
    
            
            for(Object item : result)
            {
                String temp = (String)item;
                System.out.println(temp);
            }
        }
    }
  • 相关阅读:
    cat userlist
    002 webpack基础 webpack配置
    003 webpack基础 webpack处理样式资源
    010Node事件循环
    001 webpack基础 简介
    001简介
    大数定律(Law of Large Numbers)
    Linux中nohup和&的用法和区别
    终于有人把ROS机器人操作系统讲明白了
    轻松使用 ffmpeg sdk 实现各种格式的rgb以及yuv raw data文件的转换
  • 原文地址:https://www.cnblogs.com/kuillldan/p/5572262.html
Copyright © 2020-2023  润新知