删除具有单个链接列表的元素(需要说明)

时间:2019-12-13 06:17:56

标签: java

我浏览了一个站点。它给出了以下代码集。 它运行完美,我完成了任务。但是我对此代码有疑问。而且我找不到。

代码:

public  class Node 
{ 
    private int data; 
    private Node next; 
    /**
     * @return the data
     */
    public int getData() {
        return data;
    }


    /**
     * @param data the data to set
     */
    public void setData(int data) {
        this.data = data;
    }


    /**
     * @return the next
     */
    public Node getNext() {
        return next;
    }


    /**
     * @param next the next to set
     */
    public void setNext(Node next) {
        this.next = next;
    }


    Node(int d) 
    { 
        data = d; 
        next = null; 
    } 
}

class Sam 
{ 
    Node head;  // head of list 

    /* Linked list Node*/

    /* Inserts a new Node at front of the list. */
    public void push(int new_data) 
    { 
        /* 1 & 2: Allocate the Node & 
                  Put in the data*/
        Node new_node = new Node(new_data); 

        /* 3. Make next of new Node as head */
        new_node.setNext(head); 

        /* 4. Move the head to point to new Node */
        head = new_node; 
    } 

    /* Given a reference (pointer to pointer) to the head of a list 
       and a position, deletes the node at the given position */
    void deleteNode(int position) 
    { 
        // If linked list is empty 
        if (head == null) 
            return; 

        // Store head node 
        Node temp = head; 

        // If head needs to be removed 
        if (position == 0) 
        { 
            head = temp.getNext();   // Change head 
            return; 
        } 

        // Find previous node of the node to be deleted 
        for (int i=0; temp!=null && i<position-1; i++) 
            temp = temp.getNext(); 

        // If position is more than number of ndoes 
        if (temp == null || temp.getNext() == null) 
            return; 

        // Node temp->next is the node to be deleted 
        // Store pointer to the next of node to be deleted 
        Node next = temp.getNext().getNext(); 

        temp.setNext(next);  // Unlink the deleted node from list 
    } 

    /* This function prints contents of linked list starting from 
        the given node */
    public void printList() 
    { 
        Node tnode = head; 
        while (tnode != null) 
        { 
            System.out.print(tnode.getData()+" "); 
            tnode = tnode.getNext(); 
        } 
    } 

    /* Drier program to test above functions. Ideally this function 
       should be in a separate user class.  It is kept here to keep 
       code compact */
    public static void main(String[] args) 
    { 
        /* Start with the empty list */
        Sam llist = new Sam(); 

        llist.push(7); 
        llist.push(1); 
        llist.push(3); 
        llist.push(2); 
        llist.push(8);
        llist.push(23);
        llist.push(56);

        System.out.println("\nCreated Linked list is: "); 
        llist.printList(); 

        llist.deleteNode(4);  // Delete node at position 4 

        System.out.println("\nLinked List after Deletion at position 4: "); 
        llist.printList(); 
    } 
}

这是我的疑问。

在'deleteNode(int position)'中,方法'header'被分配给新对象('temp') 在用温度进行的更改下面,我可以接受。此临时对象副本之后未分配给标题。

节点next = temp.getNext()。getNext();

    temp.setNext(next);  

但是在printList方法中,他们使用了标头对象,并且在删除后显示了剩余的元素

 public void printList() 
    { 
        Node tnode = head; 
        while (tnode != null) 
        { 
            System.out.print(tnode.getData()+" "); 
            tnode = tnode.getNext(); 
        } 
    }

我只是想知道如何将临时对象的更改替换为标头对象(不将临时对象分配给标头对象)

请有人帮助我

2 个答案:

答案 0 :(得分:1)

假设我们有一个LinkedList 1,2,3,4,5

void deleteNode(int position)方法内,head和temp都指向节点1。如果我们要删除列表中位置2(值为3的节点)的节点,它的迭代温度,直到位置1(值为2的节点)为止。

当temp在值为2的节点上时,下一个节点为3且其下一个节点为4分配给节点2的下一个节点,因为2直接指向4,所以有效地删除了3。请注意其静止节点正在被更改。

head仍指向1,因此打印工作正常。

如果您仔细观察,在整个过程中头部保持不变。

答案 1 :(得分:0)

代码非常完美
删除功能中 Header 的目的是保持指向链接列表的第一个节点。
删除功能中 temp 的目的是前往位于位置1的那个节点并更改下一个链接地址。
您的问题: 如果不分配回标头,如何保存更改?

回答:一位具有基本编程语言知识的人将了解对变量的任何更改都会立即发生。在使用链表的情况下,所有节点都相互链接。如果将temp分配给标头,则链表的头将变得无法访问。我们不希望这样做,因为标头只是指向整个程序中链表头的变量。
Temp表示临时变量,使用后将被丢弃。