# Subtraction of the alternate nodes of linked list

Here given code implementation process.

``````// C Program
// Subtraction of the alternate nodes of linked list
#include <stdio.h>
//For malloc function
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
printf("  %d", temp->data);
//visit to next node
temp = temp->next;
}
printf("\n");
}
// Find the subtraction of all alternate nodes in linked list
{
{
return;
}
//Define resultant variables
int result = 0;
int counter = 0;
//Start to first node of linked list
while (temp != NULL)
{
if (counter % 2 == 0)
{
//When get alternate node
if (result == 0)
{
result = temp->data;
}
else
{
result = result - temp->data;
}
}
counter++;
//visit to next node
temp = temp->next;
}
printf(" Alternate nodes subtraction : %d\n", result);
}
int main()
{
return 0;
}``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9``````
``````// Java Program
// Subtraction of the alternate nodes of linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
System.out.print("  " + temp.data);
//visit to next node
temp = temp.next;
}
System.out.print("\n");
}
// Find the subtraction of all alternate nodes in linked list
public void alternate_nodes_subtraction()
{
{
return;
}
//Define resultant variables
int result = 0;
int counter = 0;
//Start to first node of linked list
while (temp != null)
{
if (counter % 2 == 0)
{
//When get alternate node
if (result == 0)
{
result = temp.data;
}
else
{
result = result - temp.data;
}
}
counter++;
//visit to next node
temp = temp.next;
}
System.out.print(" Alternate nodes subtraction : " + result + "\n");
}
public static void main(String[] args)
{
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(9);
obj.insert(1);
obj.insert(3);
obj.insert(4);
obj.insert(3);
obj.insert(6);
obj.display();
obj.alternate_nodes_subtraction();
}
}``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Subtraction of the alternate nodes of linked list

class Node
{
public: int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List : ";
while (temp != NULL)
{
//display node value
cout << "  " << temp->data;
//visit to next node
temp = temp->next;
}
cout << "\n";
}
// Find the subtraction of all alternate nodes in linked list
void alternate_nodes_subtraction()
{
{
return;
}
//Define resultant variables
int result = 0;
int counter = 0;
//Start to first node of linked list
while (temp != NULL)
{
if (counter % 2 == 0)
{
//When get alternate node
if (result == 0)
{
result = temp->data;
}
else
{
result = result - temp->data;
}
}
counter++;
//visit to next node
temp = temp->next;
}
cout << " Alternate nodes subtraction : " << result << "\n";
}
};
int main()
{
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(9);
obj.insert(1);
obj.insert(3);
obj.insert(4);
obj.insert(3);
obj.insert(6);
obj.display();
obj.alternate_nodes_subtraction();
return 0;
}``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9``````
``````//Include namespace system
using System;

// C# Program
// Subtraction of the alternate nodes of linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
Console.Write("  " + temp.data);
//visit to next node
temp = temp.next;
}
Console.Write("\n");
}
// Find the subtraction of all alternate nodes in linked list
public void alternate_nodes_subtraction()
{
{
return;
}
//Define resultant variables
int result = 0;
int counter = 0;
//Start to first node of linked list
while (temp != null)
{
if (counter % 2 == 0)
{
//When get alternate node
if (result == 0)
{
result = temp.data;
}
else
{
result = result - temp.data;
}
}
counter++;
//visit to next node
temp = temp.next;
}
Console.Write(" Alternate nodes subtraction : " + result + "\n");
}
public static void Main(String[] args)
{
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(9);
obj.insert(1);
obj.insert(3);
obj.insert(4);
obj.insert(3);
obj.insert(6);
obj.display();
obj.alternate_nodes_subtraction();
}
}``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9``````
``````<?php
// Php Program
// Subtraction of the alternate nodes of linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List : ";
while (\$temp != null)
{
//display node value
echo "  ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
echo "\n";
}
// Find the subtraction of all alternate nodes in linked list
public	function alternate_nodes_subtraction()
{
{
return;
}
//Define resultant variables
\$result = 0;
\$counter = 0;
//Start to first node of linked list
while (\$temp != null)
{
if (\$counter % 2 == 0)
{
//When get alternate node
if (\$result == 0)
{
\$result = \$temp->data;
}
else
{
\$result = \$result - \$temp->data;
}
}
\$counter++;
//visit to next node
\$temp = \$temp->next;
}
echo " Alternate nodes subtraction : ". \$result ."\n";
}
}

function main()
{
\$obj->insert(4);
\$obj->insert(7);
\$obj->insert(2);
\$obj->insert(9);
\$obj->insert(1);
\$obj->insert(3);
\$obj->insert(4);
\$obj->insert(3);
\$obj->insert(6);
\$obj->display();
\$obj->alternate_nodes_subtraction();
}
main();``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9``````
``````// Node Js Program
// Subtraction of the alternate nodes of linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
//display node value
process.stdout.write("  " + temp.data);
//visit to next node
temp = temp.next;
}
process.stdout.write("\n");
}
// Find the subtraction of all alternate nodes in linked list
alternate_nodes_subtraction()
{
{
return;
}
//Define resultant variables
var result = 0;
var counter = 0;
//Start to first node of linked list
while (temp != null)
{
if (counter % 2 == 0)
{
//When get alternate node
if (result == 0)
{
result = temp.data;
}
else
{
result = result - temp.data;
}
}
counter++;
//visit to next node
temp = temp.next;
}
process.stdout.write(" Alternate nodes subtraction : " + result + "\n");
}
}

function main()
{
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(9);
obj.insert(1);
obj.insert(3);
obj.insert(4);
obj.insert(3);
obj.insert(6);
obj.display();
obj.alternate_nodes_subtraction();
}
main();``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9``````
``````#  Python 3 Program
#  Subtraction of the alternate nodes of linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructors
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

print("\n Linked List : ", end = "")
while (temp != None) :
# display node value
print("  ", temp.data, end = "")
# visit to next node
temp = temp.next

print("\n", end = "")

#  Find the subtraction of all alternate nodes in linked list
def alternate_nodes_subtraction(self) :
return

# Define resultant variables
result = 0
counter = 0
# Start to first node of linked list
while (temp != None) :
if (counter % 2 == 0) :
# When get alternate node
if (result == 0) :
result = temp.data
else :
result = result - temp.data

counter += 1
# visit to next node
temp = temp.next

print(" Alternate nodes subtraction : ", result ,"\n", end = "")

def main() :
obj.insert(4)
obj.insert(7)
obj.insert(2)
obj.insert(9)
obj.insert(1)
obj.insert(3)
obj.insert(4)
obj.insert(3)
obj.insert(6)
# Display of linked list nodes
obj.display()
obj.alternate_nodes_subtraction()

if __name__ == "__main__": main()``````

#### Output

`````` Linked List :    4   7   2   9   1   3   4   3   6
Alternate nodes subtraction :  -9``````
``````#  Ruby Program
#  Subtraction of the alternate nodes of linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

# display node value
print("  ", temp.data)
# visit to next node
temp = temp.next
end
print("\n")
end
#  Find the subtraction of all alternate nodes in linked list
def alternate_nodes_subtraction()

return
end
# Define resultant variables
result = 0
counter = 0
# Start to first node of linked list
while (temp != nil)

if (counter % 2 == 0)

# When get alternate node
if (result == 0)

result = temp.data
else

result = result - temp.data
end
end
counter += 1
# visit to next node
temp = temp.next
end
print(" Alternate nodes subtraction : ", result ,"\n")
end
end
def main()

obj.insert(4)
obj.insert(7)
obj.insert(2)
obj.insert(9)
obj.insert(1)
obj.insert(3)
obj.insert(4)
obj.insert(3)
obj.insert(6)
# Display of linked list nodes
obj.display()
obj.alternate_nodes_subtraction()
end
main()``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9
``````
``````// Scala Program
// Subtraction of the alternate nodes of linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
//display node value
print("  " + temp.data);
//visit to next node
temp = temp.next;
}
print("\n");
}
// Find the subtraction of all alternate nodes in linked list
def alternate_nodes_subtraction(): Unit = {
{
return;
}
//Define resultant variables
var result: Int = 0;
var counter: Int = 0;
//Start to first node of linked list
while (temp != null)
{
if (counter % 2 == 0)
{
//When get alternate node
if (result == 0)
{
result = temp.data;
}
else
{
result = result - temp.data;
}
}
counter += 1;
//visit to next node
temp = temp.next;
}
print(" Alternate nodes subtraction : " + result + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(9);
obj.insert(1);
obj.insert(3);
obj.insert(4);
obj.insert(3);
obj.insert(6);
obj.display();
obj.alternate_nodes_subtraction();
}
}``````

#### Output

`````` Linked List :   4  7  2  9  1  3  4  3  6
Alternate nodes subtraction : -9``````
``````// Swift Program
// Subtraction of the alternate nodes of linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List : ", terminator: "");
while (temp != nil)
{
//display node value
print("  ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print("\n", terminator: "");
}
// Find the subtraction of all alternate nodes in linked list
func alternate_nodes_subtraction()
{
{
return;
}
//Define resultant variables
var result: Int = 0;
var counter: Int = 0;
//Start to first node of linked list
while (temp != nil)
{
if (counter % 2 == 0)
{
//When get alternate node
if (result == 0)
{
result = temp!.data;
}
else
{
result = result - temp!.data;
}
}
counter += 1;
//visit to next node
temp = temp!.next;
}
print(" Alternate nodes subtraction : ", result ,"\n", terminator: "");
}
}
func main()
{
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(9);
obj.insert(1);
obj.insert(3);
obj.insert(4);
obj.insert(3);
obj.insert(6);
obj.display();
obj.alternate_nodes_subtraction();
}
main();``````

#### Output

`````` Linked List :    4   7   2   9   1   3   4   3   6
Alternate nodes subtraction :  -9``````

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.