Okey so you have 7 Nodes stored in buffer A and you want to "split" the nodes in 2 buffers, A and B where A contains 4 and b 3 of the Nodes, right?
Which 4 should A contain and which 3 b?
I'll assume A should contain the first 4 elements for the time being (correct me if i am wrong!)
Ok now a quick look at your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
void CLinkedList :: split(CLinkedList & list)
{
int size, half;
size = 1;
NodeType *temp = cursor;
bool even;
if (IsEmpty())
{
cout << "List is empty, nothing to split!" << endl;
}
else
{
while (temp->next != cursor)
{
temp = temp->next;
size++;
}
}
// Should't all of this be in the else statement?
// I mean, you don't want to do anything if the buffer is empty
if (size % 2 == 0) // checks if even or odd
{
//...
}
|
okey, so now I'm going in more detail, I didn't take a look at the rest of your code, just noticed that this should be inside the else bracket.
I'd start by making a Method to get a node _position away from _startNode
Also I'd make a Method to count the nodes (maybe I'd even just have a variable that stores the amount of nodes in the buffer)
I don't know if I'll need it :b
and maybe I'll replace it because of performance issues
This is of course not tested :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
unsigned int CLinkedList::size() const
{
NodeType* node = cursor;
unsigned int _size = 0;
do
{
size++;
node = node->next
} while(node != cursor);
return size;
}
// could be implemented in many different ways
NodeType* CLinkedList :: get(NodeType* _startNode, unsigned int _position) const
{
while(_position--)
{
_startNode = _startNode->next;
}
return _startNode;
}
// ...
// somewhere in split()
if(!isEmpty())
{
// size = 7
// nodes_for_a = 4
// nodes[0] - [3] are for a -> nodes[3]'s next is nodes[0] which is cursor
// nodes[4] - [6] are for b -> nodes[6]'s next is nodes[4]
unsigned int size = this->size();
if(size < 2) // only work if 2 or more nodes are available
return;
unsigned int nodes_for_a = size/2 + size%2;
// get nodes[3] and nodes[6]
NodeType* last_in_a = this->get(cursor, nodes_for_a - 1);
NodeType* last_in_b = this->get(last_in_a, size - nodes_for_a);
// nodex[6]->next = nodes[3]->next = nodes[4]
last_in_b->next = last_in_a->next;
// nodes[3]->next = nodes[0] = cursor
last_in_a->next = cursor;
// set lists cursot to first_in_b (which is last_in_b->next)
list.cursor = last_in_b->next; // I'll just assume that's everything i have to do to tell b where the buffer is
}
|