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

Posted kuillldan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现单向链表的增删改查相关的知识,希望对你有一定的参考价值。

class Node
{
    public int val;
    public Node next;

    public Node(int val)
    {
        this.val = val;
    }
}


class ListHead
{
    public int count ;
    public Node next;

    public ListHead()
    {
        this.count = 0;
        this.next = null;
    }
}


class List
{
    public ListHead head;
    private Node current;

    public List()
    {
        this.head = new ListHead();
        this.current = null;
    }

    public void addNew(int val)
    {
        Node newNode = new Node(val);
        if(this.head.next == null)
            this.head.next = newNode;
        else
            this.current.next = newNode;

        this.current = newNode;
    }

    public boolean contains(int val)
    {
        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.val == val)
                return true;
        }


        return false;
    }

    public void replace(int val,int newVal)
    {
        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.val == val)
            {
                current.val = newVal;
                return;
            }
        }
    }

    public void replaceAll(int val, int newVal)
    { 
        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.val == val)
                current.val = newVal;
        }
    }

    public void remove(int val)
    {
        Node theOneToBeRemoved = null;
        if(this.head.next != null && this.head.next.val == val)
        {
            theOneToBeRemoved = this.head.next;
            this.head.next = theOneToBeRemoved.next;
            theOneToBeRemoved.next = null;
            return ;
        }

        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.next != null && current.next.val == val )
            {
                theOneToBeRemoved = current.next;
                current.next = theOneToBeRemoved.next;
                theOneToBeRemoved.next = null;
                return;
            }
        }
    }



    public void removeAll(int val)
    {
        Node theOneToBeRemoved = null;
        while(this.head.next != null && this.head.next.val == val)
        {
            theOneToBeRemoved = this.head.next;
            this.head.next = theOneToBeRemoved.next;
            theOneToBeRemoved.next = null;
        }


        for(Node current = this.head.next; current != null; current = current.next)
        {
            while(current.next != null && current.next.val == val)
            {
                theOneToBeRemoved = current.next;
                current.next = theOneToBeRemoved.next;
                theOneToBeRemoved.next = null;
            }
        }
    }

    public void clear()
    {
        Node theOneToBeRemoved = null;
        while(this.head.next != null)
        {
            theOneToBeRemoved = this.head.next;
            this.head.next = theOneToBeRemoved.next;
            theOneToBeRemoved.next = null;
        }
    }

    public void printList()
    {
        for(Node current = this.head.next; current != null; current = current.next)
        {
            System.out.print(current.val + " ");
        }

        System.out.println("\n=================================");
    }


}

public class  Hello
{ 
    public static void main(String[] args) 
    {
        List myList = new List();

        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(2);
        myList.addNew(1);
        myList.addNew(2);
        myList.addNew(1);
        myList.addNew(2);
        myList.addNew(7);
        myList.addNew(7);
        myList.addNew(8);
        myList.addNew(2);

        myList.printList();
        myList.replaceAll(2,9);
        myList.removeAll(7);
        
        myList.printList();
    } 
}

 

以上是关于Java实现单向链表的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

java实现单向链表

数据结构和算法--链表一之单向链表的简单实现

双向链表的增删改查C++完整实现

算法_链表篇

算法_链表篇

单向链表的增删查改