Организовать функцию swap для двухсвязного кольцевого списка С++

Здравствуйте нужно организовать функцию swap для двухсвязного кольцевого списка. В моем варианте происходит функция swap только в центре списка, то есть если захочу поменять главный элемент с каким либо другим или также с хвостом то функция начинает работать не корректно. Мой код выглядит следующим образом:

#include <iostream>
#include <ctime>
#include <fstream>
#include<Windows.h>
#include <string>
using namespace std;

class List
{
public:
   List();
   ~List();

   void push_back(double a, float* b, unsigned int c);
   int get_size();
   void clear();
   void print_list_first();
   void print_list_from_end();
   void delete_node(int index);
   void swap_list(const int index1, const int index2);
   List operator()(List obj);
   List read_file(string name);
   void write_file_first(string name);
   void write_file_from_end(string name);
   void head_nullptr();

private:

    class Node
    {
    public:
       double a;
       float* b;
       unsigned int c;

       Node* next;
       Node* previous;

       Node() {};

       Node(double a, float* b, unsigned int c, Node* next = nullptr, Node* previous = nullptr) {
            this->a = a;
            this->b = b;
            this->c = c;
            this->next = next;
            this->previous = previous;
       }

    };

      int Size;
      Node* head;

};

List::List()
{
   Size = 0;
   head = nullptr;
}

List::~List()
{
}

void List::push_back(double a, float* b, unsigned int c)
{
    Node* current = new Node(a, b, c);
    current->next = nullptr;
    current->previous = nullptr;

    if (head == nullptr) {

        head = current;
        head->next = head;
        head->previous = head;
    }
    else {

        Node* temp = head;
        while (temp->next!=head) {
           temp = temp->next;
        }
        temp->next = current;
        current->next = head;
        current->previous = temp;
        head->previous = current;

    }

    Size++;
}

void List::print_list_first()
{ 
    Node* current = head;

    while (true) {
        cout << current->a << " " << *(current->b) << " " << current->c << endl;
        current = current->next;
        if (current == head)
           break;
    }

}

void List::print_list_from_end()
{
     Node* current = head->previous;

     while (true) {
         cout << current->a << " " << *(current->b) << " " << current->c << endl;
         current = current->previous;
         if (current == head->previous)
                   break;
     }
}

void List::swap_list(const int index1, const int index2)
{
     Node* a = head;
     for (int i = 0; i < index1; i++)
     {
        a = a->next;
     }

      Node* b = head;
     for (int i = 0; i < index2; i++)
     {
         b = b->next;
     }

     Node* prev1, * prev2, * next1, * next2;

     prev1 = a->previous; 
     prev2 = b->previous; 
     next1 = a->next; 
     next2 = b->next; 
     if (b == next1) 
     {
         b->next = a;
         b->previous = prev1;
         a->next = next2;
         a->previous = b;
         next2->previous = a;
         prev1->next = b;
     }
     else if (a == next2) 
     {
        a->next = b;
        a->previous = prev2;
        b->next = next1;
        b->previous = a;
        next1->previous = b;
        prev2->next = a;
    }
    else  
    {
        prev1->next = b;
        b->next = next1;
        prev2->next = a;
        a->next = next2;
        b->previous = prev1;
        next2->previous = a;
        a->previous = prev2;
        next1->previous = b;
   }

}


int main() {
   setlocale(LC_ALL, "Rus");
   SetConsoleCP(1251);
   SetConsoleOutputCP(1251);

   List lst;
   float a = 4.4;

   lst.push_back(1, &a, 1);
   lst.push_back(2, &a, 2);
   lst.push_back(3, &a, 3);
   lst.push_back(4, &a, 4);

   cout << "Работа функции  print_list_first() >>" << endl;
   lst.print_list_first();
   cout << endl << endl;
   cout << "Работа функции  print_list_from_end() >>" << endl;
   lst.print_list_from_end();
   cout << endl << endl;

   cout << "Работа функции swap(1,2)"<<endl;
   lst.swap_list(1, 2);

   cout << "Работа функции  print_list_first() >>" << endl;
   lst.print_list_first();
   cout << endl << endl;
   cout << "Работа функции  print_list_from_end() >>" << endl;
   lst.print_list_from_end();
   cout << endl << endl;

}

Вот в если сделать swap 1 и 2 элемента будет вот это: введите сюда описание изображения

А если допустим захочу поменять местами 0 и 1 то получается вот это: введите сюда описание изображения


Ответы (0 шт):