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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

list(LinkedList).head 和 Node head 的區(qū)別?

list(LinkedList).head 和 Node head 的區(qū)別?

智慧大石 2023-04-26 17:03:20
我試圖使用 Node head 刪除鏈接列表的第一個(gè)節(jié)點(diǎn),但是當(dāng)我使用 list.head 時(shí)它不起作用?import java.util.*;    // Java program to implement     // a Singly Linked List     public class LinkedList {         Node head;     // head of list         // Linked list Node.         // This inner class is made static         // so that main() can access it         static class Node {             int data;             Node next;             // Constructor             Node(int d)             {                 data = d;                 next = null;             }         }          static void delete(LinkedList list,int x){            Node curr=list.head,prev=list.head;            if(curr.data==x&&curr!=null){                list.head=curr.next;                return ;            }            while(curr.data!=x&&curr.next!=null){                prev=curr;                curr=curr.next;            }            if(curr.data==x)                prev.next=curr.next;            return ;        }            // There is method 'insert' to insert a new node         // Driver code         public static void main(String[] args)         {             /* Start with the empty list. */            LinkedList list = new LinkedList();             list = insert(list, 1);             list = insert(list, 2);             list = insert(list, 3);                    list = insert(list, 4);                    delete(list,1);                    printList(list);                    //There is method to print list        }     }     //Output : 2 3 4當(dāng)我使用上面的代碼時(shí),我可以刪除第一個(gè)節(jié)點(diǎn),但是當(dāng)我使用這段代碼時(shí),它不起作用import java.util.*;// Java program to implement // a Singly Linked List public class LinkedList {     Node head; // head of list     // Linked list Node.     // This inner class is made static     // so that main() can access it 我想知道這些是相同的東西是不同的,Node head 和 list(LinkedList).head注意:這兩種方法都適用于其他節(jié)點(diǎn),區(qū)別僅適用于第一個(gè)節(jié)點(diǎn)。
查看完整描述

1 回答

?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

在第一個(gè)中,您將列表作為輸入傳遞,在第二個(gè)中,您將引用您的頭節(jié)點(diǎn),如果您在第一個(gè)示例中注意到,如果數(shù)據(jù)存在于第一個(gè)節(jié)點(diǎn),您正在修改列表的頭。這是執(zhí)行此操作的代碼片段。


 Node curr=list.head,prev=list.head;

            if(curr.data==x&&curr!=null){

                list.head=curr.next;

                return ;

            }

但是在您的第二個(gè)示例中,如果在第一個(gè)節(jié)點(diǎn)找到數(shù)據(jù),那么您將分配curr.next給方法本地的 head 變量,因此列表的 head 值保持不變,當(dāng)您再次嘗試在 main 方法中打印列表時(shí),它顯示舊的 head。這是第二個(gè)示例的代碼片段


Node curr=head,prev=head;

        if(curr.data==x&&curr!=null){

           head=curr.next;

            return ;

        }

因此,如果您將頭指針存儲(chǔ)在 LinkedList 對(duì)象中,那么您必須修改其中的值。


查看完整回答
反對(duì) 回復(fù) 2023-04-26
  • 1 回答
  • 0 關(guān)注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)