• 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);
            }
        }
    }
  • 相关阅读:
    实验四
    实验三
    实验二
    实验一
    6
    5
    4
    3
    shiyan2
    实验1
  • 原文地址:https://www.cnblogs.com/kuillldan/p/5572262.html
Copyright © 2020-2023  润新知