原文:https://www.geeksforgeeks.org/product-of-all-prime-nodes-in-a-doubly-linked-list/
给定一个包含n
个节点的双链表。 任务是找到所有质数节点的乘积。
示例:
输入:
list = 15 <=> 16 <=> 6 <=> 7 <=> 17
输出:质数节点乘积:119
输入:
list = 5 <=> 3 <=> 4 <=> 2 <=> 9
输出:质数节点乘积:30
方法:
-
用链表的开头初始化一个指针
temp
,并用 1 初始化一个product
变量。 -
使用循环开始遍历链表,直到遍历所有节点。
-
如果节点值是质数,则将当前节点的值乘以乘积,即
product *= current_node->data
。 -
递增指向链表的下一个节点的指针,即
temp = temp->next
。 -
返回乘积。
下面是上述方法的实现:
c
// c implementation to product all
// prime nodes from the doubly
// linked list
#include
using namespace std;
// node of the doubly linked list
struct node {
int data;
node *prev, *next;
};
// function to insert a node at the beginning
// of the doubly linked list
void push(node** head_ref, int new_data)
{
// allocate node
node* new_node = (node*)malloc(sizeof(struct node));
// put in the data
new_node->data = new_data;
// since we are adding at the beginning,
// prev is always null
new_node->prev = null;
// link the old list off the new node
new_node->next = (*head_ref);
// change prev of head node to new node
if ((*head_ref) != null)
(*head_ref)->prev = new_node;
// move the head to point to the new node
(*head_ref) = new_node;
}
// function to check if a number is prime
bool isprime(int n)
{
// corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// this is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i 6)
if (n % i == 0 || n % (i 2) == 0)
return false;
return true;
}
// function to product all prime nodes
// from the doubly linked list
int prodofprime(node** head_ref)
{
node* ptr = *head_ref;
node* next;
// variable prod = 1 for multiplying nodes value
int prod = 1;
// travese list till last node
while (ptr != null) {
next = ptr->next;
// if number is prime then
// multiply to product
if (isprime(ptr->data))
prod = prod * ptr->data;
ptr = next;
}
// return product
return prod;
}
// driver program
int main()
{
// start with the empty list
node* head = null;
// create the doubly linked list
// 15 <-> 16 <-> 7 <-> 6 <-> 17
push(&head, 17);
push(&head, 6);
push(&head, 7);
push(&head, 16);
push(&head, 15);
int prod = prodofprime(&head);
cout << "product of prime nodes : " << prod;
return 0;
}
java
// java implementation to product all
// prime nodes from the doubly
// linked list
// node of the doubly linked list
class node
{
int data;
node next, prev;
node(int d)
{
data = d;
next = null;
prev = null;
}
}
class dll
{
// function to insert a node at the beginning
// of the doubly linked list
static node push(node head, int data)
{
node newnode = new node(data);
newnode.next = head;
newnode.prev = null;
if (head != null)
head.prev = newnode;
head = newnode;
return head;
}
// function to check if a number is prime
static boolean isprime(int n)
{
// corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// this is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i 6)
if (n % i == 0 || n % (i 2) == 0)
return false;
return true;
}
// function to product all prime nodes
// from the doubly linked list
static int prodofprime(node node)
{
// variable prod = 1 for multiplying nodes value
int prod = 1;
// travese list till last node
while (node != null)
{
// check is node value is prime
// if true then multiply to prod
if (isprime(node.data))
prod *= node.data;
node = node.next;
}
// return product
return prod;
}
// driver program
public static void main(string[] args)
{
// start with empty list
node head = null;
// create the doubly linked list
// 15 <-> 16 <-> 7 <-> 6 <-> 17
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
int prod = prodofprime(head);
system.out.println("product of prime nodes: " prod);
}
}
// this code is contributed by vivekkumar singh
python3
# python3 implementation to product all
# prime nodes from the doubly
# linked list
# node of the doubly linked list
class node:
def __init__(self, data):
self.data = data
self.prev = none
self.next = none
# function to insert a node at the beginning
# of the doubly linked list
def push(head_ref, new_data):
# allocate node
new_node = node(0)
# put in the data
new_node.data = new_data
# since we are multiplying at the beginning,
# prev is always none
new_node.prev = none
# link the old list off the new node
new_node.next = (head_ref)
# change prev of head node to new node
if ((head_ref) != none):
(head_ref).prev = new_node
# move the head to point to the new node
(head_ref) = new_node
return head_ref
# function to check if a number is prime
def isprime(n):
# corner cases
if (n <= 1):
return false
if (n <= 3):
return true
# this is checked so that we can skip
# middle five numbers in below loop
if (n % 2 == 0 or n % 3 == 0):
return false
i = 5
while ( i * i <= n ):
if (n % i == 0 or n % (i 2) == 0):
return false
i = 6;
return true
# function to product all prime nodes
# from the doubly linked list
def prodofprime(head_ref):
ptr = head_ref
next = none
# variable prod = 1 for multiplying nodes value
prod = 1
# travese list till last node
while (ptr != none):
next = ptr.next
# if number is prime then
# multiply to product
if (isprime(ptr.data)):
prod = prod * ptr.data
ptr = next
# return product
return prod
# driver code
if __name__ == "__main__":
# start with the empty list
head = none
# create the doubly linked list
# 15 <. 16 <. 7 <. 6 <. 17
head = push(head, 17)
head = push(head, 6)
head = push(head, 7)
head = push(head, 16)
head = push(head, 15)
prod = prodofprime(head)
print("product of prime nodes : ", prod)
# this code is contributed by arnab kundu
c
// c# implementation to product all
// prime nodes from the doubly
// linked list
using system;
// node of the doubly linked list
public class node
{
public int data;
public node next, prev;
public node(int d)
{
data = d;
next = null;
prev = null;
}
}
class dll
{
// function to insert a node at the beginning
// of the doubly linked list
static node push(node head, int data)
{
node newnode = new node(data);
newnode.next = head;
newnode.prev = null;
if (head != null)
head.prev = newnode;
head = newnode;
return head;
}
// function to check if a number is prime
static boolean isprime(int n)
{
// corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// this is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i 6)
if (n % i == 0 || n % (i 2) == 0)
return false;
return true;
}
// function to product all prime nodes
// from the doubly linked list
static int prodofprime(node node)
{
// variable prod = 1 for multiplying nodes value
int prod = 1;
// travese list till last node
while (node != null)
{
// check is node value is prime
// if true then multiply to prod
if (isprime(node.data))
prod *= node.data;
node = node.next;
}
// return product
return prod;
}
// driver code
public static void main(string []args)
{
// start with empty list
node head = null;
// create the doubly linked list
// 15 <-> 16 <-> 7 <-> 6 <-> 17
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
int prod = prodofprime(head);
console.writeline("product of prime nodes: " prod);
}
}
// this code is contributed by arnab kundu
输出:
product of prime nodes : 119
时间复杂度:o(n)
,其中n
是节点数。
如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。
如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处