Hey I'm a beginner in C++ so please don't blame me if the question is stupid :D
Because it was too complicated to implement the sorting function in my list I created a function with a pointer for the beginning. But now I don't know how I can take the elements of my existing list inside this function.
Explanation what I want to do: I have a text file with pairs of numbers and have to sort them principally like in this domino game. so like this [3:2][1:2][3:1](start), [1:3][3:2][2:1](end). Sometimes there are more numbers in one file and I could creat more rings.
I hope you understand me :D
Thanks for the help.
ListPair* sortedPair(Node *head)
{
Ringliste ringList; //<- this is the new ringlist with the new sorted pairs
List domino; //<- this is the list with the elements, unsorted
//name elements of list
Node *node_a = head;
Node *node_b = node_a->next;
Node *startNode = node_a;
//put everything NULL
RingPair *ringHead = new RingPair();
ringHead->next = NULL;
ListPair* listPair = NULL;
RingPair* nextPair = NULL;
//first element of list is first element of new list
ringHead->node = node_a;
while (node_a != NULL)
{
while (node_b != NULL)
{
//first two elements suit togehter
if (node_a->b == node_b->a)
{
RingPair *nextPair = new RingPair();
nextPair->node = node_b;
ringList.insertPair(ringHead,nextPair);
//the two elements are in new ringlist
//have to be deleted from old list
Node *temp_2 = node_b->next;
//head = deleteNode(head, node_b);
domino.deleteHead();
//take next element
node_a = node_b;
if (temp_2 != NULL)
node_b = temp_2;
else
node_b = head;
}
elseif (node_a->b == node_b->b)
{
...//the sorting algorithm is not that necessary for my problem
}
if ((node_a->b != node_b->a) && (node_a->b != node_b->b))
{
//can't make any more pair with first element of old list
//take next element from list
node_b = node_b->next;
if ((node_b == NULL) && (head->next != NULL))
{
node_b = head;
}
}
//insert ring to be able to begin new ring
ListPair *nextRingPair = new ListPair();
nextRingPair->ringPair = ringHead;
if (listPair == NULL)
listPair = nextRingPair;
else
{
ListPair *temp = listPair;
while (temp->next != NULL)
temp = temp->next;
temp->next = nextRingPair;
}
}
//all elements are now in ringlist
//delete old elements
head = deleteNode(head, node_a);
head = deleteNode(head, startNode);
//new element is head
node_a = head;
startNode = node_a;
//begin new ring because old one is completed
ringHead = new RingPair();
ringHead->node = node_a;
ringHead->next = NULL;
}
return listPair;
}
You can also watch the whole code here: http://c9.io/naralli/domino
(but there are the comments German because unfortunately I have to make it for a German project :D )