Posted on by Kalkicode

Find all unique elements in linked list

Here given code implementation process.

``````// C Program
// Find all unique elements in 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");
}
//This are find out all unique elements in given array
{
//Define some auxiliary variables
struct Node *inner = NULL;
//Resultant variables
int status = 0;
int counter = 1;
printf(" Unique Elements : ");
//Below process takes O(n*n) time
while (outer != NULL)
{
while (inner != NULL && counter <= 1)
{
if (inner != outer && inner->data == outer->data)
{
//When element are duplicate
counter++;
}
//visit to next node
inner = inner->next;
}
if (counter == 1)
{
status = 1;
printf("  %d", outer->data);
}
//reset the counter value
counter = 1;
//visit to next node
outer = outer->next;
}
if (status == 0)
{
printf(" None ");
}
printf("\n");
}
int main()
{
return 0;
}``````

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3``````
``````// Java Program
// Find all unique elements in 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");
}
//This are find out all unique elements in given array
public void unique_elements()
{
//Define some auxiliary variables
Node inner = null;
//Resultant variables
boolean status = false;
int counter = 1;
System.out.print(" Unique Elements : ");
//Below process takes O(n*n) time
while (outer != null)
{
while (inner != null && counter <= 1)
{
if (inner != outer && inner.data == outer.data)
{
//When element are duplicate
counter++;
}
//visit to next node
inner = inner.next;
}
if (counter == 1)
{
status = true;
System.out.print("  " + outer.data);
}
//reset the counter value
counter = 1;
//visit to next node
outer = outer.next;
}
if (status == false)
{
System.out.print(" None ");
}
System.out.print("\n");
}
public static void main(String[] args)
{
obj.insert(2);
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(5);
obj.insert(7);
obj.insert(1);
obj.insert(3);
obj.insert(1);
obj.insert(2);
obj.display();
obj.unique_elements();
}
}``````

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Find all unique elements in 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";
}
//This are find out all unique elements in given array
void unique_elements()
{
//Define some auxiliary variables
Node * inner = NULL;
//Resultant variables
bool status = false;
int counter = 1;
cout << " Unique Elements : ";
//Below process takes O(n*n) time
while (outer != NULL)
{
while (inner != NULL && counter <= 1)
{
if (inner != outer && inner->data == outer->data)
{
//When element are duplicate
counter++;
}
//visit to next node
inner = inner->next;
}
if (counter == 1)
{
status = true;
cout << "  " << outer->data;
}
//reset the counter value
counter = 1;
//visit to next node
outer = outer->next;
}
if (status == false)
{
cout << " None ";
}
cout << "\n";
}
};
int main()
{
obj.insert(2);
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(5);
obj.insert(7);
obj.insert(1);
obj.insert(3);
obj.insert(1);
obj.insert(2);
obj.display();
obj.unique_elements();
return 0;
}``````

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3``````
``````//Include namespace system
using System;
// C# Program
// Find all unique elements in 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");
}
//This are find out all unique elements in given array
public void unique_elements()
{
//Define some auxiliary variables
Node inner = null;
//Resultant variables
Boolean status = false;
int counter = 1;
Console.Write(" Unique Elements : ");
//Below process takes O(n*n) time
while (outer != null)
{
while (inner != null && counter <= 1)
{
if (inner != outer && inner.data == outer.data)
{
//When element are duplicate
counter++;
}
//visit to next node
inner = inner.next;
}
if (counter == 1)
{
status = true;
Console.Write("  " + outer.data);
}
//reset the counter value
counter = 1;
//visit to next node
outer = outer.next;
}
if (status == false)
{
Console.Write(" None ");
}
Console.Write("\n");
}
public static void Main(String[] args)
{
obj.insert(2);
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(5);
obj.insert(7);
obj.insert(1);
obj.insert(3);
obj.insert(1);
obj.insert(2);
obj.display();
obj.unique_elements();
}
}``````

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3``````
``````<?php
// Php Program
// Find all unique elements in 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";
}
//This are find out all unique elements in given array
public	function unique_elements()
{
//Define some auxiliary variables
\$inner = null;
//Resultant variables
\$status = false;
\$counter = 1;
echo " Unique Elements : ";
//Below process takes O(n*n) time
while (\$outer != null)
{
while (\$inner != null && \$counter <= 1)
{
if (\$inner != \$outer && \$inner->data == \$outer->data)
{
//When element are duplicate
\$counter++;
}
//visit to next node
\$inner = \$inner->next;
}
if (\$counter == 1)
{
\$status = true;
echo "  ". \$outer->data;
}
//reset the counter value
\$counter = 1;
//visit to next node
\$outer = \$outer->next;
}
if (\$status == false)
{
echo " None ";
}
echo "\n";
}
}

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

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3``````
``````// Node Js Program
// Find all unique elements in 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");
}
//This are find out all unique elements in given array
unique_elements()
{
//Define some auxiliary variables
var inner = null;
//Resultant variables
var status = false;
var counter = 1;
process.stdout.write(" Unique Elements : ");
//Below process takes O(n*n) time
while (outer != null)
{
while (inner != null && counter <= 1)
{
if (inner != outer && inner.data == outer.data)
{
//When element are duplicate
counter++;
}
//visit to next node
inner = inner.next;
}
if (counter == 1)
{
status = true;
process.stdout.write("  " + outer.data);
}
//reset the counter value
counter = 1;
//visit to next node
outer = outer.next;
}
if (status == false)
{
process.stdout.write(" None ");
}
process.stdout.write("\n");
}
}

function main()
{
obj.insert(2);
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(5);
obj.insert(7);
obj.insert(1);
obj.insert(3);
obj.insert(1);
obj.insert(2);
obj.display();
obj.unique_elements();
}
main();``````

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3``````
``````#  Python 3 Program
#  Find all unique elements in 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 = "")

# This are find out all unique elements in given array
def unique_elements(self) :
# Define some auxiliary variables
inner = None
# Resultant variables
status = False
counter = 1
print(" Unique Elements : ", end = "")
# Below process takes O(n*n) time
while (outer != None) :
while (inner != None and counter <= 1) :
if (inner != outer and inner.data == outer.data) :
# When element are duplicate
counter += 1

# visit to next node
inner = inner.next

if (counter == 1) :
status = True
print("  ", outer.data, end = "")

# reset the counter value
counter = 1
# visit to next node
outer = outer.next

if (status == False) :
print(" None ", end = "")

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

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

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

Output

`````` Linked List :    2   4   7   2   5   7   1   3   1   2
Unique Elements :    4   5   3``````
``````#  Ruby Program
#  Find all unique elements in 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
# This are find out all unique elements in given array
def unique_elements()

# Define some auxiliary variables
inner = nil
# Resultant variables
status = false
counter = 1
print(" Unique Elements : ")
# Below process takes O(n*n) time
while (outer != nil)

while (inner != nil && counter <= 1)

if (inner != outer && inner.data == outer.data)

# When element are duplicate
counter += 1
end
# visit to next node
inner = inner.next
end
if (counter == 1)

status = true
print("  ", outer.data)
end
# reset the counter value
counter = 1
# visit to next node
outer = outer.next
end
if (status == false)

print(" None ")
end
print("\n")
end
end
def main()

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

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3
``````
``````// Scala Program
// Find all unique elements in 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");
}
//This are find out all unique elements in given array
def unique_elements(): Unit = {
//Define some auxiliary variables
var inner: Node = null;
//Resultant variables
var status: Boolean = false;
var counter: Int = 1;
print(" Unique Elements : ");
//Below process takes O(n*n) time
while (outer != null)
{
while (inner != null && counter <= 1)
{
if (inner != outer && inner.data == outer.data)
{
//When element are duplicate
counter += 1;
}
//visit to next node
inner = inner.next;
}
if (counter == 1)
{
status = true;
print("  " + outer.data);
}
//reset the counter value
counter = 1;
//visit to next node
outer = outer.next;
}
if (status == false)
{
print(" None ");
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(2);
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(5);
obj.insert(7);
obj.insert(1);
obj.insert(3);
obj.insert(1);
obj.insert(2);
obj.display();
obj.unique_elements();
}
}``````

Output

`````` Linked List :   2  4  7  2  5  7  1  3  1  2
Unique Elements :   4  5  3``````
``````// Swift Program
// Find all unique elements in 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: "");
}
//This are find out all unique elements in given array
func unique_elements()
{
//Define some auxiliary variables
var inner: Node? = nil;
//Resultant variables
var status: Bool = false;
var counter: Int = 1;
print(" Unique Elements : ", terminator: "");
//Below process takes O(n*n) time
while (outer != nil)
{
while (inner != nil && counter <= 1)
{
if (!(inner === outer) && inner!.data == outer!.data)
{
//When element are duplicate
counter += 1;
}
//visit to next node
inner = inner!.next;
}
if (counter == 1)
{
status = true;
print("  ", outer!.data, terminator: "");
}
//reset the counter value
counter = 1;
//visit to next node
outer = outer!.next;
}
if (status == false)
{
print(" None ", terminator: "");
}
print(terminator: "\n");
}
}
func main()
{
obj.insert(2);
obj.insert(4);
obj.insert(7);
obj.insert(2);
obj.insert(5);
obj.insert(7);
obj.insert(1);
obj.insert(3);
obj.insert(1);
obj.insert(2);
obj.display();
obj.unique_elements();
}
main();``````

Output

`````` Linked List :    2   4   7   2   5   7   1   3   1   2
Unique Elements :    4   5   3``````

Comment

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.

Categories
Relative Post