Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top