This is my first time posting to one of these things but here goes nothing.
I am having issues with one of my methods from my linked list class. It is just not splitting the list how it should.
Example: I enter 17 elements into this linked list and it was give the first sublist 3 more elements in it than the second. I will admit that an odd number of elements will give one list more than the other but it should only give the first list 1 more element than the second....not 3.
This is what my output looks like:
1 11 2 3 44 55 66 77 88 99 0 13 15 16 24 26 47
List 1:
1 11 2 3 44 55 66 77 88 99
Sublist:
0 13 15 16 24 26 47
The first set of numbers is the entire linked list of 17 elements.
Here is the code for the method in my linked list class:
public void splitMid(LinkedListClass<T> sublist)
{
LinkedListNode<T> current;
if(count == 0)
{
System.out.println("List is Empty!");
}
else
{
current = first;
int subCount = count/2;
if(count%2 == 0)
{
for (int i = 0; i < subCount; i++)
{
current = current.link;
}
}
else
{
for (int i = 0; i < subCount + 1; i++)
{
current = current.link;
}
}
sublist.first = current.link;
sublist.last = last;
last = current;
last.link = null;
sublist.count = subCount;
}
}
I was pretty sure that my if/else was correct in choosing how to divide based on if an even or odd number of elements were entered. However, I now believe I have made some simple error that now eludes me.
Thank you all for any help you can offer me

New Topic/Question
Reply



MultiQuote



|