特定のリンクリストのC++ペアワイズスワップ要素
たとえば、リンクリストに存在するペアワイズノードを交換してから印刷する必要があるという問題を解決するには
Input : 1->2->3->4->5->6->NULL Output : 2->1->4->3->6->5->NULL Input : 1->2->3->4->5->NULL Output : 2->1->4->3->5->NULL Input : 1->NULL Output : 1->NULL
ソリューションにアプローチする方法は2つあり、どちらもO(N)の時間計算量を持ちます。ここで、Nは提供されたリンクリストのサイズであるため、次に両方のアプローチを検討します
反復アプローチ
このアプローチでは、リンクリスト要素を繰り返し処理し、NULLに達するまでペアごとに交換します。
例
#include <bits/stdc++.h>
using namespace std;
class Node { // node of our list
public:
int data;
Node* next;
};
void swapPairwise(Node* head){
Node* temp = head;
while (temp != NULL && temp->next != NULL) { // for pairwise swap we need to have 2 nodes hence we are checking
swap(temp->data,
temp->next->data); // swapping the data
temp = temp->next->next; // going to the next pair
}
}
void push(Node** head_ref, int new_data){ // function to push our data in list
Node* new_node = new Node(); // creating new node
new_node->data = new_data;
new_node->next = (*head_ref); // head is pushed inwards
(*head_ref) = new_node; // our new node becomes our head
}
void printList(Node* node){ // utility function to print the given linked list
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main(){
Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
cout << "Linked list before\n";
printList(head);
swapPairwise(head);
cout << "\nLinked list after\n";
printList(head);
return 0;
} 出力
Linked list before 1 2 3 4 5 Linked list after 2 1 4 3 5
次のアプローチでも同じ式を使用しますが、再帰を繰り返します。
再帰的アプローチ
このアプローチでは、再帰を使用して同じロジックを実装しています。
例
#include <bits/stdc++.h>
using namespace std;
class Node { // node of our list
public:
int data;
Node* next;
};
void swapPairwise(struct Node* head){
if (head != NULL && head->next != NULL) { // same condition as our iterative
swap(head->data, head->next->data); // swapping data
swapPairwise(head->next->next); // moving to the next pair
}
return; // else return
}
void push(Node** head_ref, int new_data){ // function to push our data in list
Node* new_node = new Node(); // creating new node
new_node->data = new_data;
new_node->next = (*head_ref); // head is pushed inwards
(*head_ref) = new_node; // our new node becomes our head
}
void printList(Node* node){ // utility function to print the given linked list
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main(){
Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
cout << "Linked list before\n";
printList(head);
swapPairwise(head);
cout << "\nLinked list after\n";
printList(head);
return 0;
} 出力
Linked list before 1 2 3 4 5 Linked list after 2 1 4 3 5
上記のコードの説明
このアプローチでは、リンクリストをペアでトラバースします。ここで、ペアに到達すると、データを交換して次のペアに移動します。これにより、プログラムは両方の方法で進行します。
結論
このチュートリアルでは、再帰と反復を使用して、特定のリンクリストのペアワイズスワップ要素を解決します。また、この問題のC ++プログラムと、この問題を解決するための完全なアプローチ(通常)についても学びました。同じプログラムを、C、java、python、その他の言語などの他の言語で作成できます。このチュートリアルがお役に立てば幸いです。
-
C++のリンクリストの代替ノードの合計
この問題では、リンクリストが表示されます。私たちのタスクは、リンクリストの代替ノードの合計を出力することです。 リンクリストは、リンクを介して相互に接続された一連のデータ構造です。 では、問題に戻りましょう。ここでは、リンクリストの代替ノードを追加します。これは、ノードが位置0、2、4、6、…であることを追加することを意味します 問題を理解するために例を見てみましょう。 入力 4 → 12 → 10 → 76 → 9 → 26 → 1 出力 24 説明 considering alternate strings
-
リンクリストがC++でペアワイズソートされているかどうかを確認します
n個の要素を持つリストLがあります。リストがペアごとにソートされているかどうかを確認する必要があります。リストが{8、10、18、20、5、15}のようなものだとします。これは、(8、10)、(18、20)、(5、15)がソートされるようにペアごとにソートされます。リストに奇数の要素がある場合、最後の要素は無視されます。 アプローチは単純すぎます。数字を左から右にトラバースします。 2つの連続する要素が取得され、それらがソートされているかどうかを確認します。いずれかのペアがソートされていない場合はfalseを返し、ペアが見つからない場合、つまりソートされていない場合はtrueを返します。 例