0% found this document useful (0 votes)
70 views3 pages

Set Union Implementation Using Java

Uploaded by

seenoos12
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views3 pages

Set Union Implementation Using Java

Uploaded by

seenoos12
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

*;

class Node
{
public int data;
public Node next;
public Node(int value)
{
data=value;
}
public void displaynode()
{
System.out.print(data+" ");
}
}

class linklist
{
public Node first;
public linklist()
{
first=null;
}
public void createlist(int x)
{
Node newnode=new Node(x);
if(first==null)
first=newnode;
else
{
Node current=first;
while(current.next!=null)
current=current.next;
current.next=newnode;
}
}

public void display()


{
Node current=first;
while(current!=null)
{
current.displaynode();
current=current.next;
}
System.out.println(" ");
}

public linklist union(linklist x)


{
linklist set3=new linklist();
Node temp=first;
while(temp!=null)
{
set3.createlist(temp.data);
temp=temp.next;
}
Node current=x.first;
while(current!=null)
{
Node p=first;
while(p!=null)
{
if(current.data==p.data)
{
break;
}
p=p.next;
}
if(p==null)
set3.createlist(current.data);
current=current.next;
}
return set3;
}
}

public class Setunion


{
public static void main(String args[])throws IOException
{
String temp;
int ch;
linklist set1=new linklist();
linklist set2=new linklist();
linklist set3=new linklist();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the number of nodes of list1:");


temp=br.readLine();
int k=Integer.parseInt(temp);
System.out.println("Enter the nodes of list1:");
for(int i=1;i<=k;i++)
{
temp=br.readLine();
set1.createlist(Integer.parseInt(temp));
}
System.out.println("Enter the number of nodes of list2:");
temp=br.readLine();
int m=Integer.parseInt(temp);
System.out.println("Enter the nodes of list2:");
for(int i=1;i<=m;i++)
{
temp=br.readLine();
set2.createlist(Integer.parseInt(temp));
}

System.out.println("The entered nodes");


set1.display();
System.out.println();
set2.display();

System.out.println("After union");
set3=list1.unionof(list2);
set3.display();

}
}

You might also like