C++の単一リンクリスト内のすべての素数ノードの積
n個のノードが与えられ、タスクはリンクリスト内のすべての素数ノードの積を出力することです。プライムノードは、カウント位置としてプライム値を持つノードです。
入力
10 20 30 40 50
出力
4,00,000
説明 − 10はプライムではないインデックス値1にあるため、スキップされます。素数であるインデックス値2で20に移動すると、考慮されます。同様に、40と50はプライムインデックスの場所にあります。
製品 − 20 * 40 * 50 =4,00,000
上の図で、赤い色のノードはプライムノードを表しています
以下で使用するアプローチは次のとおりです
-
一時的なポインタを取ります。たとえば、ノードタイプの温度
-
この一時ポインタを、ヘッドポインタが指す最初のノードに設定します
-
tempをtemp→nextに移動し、ノードがプライムノードであるか非プライムノードであるかを確認します。ノードがプライムノードの場合
-
product =product *(temp→data)
を設定してください -
ノードが素数でない場合は、次のノードに移動します
-
製品変数の最終値を出力します。
アルゴリズム
Start Step 1 → create structure of a node to insert into a list struct node int data; node* next End Step 2 → declare function to insert a node in a list void push(node** head_ref, int data) Set node* newnode = (node*)malloc(sizeof(struct node)) Set newnode→data = data Set newnode→next = (*head_ref) Set (*head_ref) = newnode End Step 3 → Declare a function to check for prime or not bool isPrime(int data) IF data <= 1 return false End IF data <= 3 return true End IF data % 2 = 0 || data % 3 = 0 return false Loop For int i = 5 and i * i <= data and i = i + 6 IFdata % i = 0 || data % (i + 2) = 0 return false End End return true Step 4→ declare a function to calculate product void product(node* head_ref) set int product = 1 set node* ptr = head_ref While ptr != NULL IF (isPrime(ptr→data)) Set product *= ptr→data End Set ptr = ptr→next End Print product Step 5 → In main() Declare node* head = NULL Call push(&head, 10) Call push(&head, 2) Call product(head) Stop
例
#include <bits/stdc++.h>
using namespace std;
//structure of a node
struct node{
int data;
node* next;
};
//function to insert a node
void push(node** head_ref, int data){
node* newnode = (node*)malloc(sizeof(struct node));
newnode→data = data;
newnode→next = (*head_ref);
(*head_ref) = newnode;
}
// Function to check if a number is prime
bool isPrime(int data){
if (data <= 1)
return false;
if (data <= 3)
return true;
if (data % 2 == 0 || data % 3 == 0)
return false;
for (int i = 5; i * i <= data; i = i + 6)
if (data % i == 0 || data % (i + 2) == 0)
return false;
return true;
}
//function to find the product
void product(node* head_ref){
int product = 1;
node* ptr = head_ref;
while (ptr != NULL){
if (isPrime(ptr→data)){
product *= ptr→data;
}
ptr = ptr→next;
}
cout << "Product of all the prime nodes of a linked list = " << product;
}
int main(){
node* head = NULL;
push(&head, 10);
push(&head, 2);
push(&head, 7);
push(&head, 6);
push(&head, 85);
product(head);
return 0;
} 出力
上記のコードを実行すると、次の出力が生成されます-
Product of all the prime nodes of a linked list = 14
-
C++の循環リンクリストのノードの合計
この問題では、循環リンクリストが表示されます。私たちのタスクは、循環リンクリストのノードの合計を見つけるプログラムを作成することです。 リンクリストのすべてのノード値を追加するだけです。 いくつかの重要な定義 リンクリストは一連のデータ構造であり、リンクを介して相互に接続されています。 循環リンクリストは、最初の要素が最後の要素を指し、最後の要素が最初の要素を指すリンクリストのバリエーションです。単一リンクリストと二重リンクリストの両方を循環リンクリストにすることができます。 では、問題を理解するために例を見てみましょう。 入力 14 ->
-
C++の循環リンクリストでノードをカウントします
ノードを含む循環リンクリストが与えられ、タスクは循環リンクリストに存在するノードの数を計算することです。 循環リンクリストは、最初の要素が最後の要素を指し、最後の要素が最初の要素を指すリンクリストのバリエーションです。単一リンクリストと二重リンクリストの両方を循環リンクリストにすることができます。 以下のプログラムでは、単一リンクリストを循環リンクリストとして実装し、その中のノード数を計算しています。 例 Input − nodes-: 20, 1, 2, 3, 4, 5 Output − count of nodes are-: 6 Input &minus