java之鏈表建立

來源:互聯網
上載者:User

java中雖沒有指標之說,但卻也能實現同指標一樣的功效。

///////////////節點//////////////////////class node{public double x;public node next;public node pre;node (double x,node n){this.x =x;this.pre =null;this.next =n;}node (double x){this.x =x;}public node getnext(){return next;}public void setnext(node next){this.next =next;}public node getpre(){return pre;}public void setpre(node pre){this.pre =pre;}/*public node(double x,node next){this.x=x; this.next =next;}*/}///////////////鏈表實現///////////////////////class list{public node head,tail;public int length;list(){head= tail =null;}public boolean isEmpty()//判斷是否為空白  {return head==null;}public void addhead(double x)// 加入head節點 一般調用一次{head=new node (x,null);//pt.setnext(head.getnext());//head.setnext(pt);if(tail==null) tail=head;}public void addtail(double x)// 往後 添加節點  {if(!isEmpty()){tail.next =new node(x);tail=tail.next ;}else {head=tail =new node(x);}}public void print(){for(node temp=head;temp!=null;temp=temp.next ){System.out.print(temp.x +" ");}System.out.println("");}public void addnow(double x,double y)//在特定的位置 插入一個節點{for(node temp=head;temp!=null;temp=temp.next ){if(temp.x ==x ){node now= temp.next ;node ne=new node(y);temp.next =ne;ne.next =now;}}}}//////////////////////////////////////////public class test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stublist k=new list();k.addtail(1);k.addtail(2);k.addtail(4);k.addtail(5);k.addnow(2, 3);//在2之後,插入為3的節點k.print();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.