用java實現單鏈表(菜鳥出征),java單鏈

來源:互聯網
上載者:User

用java實現單鏈表(菜鳥出征),java單鏈

package code;class Node{    Node next;    int data;    public Node(int data)    {        this.data=data;    }    }class LinkList{    Node first;    //頭部    public LinkList()    {        this.first=null;    }    public void addNode(Node no)    {        no.next=first;        first=no;//在頭部添加    }    public void delectNode()    {        Node n=first.next;        first=null;        first=n;//在頭部刪除    }    //刪除指定位置    public int Number()    {        int count=1;        //查看有多少元素        Node nd=first;        while(nd.next!=null)        {            nd=nd.next;            count++;        }        return count;    }    public void delectExact(int n)    {        //刪除指定位置        if(n>1)        {            int count=1;            Node de=first;            while(count<n-1)            {                de=de.next;                count++;                            }            de.next=de.next.next;        }        else            first=first.next;            }    public void addExact(int n,Node nd)    {        if(n>1)//添加指定位置        {            int count=1;            Node de=first;            while(count<n-1)            {                de=de.next;                count++;                            }            nd.next=de.next;            de.next=nd;        }        else            first=first.next;    }    public int  findNode(int n)    {        int count=1;//尋找一個數對應的位置        Node de=first;        while(de.data!=n)        {            de=de.next;            count++;            if(de==null)            {                return -1;            }        }        return count;    }    public void print()    {        Node no=first;//列印所有        while(no!=null)        {            System.out.println(no.data);            no=no.next;        }    }}public class TextNode{    public static void main(String[] args)    {        LinkList ll=new LinkList();        ll.addNode(new Node(12));        ll.addNode(new Node(15));        ll.addNode(new Node(18));        ll.addNode(new Node(19));        ll.addNode(new Node(20));        /*System.out.println(ll.first.data);        ll.delectNode();        System.out.println(ll.first.data);*/        System.out.println(ll.Number());        ll.delectExact(3);        ll.addExact(3, new Node(100));        System.out.println(ll.Number());//        ll.print();        System.out.println(ll.findNode(112));            }}

大一菜鳥初學,有誤之處請諒解。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.