Организовать функцию 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);
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;
Node* tail;
};
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;
if (head == nullptr) {
current->previous = nullptr;
head = tail = current;
}
else
{
current->previous = tail;
tail->next = current;
tail = current;
}
Size++;
}
void List::swap_list(const int index1, const int index2)
{
Node* first = head;
for (int i = 0; i < index1; i++)
{
first = first->next;
}
Node* second = head;
for (int i = 0; i < index2; i++)
{
second = second->next;
}
Node temp;
Node* a_next, * b_next;
a_next = first->next;
b_next = second->next;
temp = *first;
*first = *second;
*second = temp;
first->next = a_next;
second->next = b_next;
}
void List::print_list_first()
{
Node* current = this->head;
while (current != nullptr) {
cout << current->a << " " << *(current->b) << " " << current->c <<
endl;
current = current->next;
}
}
void List::print_list_from_end()
{
Node* current = this->tail;
while (current != nullptr) {
cout << current->a << " " << *(current->b) << " " << current->c << endl;
current = current->previous;
}
}
int main() {
setlocale(LC_ALL, "Rus");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
List onе;
float a = 4.5;
onе.push_back(1, &a, 1);
onе.push_back(2, &a, 2);
onе.push_back(3, &a, 3);
onе.push_back(4, &a, 4);
onе.print_list_first();
onе.swap_list(1,2);
cout << endl << endl;
onе.print_list_first();
cout << endl << endl;
onе.print_list_from_end();
}
Проблема состоит в том что где-то ошибка с указателями возникла и в итоге при выводе списка с хвоста показывает только хвост, а если с головы до хвоста то все работает хорошо.
Специально для Harry. Вот результат роботы того метода который Вы предложили:

Вот так выглядят функции swap, print_list_first, print_lise_from_end :
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;
}
if (a->previous) a->previous->next = b;
if (b->previous) b->previous->next = a;
if (a->next) a->next->previous = b;
if (b->next) b->next->previous = a;
Node* t;
t = a->previous;
a->previous = b->previous;
b->previous = t;
t = a->next;
a->next = b->next;
b->next = t;
}
void List::print_list_first()
{
Node* current = this->head;
while (current != nullptr) {
cout << current->a << " " << *(current->b) << " " << current->c <<
endl;
current = current->next;
}
}
void List::print_list_from_end()
{
Node* current = this->tail;
while (current != nullptr) {
cout << current->a << " " << *(current->b) << " " << current->c <<
endl;
current = current->previous;
}
}
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
А кто будет обновлять указатель prev или как там он у вас называется?
void List::swap_list(const int index1, const int index2)
{
Node* t;
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;
}
if (a->next != b && b->next != a)
{
if (a->previous) a->previous->next = b;
if (b->previous) b->previous->next = a;
if (a->next) a->next->previous = b;
if (b->next) b->next->previous = a;
t = a->previous;
a->previous = b->previous;
b->previous = t;
t = a->next;
a->next = b->next;
b->next = t;
}
else
{
if (b->next == a) { t = a; a = b; b = t; }
b->previous = a->previous;
if (a->previous) a->previous->next = b;
a->next = b->next;
if (b->next) b->next->previous = a;
a->previous = b;
b->next = a;
}
if (head == a) head = b;
else if (head == b) head = a;
if (tail == a) tail = b;
else if (tail == b) tail = a;
}