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

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

給定鏈表中的特定節點時如何使用 insertAfter()

給定鏈表中的特定節點時如何使用 insertAfter()

米脂 2022-06-23 16:32:02
// insert a new data after the given onepublic void insertAfter(int givenData, int newData){   // Your code here   Node previous = new Node(givenData);    if (previous == null) {        //System.out.println("The given previous node cannot be null.");        return;    }    Node newNode = new Node(newData);    newNode.next = previous.next;    previous.next = newNode;}// Removes the Node with the given datapublic void remove(int current) {  // Your code here  Node previous = new Node(current);    int count = 1;    int position = 0;    while (count < position -1) {        previous = previous.next;        count++;    }    Node curNode = previous.next;    previous.next = curNode.next;    curNode.next = null;}此代碼沒有給出任何錯誤消息,但不會添加或刪除節點。我相信我的困惑在于int givenData以及如果它不是節點如何訪問它。第一次在這里發帖,希望我提供了足夠的信息:)public class LinkedListTemplate {Node head;// inserts data to the end of the list only using the head pointerpublic void append(int data){    Node newNode = new Node(data);    if(head == null){                   head = newNode;    } else {                    Node currentNode = head;        while(currentNode.next != null){            currentNode = currentNode.next;        }        currentNode.next = newNode;                 }}// inserts data to the beginning of the listpublic void prepend(int data){    if(head == null){        Node newNode = new Node(data);        head = newNode;        return;    }    Node newNode = new Node(data);    newNode.next = head;    head = newNode;}// print the linked list elementspublic void print() {    Node currentNode = head;    System.out.printf("[");    while (currentNode.next != null) {        System.out.printf("%d, ", currentNode.data);        currentNode = currentNode.next;    }    System.out.printf("%d]%n", currentNode.data);}我包含了其余的鏈表代碼,還有另一個類可以測試列表并允許它運行。
查看完整描述

3 回答

?
翻翻過去那場雪

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

將插入方法更改為:


public void insertAfter(int givenData, int newData) {

    Node n = head;

    while (n != null && n.data != givenData) {

        n = n.next;

    }

    if (n != null) return;

    Node newNode = new Node(newData);

    newNode.next = n.next;

    n.next = newNode;

}

并使用以下方法刪除方法:


public void remove(int current) {


    Node prev = head;

    if(head == null) return;

    if(head.data == current){head = null; return;}

    Node n = prev.next;

    while( n != null && n.data != current){

      prev = n;

      n = n.next;   

    }

    if (n == null) return;

    prev.next = n.next;


}



查看完整回答
反對 回復 2022-06-23
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

好的,您可能需要稍微增強您的代碼。首先,你需要一個清單。所以讓我們從那個開始。


public class MyLinkedList {

    public Node *head = NULL;

}

head 指向列表的前面。


現在, insertAfter 的基本算法很簡單:


查找包含“插入位置”值的節點

創建一個新節點并在之后插入。

可愛吧?


void insertAfter(int beforeData, int valueToInsert) {

    Node *ptr = head;

    Node *lastPtr = NULL;

    while (ptr != NULL && ptr->value != beforeData) {

        lastPtr = ptr;

        ptr = ptr->next;

    }


    // At this point, ptr COULD be null, if the data is never found.

    // Insertion is at the end, and lastPtr helps with that.

    Node * newNode = new Node(valueToInsert);

    if (ptr == NULL) {

        # At the end of the list.

        if (lastPtr != NULL) {

            lastPtr->next = newNode;

            newNode->previous = lastPtr;

        }

        else {

            // The entire list is null

            head = newNode;

        }

    }

    else {

        // ptr points to the data we were searching for, so insert after.

        newNode->next = ptr->next;

        newNode->previous = ptr;

        ptr->next = newNode;

    }

}

然后,您可以執行以下操作:


MyLinkedList list;

list.insertAfter(0, 1);

list.insertAfter(1, 17);

list.insertAfter(1, 10);

如果您隨后轉儲列表的內容(您需要從 head 遍歷 next 指針),您將看到列表按 1、10、17 的順序包含。


查看完整回答
反對 回復 2022-06-23
?
富國滬深

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

我試了一下。


請注意,我實際上并不知道 remove 真正應該做什么。


您的代碼正在尋找具有給定索引的節點,因此我堅持使用該節點(第一個刪除版本)。


但評論建議,您需要刪除具有給定數據的節點。所以我添加了第二個版本,就是這樣做的(第二個刪除版本)。


    public void insertAfter(int givenData, int newData) {

        // Your code here

        Node previous = head;

        while (null != previous && previous.data != givenData) {

            previous = previous.next;

        }

        // this prevents insertion if given Data was not found 

        if (previous == null) {

            System.out.println("the given data doesn't exist");

            return;

        }

        Node newNode = new Node(newData);

        newNode.next = previous.next;

        previous.next = newNode;

    }

刪除具有給定索引的節點的版本:


    // Removes the Node with the given index

    public void remove(int current) {


        Node previous = head;

        for (int i = 0; i < current - 1; i++) {

            if (null == previous) {

                System.out.println("the given position doesn't exist");

                return;

            }

            previous = previous.next;

        }

        if (current == 0) {

            head = head.next;

        } else if (previous.next == null) {

            System.out.println("the given position is after the end - nothing to do!");

        } else {

            previous.next = previous.next.next;

        }

    }

刪除具有給定數據的元素的版本


   // Removes the Node with the given data

    public void remove(int data) {

        Node previous = head;

        if(head == null){

            System.out.println("Empty list - nothing to remove!");

            return;

        } if (head.data == data){

            head = head.next;

            return;

        }


        while(previous.next != null && previous.next.data != data){

            previous = previous.next;

        }

        if (previous.next == null) {

            System.out.println("the given element was not found - nothing to do!");

        } else {

            previous.next = previous.next.next;

        }

    }


查看完整回答
反對 回復 2022-06-23
  • 3 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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