I have been trying to bubble sort a doubly linked list using swap function. My question is does the swap function swap the pointer, and not just the data? My code shows me it only swap the data but not the pointers. Is there any way to efficiently to swap the pointers on the linked list? Please show me the code as I am very inexperienced in coding and I do not understand other codes in other answer.
void sortPoly(PolyNode* a)
{
PolyNode* head =a;
PolyNode* current = head;
PolyNode* current_next = current->next;
int len =Polylength(current);
if(len ==1 || len ==0)
{
return;
}
for(int i =0; i < len; i++)
{
for (int j =0; j< len -i; j++)
{
int sum = current->expx + current->expy;
cout << "sum=" << sum << endl;
int next_sum = current_next->expx + current_next->expy;
cout << "\t nextsum=" << next_sum << endl;
if( sum < next_sum)
{
cout << "current=" << current->coef << "expx = " << current->expx << "expy=" << current->expy << endl;
cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;
std:: swap(current, current_next);
cout << endl;
cout << "swapped" << endl;
cout << "current=" << current->coef << "expx = " << current->expx << "expy=" << current->expy << endl;
cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;
cout << "current=" << current->coef << "expx = " << current->expx << "expy=" << current->expy << endl;
cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;
current = current->next;
current_next = current->next->next;
cout << "current=" << current->coef << "expx = " << current->expx << "expy=" << current->expy << endl;
cout << "current_next=" << current_next->coef << "expx = " << current_next->expx << "expy=" << current_next->expy << endl;
}
}
}
This is my struct:
struct PolyNode
{
int coef;
int expx;
int expy;
PolyNode* prev;
PolyNode* next;
};
