struct looks like this:
struct ListNode;
typedef ListNode* ListType;
struct details
{
char first_name[20];
char last_name[20];
int start_number;
int end_number;
};
struct ListNode
{
details data;
ListType next;
};
ListType list = NULL;
ListType head = NULL;
ListType * PointerToHead = &head;
I have a circular linked list that i bubblesort, and the swap function for it now looks like this:
void Swap(ListType& a, ListType& b)//when called b is always a->next
{
ListType prevNodeA;
ListType tempPtr = a;
ListType AfterNodeB = b->next;
while(tempPtr->next!=a)//find the node before a
{
tempPtr = tempPtr->next;
}
if(tempPtr->next==a)
{
prevNodeA = tempPtr;
}
if(prevNodeA->next==head)//if it is head, assign b as head ?
{
b->next = prevNodeA->next;
prevNodeA->next = a->next;
a->next = AfterNodeB;
b = head;
PointerToHead = &b;
}
else
{
b->next = prevNodeA->next;
prevNodeA->next = a->next;
a->next = AfterNodeB;
}
};
I am fairly certain that im swapping the pointers right, the problem lies in assigning the head, because it never reads the else part. The first time im calling the fuction, a is the head of the list and it is stuck in that loop.
Sort function:
void Sort(ListType* PointerToHead)
{
ListNode *temp1;
ListNode *temp2;
for(temp1 = *PointerToHead ; temp1->next!=head ; temp1 = temp1->next)
{
for(temp2 = temp1->next ; temp2->next!=head; temp2 = temp2->next)
{
if (Smallest(temp2->data, temp1->data))
{
Swap(temp1, temp2);
}
}
}
};
so my question is, how do i reassign the head while in a circular linked list?
(i only have a head node in the linked list, no tail). the pointer of the last node points to the head.