p*s
2 楼
谢谢
l*i
3 楼
parameter 只有一个 Node,要被删除。
private static boolean removeNode(Node toRemoved)
{
if(toRemoved == null)
{
return false;
}
if(toRemoved.next == null) //This is not working.
{
toRemoved = null;
return true;
}
Node next = toRemoved.next;
toRemoved.data = next.data;
toRemoved.next = next.next;
return true;
}
我想这个node如果是最后一个元素的话,设其为 null
但是实际跑的时候,发现这个node并没有删除。请问为什么?有什么办法直接删除
linked list 的最后一个元素 ?
多谢。
private static boolean removeNode(Node toRemoved)
{
if(toRemoved == null)
{
return false;
}
if(toRemoved.next == null) //This is not working.
{
toRemoved = null;
return true;
}
Node next = toRemoved.next;
toRemoved.data = next.data;
toRemoved.next = next.next;
return true;
}
我想这个node如果是最后一个元素的话,设其为 null
但是实际跑的时候,发现这个node并没有删除。请问为什么?有什么办法直接删除
linked list 的最后一个元素 ?
多谢。
c*r
4 楼
做窗帘,家具之类的称为decorator fabric。做衣服的料很多了,比如说cotton woven
(棉布), cotton knit (棉的有收缩性的,类似国内做内衣的料),twill 或者
jeans, silk。我建议你去店里看看,感觉一下就知道了。
(棉布), cotton knit (棉的有收缩性的,类似国内做内衣的料),twill 或者
jeans, silk。我建议你去店里看看,感觉一下就知道了。
h*v
5 楼
看你最近挺忙的。一边帮人看房子,有个大门facing north的,但是,买家couldn't
afford it。。。 一边还要跟有tax evasion的evil guy打交道。。。
Just kidding.
afford it。。。 一边还要跟有tax evasion的evil guy打交道。。。
Just kidding.
g*g
8 楼
来问作业?
if(current.next.next == null) {
current.next = null;
} else {
current = current.next;
}
加上检查小于等于1个结点的特殊情况就可以了。
【在 l***i 的大作中提到】
: parameter 只有一个 Node,要被删除。
: private static boolean removeNode(Node toRemoved)
: {
: if(toRemoved == null)
: {
: return false;
: }
: if(toRemoved.next == null) //This is not working.
: {
: toRemoved = null;
if(current.next.next == null) {
current.next = null;
} else {
current = current.next;
}
加上检查小于等于1个结点的特殊情况就可以了。
【在 l***i 的大作中提到】
: parameter 只有一个 Node,要被删除。
: private static boolean removeNode(Node toRemoved)
: {
: if(toRemoved == null)
: {
: return false;
: }
: if(toRemoved.next == null) //This is not working.
: {
: toRemoved = null;
J*n
10 楼
toRemove.next==null的情况应该直接return false,因为你只给toRemove这个节点,
而节点又只有指向next的reference,没有previous,那如果toRemove是最后一个节点
,完全无解啊
【在 l***i 的大作中提到】
: parameter 只有一个 Node,要被删除。
: private static boolean removeNode(Node toRemoved)
: {
: if(toRemoved == null)
: {
: return false;
: }
: if(toRemoved.next == null) //This is not working.
: {
: toRemoved = null;
而节点又只有指向next的reference,没有previous,那如果toRemove是最后一个节点
,完全无解啊
【在 l***i 的大作中提到】
: parameter 只有一个 Node,要被删除。
: private static boolean removeNode(Node toRemoved)
: {
: if(toRemoved == null)
: {
: return false;
: }
: if(toRemoved.next == null) //This is not working.
: {
: toRemoved = null;
J*n
15 楼
toRemove只是一个reference,相当于一个指针,而指向这个要删的Node object的还有
其他指针(比如前一个节点的next),你把toRemove=null以后toRemove这个指针是不
指向你要删除的节点了,但是别忘了该节点的前一个节点的next reference还是指向该
节点的。这跟Java没关系。这样当从LinkedList的第一个node遍历的时候,还是可以走
到要删的节点,说的有点绕,希望意思表达明白了。。。
list
【在 l***i 的大作中提到】
: 请注意,这个问题要求parameter只有一个,就是当前的node的pointer,没有整个list
: 的信息。而且这个要求自己implement,不能用java现有的library。
: 我的问题是,既然倒数第二个的next指向最后一个,那我把最后一个设为null,不就相
: 当与删除了这个node吗?是不是有什么java底层的东西我搞错了?
其他指针(比如前一个节点的next),你把toRemove=null以后toRemove这个指针是不
指向你要删除的节点了,但是别忘了该节点的前一个节点的next reference还是指向该
节点的。这跟Java没关系。这样当从LinkedList的第一个node遍历的时候,还是可以走
到要删的节点,说的有点绕,希望意思表达明白了。。。
list
【在 l***i 的大作中提到】
: 请注意,这个问题要求parameter只有一个,就是当前的node的pointer,没有整个list
: 的信息。而且这个要求自己implement,不能用java现有的library。
: 我的问题是,既然倒数第二个的next指向最后一个,那我把最后一个设为null,不就相
: 当与删除了这个node吗?是不是有什么java底层的东西我搞错了?
l*i
21 楼
呵呵,你解释得很明白。谢谢。
在C++里可以用pointer的reference直接改变一个memory location的值,比如下面这个
#include
using namespace std;
void update(int*);
int main()
{
int x = 5;
int* pi = &x;
cout << "Before update, " << x << endl;
update(pi);
cout << "After update, " << x << endl;
return 0;
}
void update(int* t)
{
*t = 8;
}
输出是
Before update, 5
After update, 8
是不是可以说,在java里 没有相应的功能?
node
【在 J*******n 的大作中提到】
: 貌似知道你为啥进入死胡同了。。。从linkedlist里删掉一个node的意思就是这个node
: 前一个node的next pointer不指向它,这个node后一个node的previous pointer也不指
: 向它,就表示删掉了,因为这个node就跟linkedlist没关系了。不是说把这个node
: object清掉才叫删掉。。。
:
: node。
在C++里可以用pointer的reference直接改变一个memory location的值,比如下面这个
#include
using namespace std;
void update(int*);
int main()
{
int x = 5;
int* pi = &x;
cout << "Before update, " << x << endl;
update(pi);
cout << "After update, " << x << endl;
return 0;
}
void update(int* t)
{
*t = 8;
}
输出是
Before update, 5
After update, 8
是不是可以说,在java里 没有相应的功能?
node
【在 J*******n 的大作中提到】
: 貌似知道你为啥进入死胡同了。。。从linkedlist里删掉一个node的意思就是这个node
: 前一个node的next pointer不指向它,这个node后一个node的previous pointer也不指
: 向它,就表示删掉了,因为这个node就跟linkedlist没关系了。不是说把这个node
: object清掉才叫删掉。。。
:
: node。
J*n
23 楼
In Java, primitives are passed by value, objects are passed by reference.
in Java, if you have
int x = 5;
int y = x;
then you update y, x won't be updated, because int is primitive, so y has
its own value instead of an pointer to value of x.
if you have
Object x = new Object();
Object y = x;
then you update y, the object that x points to will be updated as well,
because reference x is passed to reference y.
这个
【在 l***i 的大作中提到】
: 呵呵,你解释得很明白。谢谢。
: 在C++里可以用pointer的reference直接改变一个memory location的值,比如下面这个
: #include
: using namespace std;
: void update(int*);
: int main()
: {
: int x = 5;
: int* pi = &x;
: cout << "Before update, " << x << endl;
in Java, if you have
int x = 5;
int y = x;
then you update y, x won't be updated, because int is primitive, so y has
its own value instead of an pointer to value of x.
if you have
Object x = new Object();
Object y = x;
then you update y, the object that x points to will be updated as well,
because reference x is passed to reference y.
这个
【在 l***i 的大作中提到】
: 呵呵,你解释得很明白。谢谢。
: 在C++里可以用pointer的reference直接改变一个memory location的值,比如下面这个
: #include
: using namespace std;
: void update(int*);
: int main()
: {
: int x = 5;
: int* pi = &x;
: cout << "Before update, " << x << endl;
c*e
24 楼
in java,it's all passed by value. e.g.
main() {
Person p= new Person("Jack");
foo(p);
system.out.println(p);
//you need to write an override tostring() in Person class
}
================
public void foo(Person s) {
s=new Person("Tom");
}
【在 J*******n 的大作中提到】
: In Java, primitives are passed by value, objects are passed by reference.
: in Java, if you have
: int x = 5;
: int y = x;
: then you update y, x won't be updated, because int is primitive, so y has
: its own value instead of an pointer to value of x.
: if you have
: Object x = new Object();
: Object y = x;
: then you update y, the object that x points to will be updated as well,
main() {
Person p= new Person("Jack");
foo(p);
system.out.println(p);
//you need to write an override tostring() in Person class
}
================
public void foo(Person s) {
s=new Person("Tom");
}
【在 J*******n 的大作中提到】
: In Java, primitives are passed by value, objects are passed by reference.
: in Java, if you have
: int x = 5;
: int y = x;
: then you update y, x won't be updated, because int is primitive, so y has
: its own value instead of an pointer to value of x.
: if you have
: Object x = new Object();
: Object y = x;
: then you update y, the object that x points to will be updated as well,
相关阅读
kaffe javacCore Java2 Notes (5)(ZZ) J2EE vs. Microsoft.NETFree bookclub accounts give away.Re: 求JAVA简历Want take SCEANew year gift, can download now.[转载] Re: 要不咱们自己做一个象napster那样的东东?[转载] zz 王森:程序设计师真情忏悔录(修正完整版).net vs. J2ee(zhuan)maybe useful SCJP-mock exam link listMwTerm 支持拼音输入, GB/BIG5 简/繁体 Release 1jar 的问题, 路径有问题? 大家帮忙看看是啥毛病?UNIX build using ANT!!!Re: 恳请各位JAVA大师:有没有这样一个东西?Java vs C++ --> What is the ultimate direction?JAVA TELNET 支持简繁中文及拼音输入 V1.0 Beta2Re: How can I get localhost's address in a Applet?(转载)国内不谈JAVA警告TeacherWei