Algorithm 按引用还是按值链接列表?
问题是。假设链表实现如下(Java): 以链接列表为例:Algorithm 按引用还是按值链接列表?,algorithm,data-structures,linked-list,Algorithm,Data Structures,Linked List,问题是。假设链表实现如下(Java): 以链接列表为例: 1 -> 2 -> 3 -> 4 我会这样做: ListNode newHead = head; newHead = head.next.next; //Now newHead is pointing to (3) in the linked list. 现在我表演魔术: newHead.val = 87 链接列表变为: 1 -> 2 -> 87 -> 4 如果我打印了头和非新头 为什么会这样
1 -> 2 -> 3 -> 4
我会这样做:
ListNode newHead = head;
newHead = head.next.next;
//Now newHead is pointing to (3) in the linked list.
现在我表演魔术:
newHead.val = 87
链接列表变为:
1 -> 2 -> 87 -> 4
如果我打印了头和非新头
为什么会这样?我没有用head修改任何内容,但它仍然更改了?因此您可以使用此选项:
节点类:
public class IntNode {
int value;
IntNode next;
public IntNode(int value) {
this.value = value;
}
}
单链表类:
/**
* A singly-linked list of integer values with fast addFirst and addLast methods
*/
public class LinkedIntList {
IntNode first;
IntNode last;
int size;
/**
* Return the integer value at position 'index'
*/
int get(int index) {
return getNode(index).value;
}
/**
* Set the integer value at position 'index' to 'value'
*/
void set(int index, int value) {
getNode(index).value = value;
}
/**
* Returns whether the list is empty (has no values)
*/
boolean isEmpty() {
return size == 0;
}
/**
* Inserts 'value' at position 0 in the list.
*/
void addFirst(int value) {
IntNode newNode = new IntNode(value);
newNode.next = first;
first = newNode;
if(last == null)
last = newNode;
size++;
}
/**
* Appends 'value' at the end of the list.
*/
void addLast(int value) {
IntNode newNode = new IntNode(value);
if(isEmpty())
first = newNode;
else
last.next = newNode;
last = newNode;
size++;
}
/**
* Removes and returns the first value of the list.
*/
int removeFirst() {
if(isEmpty()) {
System.out.println("RemoveFirst() on empty list!");
System.exit(-1);
}
int value = first.value;
if(first == last) {
// List has only one element, so just clear it
clear();
}
else {
first = first.next;
size--;
}
return value;
}
/**
* Removes and returns the last value of the list.
*/
int removeLast() {
if(isEmpty()) {
System.out.println("RemoveLast() on empty list!");
System.exit(-1);
}
int value = last.value;
if(first == last) {
// List has only one element, so just clear it
clear();
}
else {
// List has more than one element
IntNode currentNode = first;
while(currentNode.next != last)
currentNode = currentNode.next;
currentNode.next = null;
last = currentNode;
size--;
}
return value;
}
/**
* Removes all values from the list, making the list empty.
*/
void clear() {
first = last = null;
size = 0;
}
/**
* Returns a new int-array with the same contents as the list.
*/
int[] toArray() {
int[] array = new int[size];
int i = 0;
for(IntNode n = first; n != null; n = n.next, i++)
array[i] = n.value;
return array;
}
/**
* For internal use only.
*/
IntNode getNode(int index) {
if(index < 0 || index >= size) {
System.out.println("GetNode() with invalid index: " + index);
System.exit(-1);
}
IntNode current = first;
for(int i = 0; i < index; i++)
current = current.next;
return current;
}
}
/**
*具有快速addFirst和addLast方法的整数值的单链表
*/
公共类链接列表{
IntNode优先;
最后一个节点;
整数大小;
/**
*返回“index”位置的整数值
*/
int get(int索引){
返回getNode(index).value;
}
/**
*将“index”位置的整数值设置为“value”
*/
无效集(int索引,int值){
getNode(index).value=value;
}
/**
*返回列表是否为空(没有值)
*/
布尔isEmpty(){
返回大小==0;
}
/**
*在列表中的位置0处插入“值”。
*/
void addFirst(int值){
IntNode newNode=新IntNode(值);
newNode.next=first;
第一个=新节点;
if(last==null)
last=newNode;
大小++;
}
/**
*在列表末尾追加“value”。
*/
void addLast(int值){
IntNode newNode=新IntNode(值);
if(isEmpty())
第一个=新节点;
其他的
last.next=newNode;
last=newNode;
大小++;
}
/**
*删除并返回列表的第一个值。
*/
int removeFirst(){
if(isEmpty()){
System.out.println(“空列表上的RemoveFirst()!);
系统退出(-1);
}
int值=first.value;
如果(第一个==最后一个){
//列表只有一个元素,所以只需清除它
清除();
}
否则{
first=first.next;
大小--;
}
返回值;
}
/**
*删除并返回列表的最后一个值。
*/
int removeLast(){
if(isEmpty()){
System.out.println(“空列表上的RemoveLast());
系统退出(-1);
}
int value=last.value;
如果(第一个==最后一个){
//列表只有一个元素,所以只需清除它
清除();
}
否则{
//列表有多个元素
IntNode currentNode=第一个;
while(currentNode.next!=最后一个)
currentNode=currentNode.next;
currentNode.next=null;
last=currentNode;
大小--;
}
返回值;
}
/**
*从列表中删除所有值,使列表为空。
*/
无效清除(){
first=last=null;
尺寸=0;
}
/**
*返回与列表内容相同的新整数数组。
*/
int[]toArray(){
int[]数组=新的int[size];
int i=0;
for(IntNode n=first;n!=null;n=n.next,i++)
数组[i]=n.value;
返回数组;
}
/**
*仅供内部使用。
*/
IntNode getNode(int索引){
如果(索引<0 | |索引>=大小){
System.out.println(“具有无效索引的GetNode()”+“索引”);
系统退出(-1);
}
节点电流=第一;
对于(int i=0;i
有关说明,请参见代码中的注释。显然,这是因为您正在更改同一对象。ListNodes只包含对每个ListNode的引用。@MatthiasFax,如果我创建一个类然后执行此操作,是否总是这样?是的,如果不克隆或深度复制对象,则只会引用该值(原语除外)。检查这些现有问题:
/**
* A singly-linked list of integer values with fast addFirst and addLast methods
*/
public class LinkedIntList {
IntNode first;
IntNode last;
int size;
/**
* Return the integer value at position 'index'
*/
int get(int index) {
return getNode(index).value;
}
/**
* Set the integer value at position 'index' to 'value'
*/
void set(int index, int value) {
getNode(index).value = value;
}
/**
* Returns whether the list is empty (has no values)
*/
boolean isEmpty() {
return size == 0;
}
/**
* Inserts 'value' at position 0 in the list.
*/
void addFirst(int value) {
IntNode newNode = new IntNode(value);
newNode.next = first;
first = newNode;
if(last == null)
last = newNode;
size++;
}
/**
* Appends 'value' at the end of the list.
*/
void addLast(int value) {
IntNode newNode = new IntNode(value);
if(isEmpty())
first = newNode;
else
last.next = newNode;
last = newNode;
size++;
}
/**
* Removes and returns the first value of the list.
*/
int removeFirst() {
if(isEmpty()) {
System.out.println("RemoveFirst() on empty list!");
System.exit(-1);
}
int value = first.value;
if(first == last) {
// List has only one element, so just clear it
clear();
}
else {
first = first.next;
size--;
}
return value;
}
/**
* Removes and returns the last value of the list.
*/
int removeLast() {
if(isEmpty()) {
System.out.println("RemoveLast() on empty list!");
System.exit(-1);
}
int value = last.value;
if(first == last) {
// List has only one element, so just clear it
clear();
}
else {
// List has more than one element
IntNode currentNode = first;
while(currentNode.next != last)
currentNode = currentNode.next;
currentNode.next = null;
last = currentNode;
size--;
}
return value;
}
/**
* Removes all values from the list, making the list empty.
*/
void clear() {
first = last = null;
size = 0;
}
/**
* Returns a new int-array with the same contents as the list.
*/
int[] toArray() {
int[] array = new int[size];
int i = 0;
for(IntNode n = first; n != null; n = n.next, i++)
array[i] = n.value;
return array;
}
/**
* For internal use only.
*/
IntNode getNode(int index) {
if(index < 0 || index >= size) {
System.out.println("GetNode() with invalid index: " + index);
System.exit(-1);
}
IntNode current = first;
for(int i = 0; i < index; i++)
current = current.next;
return current;
}
}