Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

constructor - How to represent a 'long' number as nodes in a linked-list [JAVA]

I have a pragmatic task to solve:

Int and Long numbers that we can store in memory cells are limited in size. One way to solve the problem would be to set up a linked-list in which each node in the list will contain one digit from the number so that the number itself will not be kept as a number but as a collection of its' digits- one after the other.

I have to create a Constructor, which gets a 'long' type of number and stores it in a format of a linked list.

By that I mean that if we take the number 233,674,318 , the Constructor would create a representation of this number as the following : 2 -> 3 -> 3 -> 6 -> 7 -> 4 -> 3 -> 1 -> 8

Could you please suggest me how can I approach this?

Thank you in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can extract a number's least significant digit (the one on the right) using the modulo operator. In this case, 233,674,318 % 10 will yield 8 (% signifies modulo). You can get rid of a number's least significant digit using division. 233,674,318 / 10 will yield 23,367,431 which is like removing the 8 from the number. Using these two operations you can extract all the digits from a number and build a list out of them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...