Đề bài yêu cầu đổi thứ tự của linked list theo cặp
VD: 1->2->3->4… => 2->1->4->3…
em code bị lỗi ở dòng này ạ, không biết sửa thế nào, mọi người xem giúp em với:
SinglyLinkedListNode* temp = root;
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(int node_data) {
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
void print_singly_linked_list(SinglyLinkedListNode* node, string sep) {
while (node) {
cout << node->data;
node = node->next;
if (node) {
cout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;
free(temp);
}
}
void pairWiseSwap(SinglyLinkedList* root)
{
SinglyLinkedListNode* temp = root;
while (temp != NULL && temp->next != NULL) {
swap(temp->data,
temp->next->data);
temp = temp->next->next;
}
}
int main()
{
return 0;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?