亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用java寫的單向鏈表,第一個數據元素節點無法插入??!

用java寫的單向鏈表,第一個數據元素節點無法插入?。?/h1>
public?class?Node?{ Object?element; Node?next; public?Node(Object?element){ this.element=element; } public?Node(Object?element,?Node?next)?{ super(); this.element?=?element; this.next?=?next; } public?Object?getElement()?{ return?element; } public?void?setElement(Object?element)?{ this.element?=?element; } public?Node?getNext()?{ return?next; } public?void?setNext(Node?next)?{ this.next?=?next; } }public?class?LinList?{ private?Node?head;//頭指針 private?Node?current;//當前指針 private?int?size;//單鏈表的長度 public?LinList(){ head=current=new?Node(null); size=0; } public?void?index(int?i){//移動current指針的方法 if(i<-1||i>size-1){ System.out.println("paraments?is?wrong"); } if(i==-1){ return; } current=head.getNext(); for(int?j=0;current!=null&&j<i;j++){ current=current.getNext(); } } public?void?insert(int?a,Object?obj){ if(a<0||a>size){ System.out.println("your?position?isn't?right"); } index(a-1);//a-1的范圍是【-1,size-1】 current.setNext(new?Node(obj,current.getNext())); size++; } public?void?Show(){ current=head.getNext(); while(current!=null){ System.out.print(current.getElement()+"?"); current=current.getNext(); } } public?static?void?main(String[]?args){ LinList?ll=new?LinList(); Node?n=new?Node(50,null);//<<--遍歷出來的第一個數就是這里的50,但insert(0,0)沒有反應 ll.head=new?Node(null,n); ll.insert(0,?0); ll.insert(1,?1); ll.insert(2,?2); ll.insert(3,?3); ll.insert(4,?4); ll.Show(); } }show()的結果:50 1 2 3 4?但我插入的是1 2 3 4 ?為什么第一個數沒法替代呢?是不是書上的例子本身就有這個缺點呢?
查看完整描述

1 回答

?
肆意先生

TA貢獻14條經驗 獲得超9個贊

題主用IDE編輯的時候沒報錯嗎?LinList的head成員是private的,理論上不能直接操控...

查看完整回答
1 反對 回復 2016-10-11
  • 1 回答
  • 0 關注
  • 1869 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號