Product of the node values of a Singly Linked List
Here given code implementation process.
// C Program
// Product of the node values of a Singly Linked List
#include <stdio.h>
#include <stdlib.h> //for malloc function
// Linked List LinkNode
struct LinkNode
{
int data;
struct LinkNode *next;
};
// Singly linked list
struct SingleLL
{
struct LinkNode *head;
struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->head = NULL;
sll->tail = NULL;
}
return sll;
}
// Returns a new Node of linked list
struct LinkNode *createLinkNode(int data)
{
// Create dynamic node
struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
// Add new Node at end of linked list
void addNode(struct SingleLL *sll, int data)
{
struct LinkNode *node = createLinkNode(data);
if (sll->head == NULL)
{
sll->head = node;
}
else
{
// Append the node at last position
sll->tail->next = node;
}
sll->tail = node;
}
// Display linked list element
void display(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
struct LinkNode *temp = sll->head;
// iterating linked list elements
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}
// Find the product of all nodes in linked list
void productOfNodes(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
// Define auxiliary variable
struct LinkNode *auxiliary = sll->head;
int product = 1;
// iterate linked list elements
while (auxiliary != NULL)
{
// Calculate Product
product *= auxiliary->data;
// Visit to next node
auxiliary = auxiliary->next;
}
display(sll);
// Display Product
printf(" Product : %d \n", product);
}
int main()
{
// Define linked list
struct SingleLL *sll = newLinkedList();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
addNode(sll, 1);
addNode(sll, 2);
addNode(sll, 5);
addNode(sll, 3);
addNode(sll, -1);
addNode(sll, 4);
productOfNodes(sll);
return 0;
}
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
/*
Java Program
Product of the node values of a Singly Linked List
*/
// Linked list node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
this.head = null;
this.tail = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
//iterating linked list elements
while (temp != null)
{
System.out.print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
System.out.print(" NULL\n");
}
// Find the product of all nodes in linked list
public void productOfNodes()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
// Define auxiliary variable
LinkNode auxiliary = this.head;
int product = 1;
// iterate linked list elements
while (auxiliary != null)
{
// Calculate Product
product *= auxiliary.data;
// Visit to next node
auxiliary = auxiliary.next;
}
this.display();
// Display Product
System.out.print(" Product : " + product + " \n");
}
public static void main(String[] args)
{
SingleLL sll = new SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1);
sll.addNode(2);
sll.addNode(5);
sll.addNode(3);
sll.addNode(-1);
sll.addNode(4);
sll.productOfNodes();
}
}
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Product of the node values of a Singly Linked List
*/
// Linked list node
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode(int data)
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
public: LinkNode *head;
LinkNode *tail;
SingleLL()
{
this->head = NULL;
this->tail = NULL;
}
//Add new Node at end of linked list
void addNode(int data)
{
LinkNode *node = new LinkNode(data);
if (this->head == NULL)
{
this->head = node;
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
// Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
LinkNode *temp = this->head;
//iterating linked list elements
while (temp != NULL)
{
cout << " " << temp->data << " →";
// Visit to next node
temp = temp->next;
}
cout << " NULL\n";
}
// Find the product of all nodes in linked list
void productOfNodes()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
// Define auxiliary variable
LinkNode *auxiliary = this->head;
int product = 1;
// iterate linked list elements
while (auxiliary != NULL)
{
// Calculate Product
product *= auxiliary->data;
// Visit to next node
auxiliary = auxiliary->next;
}
this->display();
// Display Product
cout << " Product : " << product << " \n";
}
};
int main()
{
SingleLL sll = SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1);
sll.addNode(2);
sll.addNode(5);
sll.addNode(3);
sll.addNode(-1);
sll.addNode(4);
sll.productOfNodes();
return 0;
}
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
// Include namespace system
using System;
/*
C# Program
Product of the node values of a Singly Linked List
*/
// Linked list node
public class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
this.head = null;
this.tail = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
//iterating linked list elements
while (temp != null)
{
Console.Write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
// Find the product of all nodes in linked list
public void productOfNodes()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
// Define auxiliary variable
LinkNode auxiliary = this.head;
int product = 1;
// iterate linked list elements
while (auxiliary != null)
{
// Calculate Product
product *= auxiliary.data;
// Visit to next node
auxiliary = auxiliary.next;
}
this.display();
// Display Product
Console.Write(" Product : " + product + " \n");
}
public static void Main(String[] args)
{
SingleLL sll = new SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1);
sll.addNode(2);
sll.addNode(5);
sll.addNode(3);
sll.addNode(-1);
sll.addNode(4);
sll.productOfNodes();
}
}
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
<?php
/*
Php Program
Product of the node values of a Singly Linked List
*/
// Linked list node
class LinkNode
{
public $data;
public $next;
function __construct($data)
{
$this->data = $data;
$this->next = null;
}
}
class SingleLL
{
public $head;
public $tail;
function __construct()
{
$this->head = null;
$this->tail = null;
}
//Add new Node at end of linked list
public function addNode($data)
{
$node = new LinkNode($data);
if ($this->head == null)
{
$this->head = $node;
}
else
{
// Append the node at last position
$this->tail->next = $node;
}
$this->tail = $node;
}
// Display linked list element
public function display()
{
if ($this->head == null)
{
echo "\n Empty linked list\n";
return;
}
$temp = $this->head;
//iterating linked list elements
while ($temp != null)
{
echo " ". $temp->data ." →";
// Visit to next node
$temp = $temp->next;
}
echo " NULL\n";
}
// Find the product of all nodes in linked list
public function productOfNodes()
{
if ($this->head == null)
{
echo "\n Empty linked list\n";
return;
}
// Define auxiliary variable
$auxiliary = $this->head;
$product = 1;
// iterate linked list elements
while ($auxiliary != null)
{
// Calculate Product
$product *= $auxiliary->data;
// Visit to next node
$auxiliary = $auxiliary->next;
}
$this->display();
// Display Product
echo " Product : ". $product ." \n";
}
}
function main()
{
$sll = new SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
$sll->addNode(1);
$sll->addNode(2);
$sll->addNode(5);
$sll->addNode(3);
$sll->addNode(-1);
$sll->addNode(4);
$sll->productOfNodes();
}
main();
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
/*
Node Js Program
Product of the node values of a Singly Linked List
*/
// Linked list node
class LinkNode
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.head = null;
this.tail = null;
}
//Add new Node at end of linked list
addNode(data)
{
var node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
var temp = this.head;
//iterating linked list elements
while (temp != null)
{
process.stdout.write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
process.stdout.write(" NULL\n");
}
// Find the product of all nodes in linked list
productOfNodes()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
// Define auxiliary variable
var auxiliary = this.head;
var product = 1;
// iterate linked list elements
while (auxiliary != null)
{
// Calculate Product
product *= auxiliary.data;
// Visit to next node
auxiliary = auxiliary.next;
}
this.display();
// Display Product
process.stdout.write(" Product : " + product + " \n");
}
}
function main()
{
var sll = new SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1);
sll.addNode(2);
sll.addNode(5);
sll.addNode(3);
sll.addNode(-1);
sll.addNode(4);
sll.productOfNodes();
}
main();
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
# Python 3 Program
# Product of the node values of a Singly Linked List
# Linked list node
class LinkNode :
def __init__(self, data) :
self.data = data
self.next = None
class SingleLL :
def __init__(self) :
self.head = None
self.tail = None
# Add new Node at end of linked list
def addNode(self, data) :
node = LinkNode(data)
if (self.head == None) :
self.head = node
else :
# Append the node at last position
self.tail.next = node
self.tail = node
# Display linked list element
def display(self) :
if (self.head == None) :
print("\n Empty linked list")
return
temp = self.head
# iterating linked list elements
while (temp != None) :
print("", temp.data ,"→", end = "")
# Visit to next node
temp = temp.next
print(" NULL")
# Find the product of all nodes in linked list
def productOfNodes(self) :
if (self.head == None) :
print("\n Empty linked list")
return
# Define auxiliary variable
auxiliary = self.head
product = 1
# iterate linked list elements
while (auxiliary != None) :
# Calculate Product
product *= auxiliary.data
# Visit to next node
auxiliary = auxiliary.next
self.display()
# Display Product
print(" Product : ", product ," ")
def main() :
sll = SingleLL()
# Constructed linked list
# 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1)
sll.addNode(2)
sll.addNode(5)
sll.addNode(3)
sll.addNode(-1)
sll.addNode(4)
sll.productOfNodes()
if __name__ == "__main__": main()
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
# Ruby Program
# Product of the node values of a Singly Linked List
# Linked list node
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end
end
class SingleLL
# Define the accessor and reader of class SingleLL
attr_reader :head, :tail
attr_accessor :head, :tail
def initialize()
self.head = nil
self.tail = nil
end
# Add new Node at end of linked list
def addNode(data)
node = LinkNode.new(data)
if (self.head == nil)
self.head = node
else
# Append the node at last position
self.tail.next = node
end
self.tail = node
end
# Display linked list element
def display()
if (self.head == nil)
print("\n Empty linked list\n")
return
end
temp = self.head
# iterating linked list elements
while (temp != nil)
print(" ", temp.data ," →")
# Visit to next node
temp = temp.next
end
print(" NULL\n")
end
# Find the product of all nodes in linked list
def productOfNodes()
if (self.head == nil)
print("\n Empty linked list\n")
return
end
# Define auxiliary variable
auxiliary = self.head
product = 1
# iterate linked list elements
while (auxiliary != nil)
# Calculate Product
product *= auxiliary.data
# Visit to next node
auxiliary = auxiliary.next
end
self.display()
# Display Product
print(" Product : ", product ," \n")
end
end
def main()
sll = SingleLL.new()
# Constructed linked list
# 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1)
sll.addNode(2)
sll.addNode(5)
sll.addNode(3)
sll.addNode(-1)
sll.addNode(4)
sll.productOfNodes()
end
main()
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
/*
Scala Program
Product of the node values of a Singly Linked List
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
def this(data: Int)
{
this(data, null);
}
}
class SingleLL(var head: LinkNode , var tail: LinkNode)
{
def this()
{
this(null, null);
}
//Add new Node at end of linked list
def addNode(data: Int): Unit = {
var node: LinkNode = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
def display(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode = this.head;
//iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
// Find the product of all nodes in linked list
def productOfNodes(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// Define auxiliary variable
var auxiliary: LinkNode = this.head;
var product: Int = 1;
// iterate linked list elements
while (auxiliary != null)
{
// Calculate Product
product *= auxiliary.data;
// Visit to next node
auxiliary = auxiliary.next;
}
this.display();
// Display Product
print(" Product : " + product + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll: SingleLL = new SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1);
sll.addNode(2);
sll.addNode(5);
sll.addNode(3);
sll.addNode(-1);
sll.addNode(4);
sll.productOfNodes();
}
}
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
/*
Swift 4 Program
Product of the node values of a Singly Linked List
*/
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
var head: LinkNode? ;
var tail: LinkNode? ;
init()
{
self.head = nil;
self.tail = nil;
}
//Add new Node at end of linked list
func addNode(_ data: Int)
{
let node: LinkNode? = LinkNode(data);
if (self.head == nil)
{
self.head = node;
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
// Display linked list element
func display()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
var temp: LinkNode? = self.head;
//iterating linked list elements
while (temp != nil)
{
print("", temp!.data ,"→", terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" NULL");
}
// Find the product of all nodes in linked list
func productOfNodes()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
// Define auxiliary variable
var auxiliary: LinkNode? = self.head;
var product: Int = 1;
// iterate linked list elements
while (auxiliary != nil)
{
// Calculate Product
product *= auxiliary!.data;
// Visit to next node
auxiliary = auxiliary!.next;
}
self.display();
// Display Product
print(" Product : ", product ," ");
}
}
func main()
{
let sll: SingleLL = SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1);
sll.addNode(2);
sll.addNode(5);
sll.addNode(3);
sll.addNode(-1);
sll.addNode(4);
sll.productOfNodes();
}
main();
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
/*
Kotlin Program
Product of the node values of a Singly Linked List
*/
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
var head: LinkNode ? ;
var tail: LinkNode ? ;
constructor()
{
this.head = null;
this.tail = null;
}
//Add new Node at end of linked list
fun addNode(data: Int): Unit
{
var node: LinkNode = LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail?.next = node;
}
this.tail = node;
}
// Display linked list element
fun display(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode ? = this.head;
//iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
// Find the product of all nodes in linked list
fun productOfNodes(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// Define auxiliary variable
var auxiliary: LinkNode ? = this.head;
var product: Int = 1;
// iterate linked list elements
while (auxiliary != null)
{
// Calculate Product
product *= auxiliary.data;
// Visit to next node
auxiliary = auxiliary.next;
}
this.display();
// Display Product
print(" Product : " + product + " \n");
}
}
fun main(args: Array <String> ): Unit
{
var sll: SingleLL = SingleLL();
// Constructed linked list
// 1 → 2 → 5 → 3 → -1 → 4 → NULL
sll.addNode(1);
sll.addNode(2);
sll.addNode(5);
sll.addNode(3);
sll.addNode(-1);
sll.addNode(4);
sll.productOfNodes();
}
Output
1 → 2 → 5 → 3 → -1 → 4 → NULL
Product : -120
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment