Remove all even parity nodes from linked list
Here given code implementation process.
// C Program
// Remove all even parity nodes from 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");
}
// Returns the number of active set bits in positive integer
int setBit(int num)
{
int count = 0;
while (num > 0)
{
count++;
num = num & (num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
void removeEvenParity(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
// Define some useful variables
struct LinkNode *auxiliary = sll->head;
struct LinkNode *node = NULL;
struct LinkNode *back = NULL;
while (auxiliary != NULL)
{
if (auxiliary->data >= 0 && (setBit(auxiliary->data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node->next;
if (back != NULL)
{
back->next = auxiliary;
}
if (node == sll->head)
{
// When removing a front node
sll->head = auxiliary;
}
if (node == sll->tail)
{
// When removing a last node
// Set new tail node of linked list
sll->tail = back;
}
// Remove linked list node
free(node);
node = NULL;
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary->next;
}
}
}
int main()
{
// Define linked list
struct SingleLL *sll = newLinkedList();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
addNode(sll, 11);
addNode(sll, 12);
addNode(sll, 3);
addNode(sll, 5);
addNode(sll, 9);
addNode(sll, 12);
addNode(sll, 4);
addNode(sll, 7);
addNode(sll, 8);
addNode(sll, 17);
printf("\n Before Delete \n");
display(sll);
removeEvenParity(sll);
printf("\n After Delete \n");
display(sll);
return 0;
}
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
/*
Java Program for
Remove all even parity nodes from 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");
}
// Returns the number of active set bits in positive integer
public int setBit(int num)
{
int count = 0;
while (num > 0)
{
count++;
num = num & (num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
public void removeEvenParity()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
// Define some useful variables
LinkNode auxiliary = this.head;
LinkNode node = null;
LinkNode back = null;
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (setBit(auxiliary.data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node.next;
if (back != null)
{
back.next = auxiliary;
}
if (node == this.head)
{
// When removing a front node
this.head = auxiliary;
}
if (node == this.tail)
{
// When removing a last node
// Set new tail node of linked list
this.tail = back;
}
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
public static void main(String[] args)
{
// Create a empty linked list
SingleLL sll = new SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11);
sll.addNode(12);
sll.addNode(3);
sll.addNode(5);
sll.addNode(9);
sll.addNode(12);
sll.addNode(4);
sll.addNode(7);
sll.addNode(8);
sll.addNode(17);
System.out.print("\n Before Delete \n");
sll.display();
sll.removeEvenParity();
System.out.print("\n After Delete \n");
sll.display();
}
}
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Remove all even parity nodes from 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";
}
// Returns the number of active set bits in positive integer
int setBit(int num)
{
int count = 0;
while (num > 0)
{
count++;
num = num &(num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
void removeEvenParity()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
// Define some useful variables
LinkNode *auxiliary = this->head;
LinkNode *node = NULL;
LinkNode *back = NULL;
while (auxiliary != NULL)
{
if (auxiliary->data >= 0 && (this->setBit(auxiliary->data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node->next;
if (back != NULL)
{
back->next = auxiliary;
}
if (node == this->head)
{
// When removing a front node
this->head = auxiliary;
}
if (node == this->tail)
{
// When removing a last node
// Set new tail node of linked list
this->tail = back;
}
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary->next;
}
}
}
};
int main()
{
// Create a empty linked list
SingleLL sll = SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11);
sll.addNode(12);
sll.addNode(3);
sll.addNode(5);
sll.addNode(9);
sll.addNode(12);
sll.addNode(4);
sll.addNode(7);
sll.addNode(8);
sll.addNode(17);
cout << "\n Before Delete \n";
sll.display();
sll.removeEvenParity();
cout << "\n After Delete \n";
sll.display();
return 0;
}
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
// Include namespace system
using System;
/*
C# Program for
Remove all even parity nodes from 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");
}
// Returns the number of active set bits in positive integer
public int setBit(int num)
{
int count = 0;
while (num > 0)
{
count++;
num = num & (num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
public void removeEvenParity()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
// Define some useful variables
LinkNode auxiliary = this.head;
LinkNode node = null;
LinkNode back = null;
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (setBit(auxiliary.data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node.next;
if (back != null)
{
back.next = auxiliary;
}
if (node == this.head)
{
// When removing a front node
this.head = auxiliary;
}
if (node == this.tail)
{
// When removing a last node
// Set new tail node of linked list
this.tail = back;
}
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
public static void Main(String[] args)
{
// Create a empty linked list
SingleLL sll = new SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11);
sll.addNode(12);
sll.addNode(3);
sll.addNode(5);
sll.addNode(9);
sll.addNode(12);
sll.addNode(4);
sll.addNode(7);
sll.addNode(8);
sll.addNode(17);
Console.Write("\n Before Delete \n");
sll.display();
sll.removeEvenParity();
Console.Write("\n After Delete \n");
sll.display();
}
}
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
<?php
/*
Php Program for
Remove all even parity nodes from 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";
}
// Returns the number of active set bits in positive integer
public function setBit($num)
{
$count = 0;
while ($num > 0)
{
$count++;
$num = $num & ($num - 1);
}
// Returning the value of calculate result
return $count;
}
// Remove all even parity node of given single linked list
public function removeEvenParity()
{
if ($this->head == null)
{
echo "\n Empty linked list\n";
return;
}
// Define some useful variables
$auxiliary = $this->head;
$node = null;
$back = null;
while ($auxiliary != null)
{
if ($auxiliary->data >= 0 && ($this->setBit($auxiliary->data) % 2 == 0))
{
// Get a removing node
$node = $auxiliary;
// Visit to next node
$auxiliary = $node->next;
if ($back != null)
{
$back->next = $auxiliary;
}
if ($node == $this->head)
{
// When removing a front node
$this->head = $auxiliary;
}
if ($node == $this->tail)
{
// When removing a last node
// Set new tail node of linked list
$this->tail = $back;
}
}
else
{
// Get current test node
$back = $auxiliary;
// Visit to next node
$auxiliary = $auxiliary->next;
}
}
}
}
function main()
{
// Create a empty linked list
$sll = new SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
$sll->addNode(11);
$sll->addNode(12);
$sll->addNode(3);
$sll->addNode(5);
$sll->addNode(9);
$sll->addNode(12);
$sll->addNode(4);
$sll->addNode(7);
$sll->addNode(8);
$sll->addNode(17);
echo "\n Before Delete \n";
$sll->display();
$sll->removeEvenParity();
echo "\n After Delete \n";
$sll->display();
}
main();
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
/*
Node Js Program for
Remove all even parity nodes from 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");
}
// Returns the number of active set bits in positive integer
setBit(num)
{
var count = 0;
while (num > 0)
{
count++;
num = num & (num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
removeEvenParity()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
// Define some useful variables
var auxiliary = this.head;
var node = null;
var back = null;
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.setBit(auxiliary.data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node.next;
if (back != null)
{
back.next = auxiliary;
}
if (node == this.head)
{
// When removing a front node
this.head = auxiliary;
}
if (node == this.tail)
{
// When removing a last node
// Set new tail node of linked list
this.tail = back;
}
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
}
function main()
{
// Create a empty linked list
var sll = new SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11);
sll.addNode(12);
sll.addNode(3);
sll.addNode(5);
sll.addNode(9);
sll.addNode(12);
sll.addNode(4);
sll.addNode(7);
sll.addNode(8);
sll.addNode(17);
process.stdout.write("\n Before Delete \n");
sll.display();
sll.removeEvenParity();
process.stdout.write("\n After Delete \n");
sll.display();
}
main();
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
# Python 3 Program for
# Remove all even parity nodes from 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")
# Returns the number of active set bits in positive integer
def setBit(self, num) :
count = 0
while (num > 0) :
count += 1
num = num & (num - 1)
# Returning the value of calculate result
return count
# Remove all even parity node of given single linked list
def removeEvenParity(self) :
if (self.head == None) :
print("\n Empty linked list")
return
# Define some useful variables
auxiliary = self.head
node = None
back = None
while (auxiliary != None) :
if (auxiliary.data >= 0 and(self.setBit(auxiliary.data) % 2 == 0)) :
# Get a removing node
node = auxiliary
# Visit to next node
auxiliary = node.next
if (back != None) :
back.next = auxiliary
if (node == self.head) :
# When removing a front node
self.head = auxiliary
if (node == self.tail) :
# When removing a last node
# Set new tail node of linked list
self.tail = back
else :
# Get current test node
back = auxiliary
# Visit to next node
auxiliary = auxiliary.next
def main() :
# Create a empty linked list
sll = SingleLL()
# Constructed linked list
# 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11)
sll.addNode(12)
sll.addNode(3)
sll.addNode(5)
sll.addNode(9)
sll.addNode(12)
sll.addNode(4)
sll.addNode(7)
sll.addNode(8)
sll.addNode(17)
print("\n Before Delete ")
sll.display()
sll.removeEvenParity()
print("\n After Delete ")
sll.display()
if __name__ == "__main__": main()
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
# Ruby Program for
# Remove all even parity nodes from 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
# Returns the number of active set bits in positive integer
def setBit(num)
count = 0
while (num > 0)
count += 1
num = num & (num - 1)
end
# Returning the value of calculate result
return count
end
# Remove all even parity node of given single linked list
def removeEvenParity()
if (self.head == nil)
print("\n Empty linked list\n")
return
end
# Define some useful variables
auxiliary = self.head
node = nil
back = nil
while (auxiliary != nil)
if (auxiliary.data >= 0 && (self.setBit(auxiliary.data) % 2 == 0))
# Get a removing node
node = auxiliary
# Visit to next node
auxiliary = node.next
if (back != nil)
back.next = auxiliary
end
if (node == self.head)
# When removing a front node
self.head = auxiliary
end
if (node == self.tail)
# When removing a last node
# Set new tail node of linked list
self.tail = back
end
else
# Get current test node
back = auxiliary
# Visit to next node
auxiliary = auxiliary.next
end
end
end
end
def main()
# Create a empty linked list
sll = SingleLL.new()
# Constructed linked list
# 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11)
sll.addNode(12)
sll.addNode(3)
sll.addNode(5)
sll.addNode(9)
sll.addNode(12)
sll.addNode(4)
sll.addNode(7)
sll.addNode(8)
sll.addNode(17)
print("\n Before Delete \n")
sll.display()
sll.removeEvenParity()
print("\n After Delete \n")
sll.display()
end
main()
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
/*
Scala Program for
Remove all even parity nodes from 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");
}
// Returns the number of active set bits in positive integer
def setBit(n: Int): Int = {
var num = n;
var count: Int = 0;
while (num > 0)
{
count += 1;
num = num & (num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
def removeEvenParity(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// Define some useful variables
var auxiliary: LinkNode = this.head;
var node: LinkNode = null;
var back: LinkNode = null;
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.setBit(auxiliary.data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node.next;
if (back != null)
{
back.next = auxiliary;
}
if (node == this.head)
{
// When removing a front node
this.head = auxiliary;
}
if (node == this.tail)
{
// When removing a last node
// Set new tail node of linked list
this.tail = back;
}
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create a empty linked list
var sll: SingleLL = new SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11);
sll.addNode(12);
sll.addNode(3);
sll.addNode(5);
sll.addNode(9);
sll.addNode(12);
sll.addNode(4);
sll.addNode(7);
sll.addNode(8);
sll.addNode(17);
print("\n Before Delete \n");
sll.display();
sll.removeEvenParity();
print("\n After Delete \n");
sll.display();
}
}
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
/*
Swift 4 Program for
Remove all even parity nodes from 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");
}
// Returns the number of active set bits in positive integer
func setBit(_ n: Int)->Int
{
var num = n;
var count: Int = 0;
while (num > 0)
{
count += 1;
num = num & (num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
func removeEvenParity()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
// Define some useful variables
var auxiliary: LinkNode? = self.head;
var node: LinkNode? = nil;
var back: LinkNode? = nil;
while (auxiliary != nil)
{
if (auxiliary!.data >= 0 && (self.setBit(auxiliary!.data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node!.next;
if (back != nil)
{
back!.next = auxiliary;
}
if (node === self.head)
{
// When removing a front node
self.head = auxiliary;
}
if (node === self.tail)
{
// When removing a last node
// Set new tail node of linked list
self.tail = back;
}
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary!.next;
}
}
}
}
func main()
{
// Create a empty linked list
let sll: SingleLL = SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11);
sll.addNode(12);
sll.addNode(3);
sll.addNode(5);
sll.addNode(9);
sll.addNode(12);
sll.addNode(4);
sll.addNode(7);
sll.addNode(8);
sll.addNode(17);
print("\n Before Delete ");
sll.display();
sll.removeEvenParity();
print("\n After Delete ");
sll.display();
}
main();
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
/*
Kotlin Program for
Remove all even parity nodes from 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");
}
// Returns the number of active set bits in positive integer
fun setBit(n: Int): Int
{
var num = n;
var count: Int = 0;
while (num > 0)
{
count += 1;
num = num and (num - 1);
}
// Returning the value of calculate result
return count;
}
// Remove all even parity node of given single linked list
fun removeEvenParity(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// Define some useful variables
var auxiliary: LinkNode ? = this.head;
var node: LinkNode ? ;
var back: LinkNode ? = null;
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.setBit(auxiliary.data) % 2 == 0))
{
// Get a removing node
node = auxiliary;
// Visit to next node
auxiliary = node.next;
if (back != null)
{
back.next = auxiliary;
}
if (node == this.head)
{
// When removing a front node
this.head = auxiliary;
}
if (node == this.tail)
{
// When removing a last node
// Set new tail node of linked list
this.tail = back;
}
}
else
{
// Get current test node
back = auxiliary;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
}
fun main(args: Array < String > ): Unit
{
// Create a empty linked list
var sll: SingleLL = SingleLL();
// Constructed linked list
// 11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
sll.addNode(11);
sll.addNode(12);
sll.addNode(3);
sll.addNode(5);
sll.addNode(9);
sll.addNode(12);
sll.addNode(4);
sll.addNode(7);
sll.addNode(8);
sll.addNode(17);
print("\n Before Delete \n");
sll.display();
sll.removeEvenParity();
print("\n After Delete \n");
sll.display();
}
Output
Before Delete
11 → 12 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → 17 → NULL
After Delete
11 → 4 → 7 → 8 → NULL
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