Redian新闻
>
array 转换成 linkedlist, 在线等, 挺急的--help is still nee
avatar
array 转换成 linkedlist, 在线等, 挺急的--help is still nee# LeisureTime - 读书听歌看电影
H*e
1
希望static node fromArray(int[] a) {...}中的head或者tail只用到一个, 应该如何
update codes? Expect working codes in visual studio with only head or tail,
which mean eliminate the head or tail. :)
hint:
You do not need double-linked list. Just think, why do we have head and tail
variables. We do not do anything with head except initial assignment.
class node
{
public int value;
public node next = null;
static node fromArray(int[] a)
{
if (a.Count() == 0) return null;
else
{
node head = new node();
node tail = head;
for (int i = 0; i < a.Count(); i++)
{
tail.value = a[i];
if (i != a.Count() - 1)
{
tail.next = new node();
tail = tail.next;
}
}
return head;
}
}
static void Main(string[] args)
{
int[] c = new int[] { 11, 2, 3 };
node my_list = node.fromArray(c);
while (my_list != null)
{
Console.WriteLine(my_list.value);
my_list = my_list.next;
}
Console.ReadLine();
}
}
avatar
c*d
2
那个class node是出题者定义好的,不许变,只许你改动fromArray()?
如果node本身可以改,加一个prev,单向链表改成双向就OK了。
如果非要用单向链表,我记得有个很操蛋的做法,那就是每个next不是直接指向下一个
node的指针,而是指向下一个node和指向前一个node指针求或非之后的结果……这样等
于变相地把这链表改成双向的。

update

【在 H******e 的大作中提到】
: 希望static node fromArray(int[] a) {...}中的head或者tail只用到一个, 应该如何
: update codes? Expect working codes in visual studio with only head or tail,
: which mean eliminate the head or tail. :)
: hint:
: You do not need double-linked list. Just think, why do we have head and tail
: variables. We do not do anything with head except initial assignment.
: class node
: {
: public int value;
: public node next = null;

avatar
H*e
3
Thanks very mush. This should be fine: "如果node本身可以改,加一个prev,单向
链表改成双向就OK了"
I am working on it right now. Meanwhile, any available codes?

【在 c********d 的大作中提到】
: 那个class node是出题者定义好的,不许变,只许你改动fromArray()?
: 如果node本身可以改,加一个prev,单向链表改成双向就OK了。
: 如果非要用单向链表,我记得有个很操蛋的做法,那就是每个next不是直接指向下一个
: node的指针,而是指向下一个node和指向前一个node指针求或非之后的结果……这样等
: 于变相地把这链表改成双向的。
:
: update

avatar
c*d
4
大概就是这样吧……不保证全对哦。
static node fromArray(int[] a)
{
if (a.Count() == 0) return null;
else
{
node curr = null;
node prev = null;
for (int i = 0; i < a.Count(); i++)
{
curr = new node();
curr.prev = prev;
if (prev) { prev.next = curr; }
prev = curr;
}
curr.next = null;
return curr;
}
}

【在 H******e 的大作中提到】
: Thanks very mush. This should be fine: "如果node本身可以改,加一个prev,单向
: 链表改成双向就OK了"
: I am working on it right now. Meanwhile, any available codes?

avatar
H*e
5
You have visual studio open right now? Expect working codes with only head
or tail, which mean eliminate the head or tail. :)

【在 c********d 的大作中提到】
: 大概就是这样吧……不保证全对哦。
: static node fromArray(int[] a)
: {
: if (a.Count() == 0) return null;
: else
: {
: node curr = null;
: node prev = null;
: for (int i = 0; i < a.Count(); i++)
: {

avatar
c*d
6
原来如此。循环那部分写成这样大概差不多了吧。
我不用VS好多年。
node head = new node();
for (int i = 0; i < a.Count(); i++)
{
head.value = a[i];
if (i != a.Count() - 1)
{
head.next = new node();
head.next.prev = head;
head = head.next;
}
}

【在 H******e 的大作中提到】
: You have visual studio open right now? Expect working codes with only head
: or tail, which mean eliminate the head or tail. :)

avatar
H*e
7
It only prints out the last number of 3, instead of 11, 2, 3
I got the same codes of below after your first reply. I think it is correct,
it just is not working...

【在 c********d 的大作中提到】
: 原来如此。循环那部分写成这样大概差不多了吧。
: 我不用VS好多年。
: node head = new node();
: for (int i = 0; i < a.Count(); i++)
: {
: head.value = a[i];
: if (i != a.Count() - 1)
: {
: head.next = new node();
: head.next.prev = head;

avatar
H*e
8
I change line from
my_list = my_list.next;
to
my_list = my_list.prev;
it prints out 3,2, 11. interesting. I think it only needs some minor
correction now. :)
avatar
c*d
9
因为返回的是双向链表的尾巴啊,你需要从尾巴退回到头。

correct,

【在 H******e 的大作中提到】
: It only prints out the last number of 3, instead of 11, 2, 3
: I got the same codes of below after your first reply. I think it is correct,
: it just is not working...

avatar
H*e
10
but I need print out 11, 2, 3, how to make the correction? I can work this
out, just need it asap....

【在 c********d 的大作中提到】
: 因为返回的是双向链表的尾巴啊,你需要从尾巴退回到头。
:
: correct,

avatar
c*d
11
这个等于倒着建链表,从尾建到头,于是最后返回头。挺好的,省得你还需要从尾退回
去。把循环改成
for (int i = a.Count-1; i > 0; i--)
应该就能给你要的了。
注意链表最后那一位的边界条件也要改。

【在 H******e 的大作中提到】
: I change line from
: my_list = my_list.next;
: to
: my_list = my_list.prev;
: it prints out 3,2, 11. interesting. I think it only needs some minor
: correction now. :)

avatar
H*e
12
Thanks very much!

【在 c********d 的大作中提到】
: 这个等于倒着建链表,从尾建到头,于是最后返回头。挺好的,省得你还需要从尾退回
: 去。把循环改成
: for (int i = a.Count-1; i > 0; i--)
: 应该就能给你要的了。
: 注意链表最后那一位的边界条件也要改。

avatar
a*o
13
早就说过,都不是文科。。
哈哈哈。。。
明天复明天,明天何其多。。
今天 = 明天
明天 = 后天
今天.next = 明天
明天. prv = 今天
array【今天,明天,后天】
明日何其多。。。
为什么不是明天何其多?要用 日?
唉。。
avatar
H*e
14
If you people think this thread if not leisuretime related, please let me
know, I will delete this thread. Or, bz please just delete it. :)
I got the help needed, have been very happy already. :)
avatar
a*o
15
true, when you need help, and you get it, you will be happy. that's the same
to everyone..even couples..
这是很深的学问,其实全部科目都是连在一起的。。
就如这 linked list 的学问。。很高文学内涵的。。就如,昨天,今天,后天。。或
者,从前,过去,现在,将来。。
只不过这是单向的。
哈哈哈。。

【在 H******e 的大作中提到】
: If you people think this thread if not leisuretime related, please let me
: know, I will delete this thread. Or, bz please just delete it. :)
: I got the help needed, have been very happy already. :)

avatar
B*n
16
這樣推的話 每個人今天就都死了

【在 a*o 的大作中提到】
: 早就说过,都不是文科。。
: 哈哈哈。。。
: 明天复明天,明天何其多。。
: 今天 = 明天
: 明天 = 后天
: 今天.next = 明天
: 明天. prv = 今天
: array【今天,明天,后天】
: 明日何其多。。。
: 为什么不是明天何其多?要用 日?

avatar
B*n
17
看成array轉成linklisa

update

【在 H******e 的大作中提到】
: If you people think this thread if not leisuretime related, please let me
: know, I will delete this thread. Or, bz please just delete it. :)
: I got the help needed, have been very happy already. :)

avatar
H*e
18
I was told below:
So, you still have 2 variables. Even more, you have 3 variables.
You do not need double-linked list. Just think, why do we have head and tail
variables. We do not do anything with head except initial assignment.
==================================
Below are the updated codes for the solution using doubly linked list:
class node
{
public int value;
public node next = null;
public node prev = null;
static node fromArray(int[] a)
{
if (a.Count() == 0) return null;
else
{
node head = new node();
for (int i = a.Count() - 1; i >= 0; i--)
{
head.value = a[i];
if (i > 0)
{
head.next = new node();
head.next.prev = head;
head = head.next;
}
}
return head;
}
}
static void Main(string[] args)
{
int[] c = new int[] { 11, 2, 3 };
node my_list = node.fromArray(c);
while (my_list != null)
{
Console.WriteLine(my_list.value);
my_list = my_list.prev;
}
Console.ReadLine();
}
}
avatar
s*l
19
哈哈

【在 B****n 的大作中提到】
: 看成array轉成linklisa
:
: update

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。