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>
//Linked List Node
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;
}
//Add new node at end of linked list
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
if ( *head == NULL)
{
*head = node;
}
else
{
struct Node *temp = *head;
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
//Add node at last possition
temp->next = node;
}
}
//Display linked list element
void display(struct Node *head)
{
if (head == NULL)
{
printf("\nEmpty linked list\n");
return;
}
struct Node *temp = head;
printf("\n Linked List : ");
//iterating linked list elements
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
void unique_elements(struct Node *head)
{
//Define some auxiliary variables
struct Node *outer = head;
struct Node *inner = NULL;
//Resultant variables
int status = 0;
int counter = 1;
printf(" Unique Elements : ");
//Below process takes O(n*n) time
//iterating linked list elements
while (outer != NULL)
{
inner = head;
//iterating linked list elements
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()
{
struct Node *head = NULL;
//Create linked list
insert( &head, 2);
insert( &head, 4);
insert( &head, 7);
insert( &head, 2);
insert( &head, 5);
insert( &head, 7);
insert( &head, 1);
insert( &head, 3);
insert( &head, 1);
insert( &head, 2);
//Display of linked list nodes
display(head);
unique_elements(head);
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
//Node of LinkedList
class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
class MyLinkedList
{
public Node head;
public Node tail;
//Class constructors
public MyLinkedList()
{
this.head = null;
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
public void display()
{
if (this.head == null)
{
System.out.print("\nEmpty linked list\n");
return;
}
Node temp = this.head;
System.out.print("\n Linked List : ");
//iterating linked list elements
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 outer = this.head;
Node inner = null;
//Resultant variables
boolean status = false;
int counter = 1;
System.out.print(" Unique Elements : ");
//Below process takes O(n*n) time
//iterating linked list elements
while (outer != null)
{
inner = head;
//iterating linked list elements
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)
{
MyLinkedList obj = new MyLinkedList();
//Create linked list
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();
}
}
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
//Node of LinkedList
class Node
{
public: int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
class MyLinkedList
{
public: Node * head;
Node * tail;
//Class constructors
MyLinkedList()
{
this->head = NULL;
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
if (this->head == NULL)
{
//When linked list empty add first node
this->head = node;
this->tail = node;
}
else
{
//Add new node at end of linked list
this->tail->next = node;
this->tail = node;
}
}
//Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\nEmpty linked list\n";
return;
}
Node * temp = this->head;
cout << "\n Linked List : ";
//iterating linked list elements
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 * outer = this->head;
Node * inner = NULL;
//Resultant variables
bool status = false;
int counter = 1;
cout << " Unique Elements : ";
//Below process takes O(n*n) time
//iterating linked list elements
while (outer != NULL)
{
inner = this->head;
//iterating linked list elements
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()
{
MyLinkedList obj = MyLinkedList();
//Create linked list
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();
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
//Node of LinkedList
class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
class MyLinkedList
{
public Node head;
public Node tail;
//Class constructors
public MyLinkedList()
{
this.head = null;
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
public void display()
{
if (this.head == null)
{
Console.Write("\nEmpty linked list\n");
return;
}
Node temp = this.head;
Console.Write("\n Linked List : ");
//iterating linked list elements
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 outer = this.head;
Node inner = null;
//Resultant variables
Boolean status = false;
int counter = 1;
Console.Write(" Unique Elements : ");
//Below process takes O(n*n) time
//iterating linked list elements
while (outer != null)
{
inner = head;
//iterating linked list elements
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)
{
MyLinkedList obj = new MyLinkedList();
//Create linked list
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();
}
}
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
//Node of LinkedList
class Node
{
public $data;
public $next;
function __construct($data)
{
//Set node value
$this->data = $data;
$this->next = null;
}
}
class MyLinkedList
{
public $head;
public $tail;
//Class constructors
function __construct()
{
$this->head = null;
$this->tail = null;
}
//insert node at last of linke list
public function insert($data)
{
//Create a node
$node = new Node($data);
if ($this->head == null)
{
//When linked list empty add first node
$this->head = $node;
$this->tail = $node;
}
else
{
//Add new node at end of linked list
$this->tail->next = $node;
$this->tail = $node;
}
}
//Display linked list element
public function display()
{
if ($this->head == null)
{
echo "\nEmpty linked list\n";
return;
}
$temp = $this->head;
echo "\n Linked List : ";
//iterating linked list elements
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
$outer = $this->head;
$inner = null;
//Resultant variables
$status = false;
$counter = 1;
echo " Unique Elements : ";
//Below process takes O(n*n) time
//iterating linked list elements
while ($outer != null)
{
$inner = $this->head;
//iterating linked list elements
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 = new MyLinkedList();
//Create linked list
$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();
}
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
//Node of LinkedList
class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
class MyLinkedList
{
//Class constructors
constructor()
{
this.head = null;
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\nEmpty linked list\n");
return;
}
var temp = this.head;
process.stdout.write("\n Linked List : ");
//iterating linked list elements
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 outer = this.head;
var inner = null;
//Resultant variables
var status = false;
var counter = 1;
process.stdout.write(" Unique Elements : ");
//Below process takes O(n*n) time
//iterating linked list elements
while (outer != null)
{
inner = this.head;
//iterating linked list elements
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()
{
var obj = new MyLinkedList();
//Create linked list
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();
}
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
# Node of LinkedList
class Node :
def __init__(self, data) :
# Set node value
self.data = data
self.next = None
class MyLinkedList :
# Class constructors
def __init__(self) :
self.head = None
self.tail = None
# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
if (self.head == None) :
# When linked list empty add first node
self.head = node
self.tail = node
else :
# Add new node at end of linked list
self.tail.next = node
self.tail = node
# Display linked list element
def display(self) :
if (self.head == None) :
print("\nEmpty linked list\n", end = "")
return
temp = self.head
print("\n Linked List : ", end = "")
# iterating linked list elements
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
outer = self.head
inner = None
# Resultant variables
status = False
counter = 1
print(" Unique Elements : ", end = "")
# Below process takes O(n*n) time
# iterating linked list elements
while (outer != None) :
inner = self.head
# iterating linked list elements
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 = MyLinkedList()
# Create linked list
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
# Node of LinkedList
class Node
# Define the accessor and reader of class Node
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data)
# Set node value
self.data = data
self.next = nil
end
end
class MyLinkedList
# Define the accessor and reader of class MyLinkedList
attr_reader :head, :tail
attr_accessor :head, :tail
# Class constructors
def initialize()
self.head = nil
self.tail = nil
end
# insert node at last of linke list
def insert(data)
# Create a node
node = Node.new(data)
if (self.head == nil)
# When linked list empty add first node
self.head = node
self.tail = node
else
# Add new node at end of linked list
self.tail.next = node
self.tail = node
end
end
# Display linked list element
def display()
if (self.head == nil)
print("\nEmpty linked list\n")
return
end
temp = self.head
print("\n Linked List : ")
# iterating linked list elements
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
outer = self.head
inner = nil
# Resultant variables
status = false
counter = 1
print(" Unique Elements : ")
# Below process takes O(n*n) time
# iterating linked list elements
while (outer != nil)
inner = @head
# iterating linked list elements
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 = MyLinkedList.new()
# Create linked list
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
//Node of LinkedList
class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
class MyLinkedList(var head: Node,
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);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
def display(): Unit = {
if (this.head == null)
{
print("\nEmpty linked list\n");
return;
}
var temp: Node = this.head;
print("\n Linked List : ");
//iterating linked list elements
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 outer: Node = this.head;
var inner: Node = null;
//Resultant variables
var status: Boolean = false;
var counter: Int = 1;
print(" Unique Elements : ");
//Below process takes O(n*n) time
//iterating linked list elements
while (outer != null)
{
inner = head;
//iterating linked list elements
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 = {
var obj: MyLinkedList = new MyLinkedList();
//Create linked list
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();
}
}
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
//Node of LinkedList
class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
class MyLinkedList
{
var head: Node? ;
var tail: Node? ;
//Class constructors
init()
{
self.head = nil;
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
if (self.head == nil)
{
//When linked list empty add first node
self.head = node;
self.tail = node;
}
else
{
//Add new node at end of linked list
self.tail!.next = node;
self.tail = node;
}
}
//Display linked list element
func display()
{
if (self.head == nil)
{
print("\nEmpty linked list\n", terminator: "");
return;
}
var temp: Node? = self.head;
print("\n Linked List : ", terminator: "");
//iterating linked list elements
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 outer: Node? = self.head;
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
//iterating linked list elements
while (outer != nil)
{
inner = self.head;
//iterating linked list elements
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()
{
let obj: MyLinkedList = MyLinkedList();
//Create linked list
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();
}
main();
Output
Linked List : 2 4 7 2 5 7 1 3 1 2
Unique Elements : 4 5 3
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