第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

求解,關于單向鏈表的問題。

求解,關于單向鏈表的問題。

Beaten丶 2016-01-11 17:00:57
class Link{ // 鏈表的完成類 class Node{ // 保存每一個節(jié)點,此處為了方便直接定義成內部類 private String data ; // 保存節(jié)點的內容 private Node next ; // 保存下一個節(jié)點 public Node(String data){ this.data = data ; // 通過構造方法設置節(jié)點內容 } public void add(Node newNode){ // 將節(jié)點加入到合適的位置 if(this.next==null){ // 如果下一個節(jié)點為空,則把新節(jié)點設置在next的位置上 this.next = newNode ; }else{ // 如果不為空,則需要向下繼續(xù)找next this.next.add(newNode) ; } } public void print(){ System.out.print(this.data + "\t") ; // 輸出節(jié)點內容 if(this.next!=null){ // 還有下一個元素,需要繼續(xù)輸出 this.next.print() ; // 下一個節(jié)點繼續(xù)調用print } } }; private Node root ; // 鏈表中必然存在一個根節(jié)點 public void addNode(String data){ // 增加節(jié)點 Node newNode = new Node(data) ; // 定義新的節(jié)點 if(this.root==null){ // 沒有根節(jié)點 this.root = newNode ; // 將第一個節(jié)點設置成根節(jié)點 }else{ // 不是根節(jié)點,放到最后一個節(jié)點之后 this.root.add(newNode) ; // 通過Node自動安排此節(jié)點放的位置 } } public void printNode(){ // 輸出全部的鏈表內容 if(this.root!=null){ // 如果根元素不為空 this.root.print() ; // 調用Node類中的輸出操作 } } };public class LinkDemo02{ public static void main(String args[]){ Link l = new Link() ; l.addNode("A") ; // 增加節(jié)點 l.addNode("B") ; // 增加節(jié)點 l.addNode("C") ; // 增加節(jié)點 l.addNode("D") ; // 增加節(jié)點 l.addNode("E") ; // 增加節(jié)點 System.out.println("======= 刪除之前 ========") ; l.printNode() ; }};代碼中的? this.next.add(newNode);this.next.print();是什么意思?。?
查看完整描述

3 回答

已采納
?
Its_forever

TA貢獻361條經(jīng)驗 獲得超328個贊

http://img1.sycdn.imooc.com//56937f7200017e5007290424.jpg

我調試了一下你的代碼。你的代碼的執(zhí)行結構是這樣的。

56937fe800017fad05000096.jpg

其中的next結構如下:

56937fe80001b4ab05000154.jpg

每個next中都有一個next結構。

if(this.next==null){	//?如果下一個節(jié)點為空,則把新節(jié)點設置在next的位置上
this.next?=?newNode?;
}else{	//?如果不為空,則需要向下繼續(xù)找next
this.next.add(newNode)?;
}

所以this.next.add(newNode);就是說(這個還不好描述呢)next位置上的節(jié)點有數(shù)據(jù),那么他就繼續(xù)往下找next,然后把你傳過來的數(shù)據(jù),添加到一個新節(jié)點上。(這個新節(jié)點里面又有一個next,這大概就是單向鏈表的思想吧,猜測)。

查看完整回答
2 反對 回復 2016-01-11
?
任性的源代碼

TA貢獻2條經(jīng)驗 獲得超0個贊

單向鏈表,簡單講其實就是 判斷有沒有根節(jié)點 沒有就添加? 有就一路next

this.next.add(newNode);? 就是個遞歸調用? 簡單點講就是如果有下一個元素 就循環(huán)添加下一個元素

這里的this跟樓上說的一樣,就是當前節(jié)點的意思,也可以理解為下一個節(jié)點的上一個節(jié)點

this.next.print(); 同理

查看完整回答
反對 回復 2016-01-11
?
Its_forever

TA貢獻361條經(jīng)驗 獲得超328個贊

this關鍵字,誰調用它就代表誰,你的程序里面的this.next.add(newNode);this.next.print();中的this都是代表的l(Link l = new Link() )對象。

查看完整回答
反對 回復 2016-01-11
  • Beaten丶
    Beaten丶
    this.next.add(newNode);代表的意思是什么呢?
  • 3 回答
  • 1 關注
  • 1624 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號