双向链表混乱

发布于 2024-12-04 20:40:55 字数 740 浏览 1 评论 0原文

在我的课堂上,我有一个任务是创建一个 Number 类,该类具有算术运算。 (加/减/乘/等)

有一个部分让我感到困惑 - 双向链表。规范中讨论它的唯一部分我觉得有点令人困惑。我不知道我应该存储什么 - 输入的所有数字?它表示高位数字的节点的高点......我不知道这意味着什么。另外,我不确定如何实现双向链表......只需引用下一个“高位数字”?

另外 - 什么是Node?作业只说我要创建 class Number...但是 Java API 中的 Node 说它与 HTML 标签有关?我不应该使用数字低,高吗???

这是规范中详细说明双向链表部分的部分:

数字将存储在双向链表中(此处不要使用泛型)。每个节点都会 有一个 int 值字段,该字段将保存一位数字(0 到 9)和两个指针字段, 上一个和下一个。

Number 类将有五个字段: 

私有节点低,高;
私有 int 数字计数 = 0;
私有 int 小数位数 = 0;
私有布尔负数= false;

high指向高位数字的节点,low指向 低位数字的节点, digitalCount 是列表中存储的位数, DecimalPlaces 是位数(节点) 小数点后和负数给出符号。

我并不要求一个确切的解决方案,只是一些指导和理解。我真的很感谢所提供的任何帮助。

In my class I have an assignment to create a Number class, which has operations for arithmetic. (Add/subtract/multiply/etc.)

There is one part I am confused about - the doubly-linked lists. The only part of the spec that discusses it I find a little confusing. I don't know what I'm supposed to be storing - all numbers entered? It says high points to the high-order digit's node...I don't know what that means. Also, I'm not sure exactly how I would implement the doubly-linked list...just have a reference to the next 'high-order digit'?

ALSO - What is Node? The assignment only says I am to create the class Number...but Node in the Java API says it is something to do with HTML tags? Shouldn't I use Number low, high???

Here is the part of the spec detailing the doubly-linked list part:

Numbers will be stored in doubly-linked lists (do not use generics here). Each node will
have an int value field which will hold one digit (0 through 9) and two pointer fields,
prev and next.

The Number class will have five fields: 

private Node low, high;
private int digitCount = 0;
private int decimalPlaces = 0;
private boolean negative = false;

high points to the high-order digit's node, low points to the
low-order digit's node,
digitCount is the number of digits stored in the list,
decimalPlaces is the number of digits (nodes)
after the decimal place and negative gives the sign.

I am not asking for an exact solution, just some guidance and understanding. I truly appreciate any help given.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坦然微笑 2024-12-11 20:40:55

在这种情况下,节点是链表的一个元素。

看起来你应该将一个数字表示为一个链表,其中每个节点包含一位数字。

因此数字

12.34

将有 4 个节点,每个节点代表 1、2、3 和 4。你的数字将数字计数为 2,小数位数为 2,因为小数点前后各有 2 位数字。

您的链接列表应类似于

1 <-> 2<-> 3<-> 4

其中 <-> 表示链表的双向链接性质(指向上一个和下一个节点的指针。)可能应该有来自 1 <-> 的指针。 4 同样,如果列表应该是循环的。

您的 Number 类的大纲看起来像

public class MyNumber {
   DigitNode low;
   DigitNode high;
   int digitCount;
   int decimalPlaces;
   bool negative;
}

您的 DigitNode 类大纲看起来像

public class DigitNode {
   int digit;
   DigitNode next;
   DigitNode prev;
}

我忽略诸如私有/受保护的内容以及诸如设置器/获取器之类的内容。现在,您可以通过获取 low 属性、获取其“next”属性并循环直到 next 为 null 来迭代数字。

A Node in this context is an element of your linked list.

It seems like you are supposed to represent a Number as a linked list, where each Node contains one digit..

So the number

12.34

will have 4 nodes in it, one for each of 1, 2, 3, and 4. Your Number will have digitCount of 2 and decimalPlaces of 2 since there are 2 digits each before and after the decimal.

Your linked list should look like

1 <-> 2 <-> 3 <-> 4

where <-> represents the doubly linked nature of the linked list (pointers for both previous and next node.) There should probably be pointers from 1 <-> 4 as well, if the list is supposed to be circular.

The outline of your Number class would look like

public class MyNumber {
   DigitNode low;
   DigitNode high;
   int digitCount;
   int decimalPlaces;
   bool negative;
}

your DigitNode class outline would look like

public class DigitNode {
   int digit;
   DigitNode next;
   DigitNode prev;
}

im ignoring things like private/protected and things like setters/getters. You can now do something like iterate over the digits by getting the low property, getting its 'next' property, and looping until next is null.

你怎么敢 2024-12-11 20:40:55

看起来数字是一个节点,基于您规范中的这段文本:

...the number of digits (nodes) after the...

高阶,在这种情况下,似乎意味着 10 的下一个幂,所以下一个更大的数字,而低阶似乎是相反的,所以下一个最低数字。

It looks like a digit is a node, based on this text from your spec:

...the number of digits (nodes) after the...

High-order, in this context, seems to mean the next power of 10, so the next larger digit, and low order seems to be the opposite, so the next lowest digit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文