単一リンクリストのノードの積
n個のノードがある場合、タスクは、単一リンクリストのすべてのノードの積を出力することです。プログラムは、最初のノードからNULLが見つからなくなるまで、単一リンクリストのすべてのノードをトラバースする必要があります。
例
Input -: 1 2 3 4 5 Output -: 120
上記の例では、最初のノードから開始して、すべてのノードがトラバースされます。つまり、1、2、3、4、5、6であり、それらの積は1 * 2 * 3 * 4 * 5 * 6 =120
以下で使用するアプローチは次のとおりです
- 一時的なポインタ、たとえばノードタイプの温度を取得します
- この一時ポインタを、ヘッドポインタが指す最初のノードに設定します
- tempがNULLでないときに、tempをtemp->nextに移動します。
- set product =product *(temp-> data)
アルゴリズム
Start Step 1 -> create structure of a node and temp, next and head as pointer to a structure node struct node int data struct node *next, *head, *temp End Step 2 -> declare function to insert a node in a list void insert(int val) struct node* newnode = (struct node*)malloc(sizeof(struct node)) newnode->data = val IF head= NULL set head = newnode set head->next = NULL End Else Set temp=head Loop While temp->next!=NULL Set temp=temp->next End Set newnode->next=NULL Set temp->next=newnode End Step 3 -> Declare a function to display list void display() IF head=NULL Print no node End Else Set temp=head Loop While temp!=NULL Print temp->data Set temp=temp->next End End Step 4 -> declare a function to find alternate nodes void product_nodes() declare int product=1 Set temp=head Loop While temp!=NULL Set product=product * (temp->data) Set temp=temp->next End Print product Step 5 -> in main() Create nodes using struct node* head = NULL; Call function insert(10) to insert a node Call display() to display the list Call product_nodes() to find alternate nodes product Stop
例
#include<stdio.h>
#include<stdlib.h>
//structure of a node
struct node{
int data;
struct node *next;
}*head,*temp;
//function for inserting nodes into a list
void insert(int val){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = val;
newnode->next = NULL;
if(head == NULL){
head = newnode;
temp = head;
} else {
temp->next=newnode;
temp=temp->next;
}
}
//function for displaying a list
void display(){
if(head==NULL)
printf("no node ");
else{
temp=head;
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
}
}
//function for finding product
void product_nodes(){
int product=1;
temp=head;
while(temp!=NULL){
product=product * (temp->data);
temp=temp->next;
}
printf("\nproduct of nodes is : %d" ,product);
}
int main(){
//creating list
struct node* head = NULL;
//inserting elements into a list
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
//displaying the list
printf("linked list is : ");
display();
//calling function for finding prodouct
Product_nodes();
return 0;
} 出力
linked list is : 1 2 3 4 5 6 product of nodes is : 720
-
C++の単一リンクリスト内のすべての素数ノードの積
n個のノードが与えられ、タスクはリンクリスト内のすべての素数ノードの積を出力することです。プライムノードは、カウント位置としてプライム値を持つノードです。 入力 10 20 30 40 50 出力 4,00,000 説明 − 10はプライムではないインデックス値1にあるため、スキップされます。素数であるインデックス値2で20に移動すると、考慮されます。同様に、40と50はプライムインデックスの場所にあります。 製品 − 20 * 40 * 50 =4,00,000 上の図で、赤い色のノードはプライムノードを表しています 以下で使用するアプローチは次のとおりです 一時
-
C++の循環リンクリストのノードの合計
この問題では、循環リンクリストが表示されます。私たちのタスクは、循環リンクリストのノードの合計を見つけるプログラムを作成することです。 リンクリストのすべてのノード値を追加するだけです。 いくつかの重要な定義 リンクリストは一連のデータ構造であり、リンクを介して相互に接続されています。 循環リンクリストは、最初の要素が最後の要素を指し、最後の要素が最初の要素を指すリンクリストのバリエーションです。単一リンクリストと二重リンクリストの両方を循環リンクリストにすることができます。 では、問題を理解するために例を見てみましょう。 入力 14 ->