删除具有单个链表的元素(需要解释)



我浏览了一个站点。 它给出了以下代码集。 它工作得很好,我完成了我的任务。但我对此代码有疑问。而且我找不到。

法典:

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"( 在用临时完成的更改下面,我对此没意见。在此之后,临时对象副本未分配给标头。

Node 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(); 
} 
}

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

请任何人帮助我

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

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

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

头部仍然指向 1,因此打印工作正常。

如果仔细观察,整个过程中头部是不变的。

代码非常好
删除函数中标头的目的是继续指向链表的第一个节点。
删除函数中temp的目的是移动到位于位置 1 位置的节点并更改下一个链接地址。
您的问题:如果不分配回标题,如何保存更改?

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

相关内容

最新更新