I have a project that ive been working on where I am using a linked list to create an integer value that has no limit to its size. It is done so by passing a String into the constructor and then each node of the linked list contains 3 digits of the number. The String values are converted to ints. Where I am struggling is iteration through the string and adding the ints to nodes correctly. Also, the number should be stored in reverse order so for example if the number is 123456 the list would have 2 nodes. Head would be 654 and the second node(tail) would be 321.
Here is my current code for the constructor
public UnboundedInt(String digits){
head = null;//head of list
tail = head;//tail of list
cursor = null;//current node of list
precursor = null;//node previous to current
manyNodes = 0;//number of nodes in the list
String headData;
for(int i = 0; i < digits.length(); i ++){
headData = digits.substring(i,i+=3);//creates substring of next 3
//digits
int dataHold = Integer.parseInt(headData);//converts substring to int
//value
IntNode temp = new IntNode(dataHold,null);
temp.setLink(head);
head = temp;
manyNodes++;//increases number of nodes
}
}
I am using 132456789123456789123456789 as a test value and it is currently telling me in the debug that I only have 7 nodes which are currently stored as 789 891 456 912 567 132. This should come out to be 9 nodes. Im sure theres something very trivial that I am missing but any suggestions or advice is greatly appreciated. Thank you.
Lets do some tracing here
132456789123456789123456789
while the substring
method using i+=3
the method will be for the first loop as this
headData = digits.substring(0,3)
which means the result will be like this
loopNo resultOfSubString deletedDigit
1 132 -
2 567 4
3 912 8
4 456 3
5 891 7
6 345 2
7 789 6
the printed nodes
You should decrement the value of i
by 1 at the end of each loop .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.