集册 Java实例教程 用insert和delete函数实现链表

用insert和delete函数实现链表

欢马劈雪     最近更新时间:2020-01-02 10:19:05

611
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
用insert和delete函数实现链表

class Linked_List

{//from n o w    j a v a  . c o m

    node head;


    static class node

    {

        int data;

        node next;


        // Constructor

        node(int d)

        {

            data = d;

            next = null;

        }

    }


    public boolean Is_List_Empty()

    {

        if(head == null)

            return true;
/*from 时 代 J a v a 公 众 号 - N o w J a v  a . c o m*/

        return false;

    }


    public void Insert_At_Beginning(int value)

    {

        node temp = new node(value);

        temp.next = head;

        head = temp;

    }


    public void Insert_At_End(int value)

    {

        node temp = new node(value), current = head;

        temp.next = null;


        if(Is_List_Empty())

        {

            head = temp;

            return ;

        }


        while(current.next != null)

            current = current.next;


        current.next = temp;

    }


    public void Insert_After_Value(int desired, int value)

    {

        node current = head;


        while(current != null && current.data != desired)

            current = current.next;


        if(current == null)

            System.out.println("Element " + desired + " is not in list");

        else

        {

            node temp = new node(value);

            temp.next = current.next;

            current.next = temp;

        }

    }


    public void Delete_At_Beginning()

    {

        if(Is_List_Empty())

            System.out.println("List is empty");

        else

        {

            node temp = head;

            head = head.next;

            temp.next = null;

        }

    }


    public void Delete_At_End()

    {

        if(Is_List_Empty())

        {

            System.out.println("List is empty");

            return ;

        }


        node temp = head, prev = null;


        if(head.next == null)

        {

            head = null;

            temp.next = null;

            return ;

        }


        while(temp.next != null)

        {

            prev = temp;

            temp = temp.next;

        }


        prev.next = temp.next;

        temp.next = null;

    }


    public void Delete_With_Value(int desired)

    {

        if(Is_List_Empty())

        {

            System.out.println("List is empty");

            return ;

        }


        node temp = head, prev = null;


        if(head.data == desired)

        {

            head = head.next;

            temp.next = null;

            return ;

        }


        while(temp != null && temp.data != desired)

        {

            prev = temp;

            temp = temp.next;

        }


        if(temp == null)

            System.out.println("Element " + desired + " is not in list");

        else

        {

            prev.next = temp.next;

            temp.next = null;

        }

    }


    public void Search(int desired)

    {

        node temp = head;


        while(temp != null && temp.data != desired)

            temp = temp.next;


        if(temp == null)

            System.out.println("Element " + desired + " not found");

        else

            System.out.println("Element " + desired + " is present in list");

    }


    public void Print_Linked_List()

    {

        if(Is_List_Empty())

        {

            System.out.println("List is empty");

            return ;

        }


        node current = head;


        while(current.next != null)

        {

            System.out.print(current.data + " -> ");

            current = current.next;

        }


        System.out.println(current.data);

    }


    public static void main(String[] args)

    {

        Linked_List LinkedList = new Linked_List();


        int i;


        for(i = 0; i <
展开阅读全文