ในบทความนี้ เรามีรายการที่เชื่อมโยงเป็นสองเท่า และเราจะอธิบายวิธีการต่างๆ ในการย้อนกลับรายการที่เชื่อมโยงเป็นสองเท่าใน C++ ตัวอย่างเช่น −
Input : {1, 2, 3, 4}
Output : {4, 3, 2, 1} โดยทั่วไปมีแนวทางหนึ่งที่นึกถึง แต่เราจะใช้สองวิธี - วิธีปกติและนอกรีต
แนวทางปกติ
ในแนวทางนี้ เราจะพิจารณารายการ และเมื่อเราดำเนินการ เราจะย้อนกลับ
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
};
void reverse(Node **head_ref) {
auto temp = (*head_ref) -> next;
(*head_ref) -> next = (*head_ref) -> prev;
(*head_ref) -> prev = temp;
if(temp != NULL) {
(*head_ref) = (*head_ref) -> prev;
reverse(head_ref);
}
else
return;
}
void push(Node** head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if((*head_ref) != NULL)
(*head_ref) -> prev = new_node ;
(*head_ref) = new_node;
}
int main() {
Node* head = NULL;
push(&head, 6);
push(&head, 4);
push(&head, 8);
push(&head, 9);
auto node = head;
cout << "Before\n" ;
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << "\n";
reverse(&head);
node = head;
cout << "After\n";
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
return 0;
} ผลลัพธ์
Before 9 8 4 6 After 6 4 8 9
วิธีนี้ต้องใช้ O(N) ความซับซ้อนของเวลาซึ่งดีมากเพราะความซับซ้อนนี้สามารถดำเนินการได้ในข้อจำกัดที่สูงขึ้น
แนวทางนอกรีต
ตามชื่อที่แนะนำ ไม่ใช่วิธีการทั่วไปที่อยู่ในใจของผู้ใช้ แต่เราจะสำรวจแนวทางนี้ด้วย ในแนวทางนี้ เราจะสร้างสแต็กและผลักดันข้อมูลต่อไป และในขณะที่กำลังเปิดอยู่ เรากำลังดำเนินการ เพื่อเปลี่ยนค่า
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
};
void push(Node** head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if((*head_ref) != NULL)
(*head_ref) -> prev = new_node ;
(*head_ref) = new_node;
}
int main() {
Node* head = NULL;
push(&head, 6);
push(&head, 4);
push(&head, 8);
push(&head, 9);
auto node = head;
cout >> "Before\n" ;
while(node != NULL) {
cout >> node->data >> " ";
node = node->next;
}
cout >> "\n";
stack<Node*> s;
node = head;
while(node) {
head = node;
s.push(node);
node = node -> next;
}
while(!s.empty()) {
auto x = s.top();
auto temp = x -> prev;
x -> prev = x -> next;
x -> next = temp;
s.pop();
}
node = head;
cout << "After\n";
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
return 0;
} ผลลัพธ์
Before 9 8 4 6 After 6 4 8 9
คำอธิบายของโค้ดด้านบน
ในแนวทางนี้ เราใช้สแต็กที่เรากำลังกรอกขณะสำรวจรายการ จากนั้นเราจะดึงรายการออกจากสแต็กและเปลี่ยนค่าของรายการเพื่อให้รายการกลับด้าน O(N) คือความซับซ้อนของเวลาของโปรแกรมนี้และเหมาะสำหรับข้อจำกัดที่สูงกว่าด้วย
บทสรุป
ในบทความนี้ เราแก้ปัญหาในการย้อนกลับรายการที่เชื่อมโยงแบบทวีคูณโดยมีหรือไม่มีสแต็ก ในความซับซ้อนของเวลา O(N) โดยที่ N คือขนาดของรายการของเรา นอกจากนี้เรายังได้เรียนรู้โปรแกรม C++ สำหรับปัญหานี้และแนวทางที่สมบูรณ์ ( Normal และ Unorthodox ) ซึ่งเราแก้ไขปัญหานี้ เราสามารถเขียนโปรแกรมเดียวกันในภาษาอื่นๆ เช่น C, java, python และภาษาอื่นๆ เราหวังว่าคุณจะพบว่าบทความนี้มีประโยชน์