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 differenceof(linklist x,linklist y) { linklist list3=new linklist(); Node temp=x.first;
}
while(temp!=null) { Node current=y.first; while(current!=null) { if(temp.data==current.data) break; current=current.next; } if(current==null) list3.createlist(temp.data); temp=temp.next; } return list3;
} public class SetDifference { public static void main(String args[])throws IOException { String temp; int choice; linklist list1=new linklist(); linklist list2=new linklist(); linklist list3=new linklist(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number of elements of set A:"); temp=br.readLine(); int k=Integer.parseInt(temp); System.out.println("Enter the elements of set A:"); for(int i=1;i<=k;i++) { temp=br.readLine(); list1.createlist(Integer.parseInt(temp)); } System.out.println("Enter the number of elements of set B:"); temp=br.readLine(); int m=Integer.parseInt(temp); System.out.println("Enter the elements of set B:"); for(int i=1;i<=m;i++) { temp=br.readLine(); list2.createlist(Integer.parseInt(temp)); } System.out.println("The set A"); list1.display(); System.out.println(); System.out.println("The set B"); list2.display(); do {
System.out.println("Menu"); System.out.println("1)A-B"+"\n"+"2)B-A"); System.out.println("Enter your choice"); temp=br.readLine(); choice=Integer.parseInt(temp); switch(choice) { case 1: System.out.println("The new set(A-B) is"); linklist diff1=list1.differenceof(list1,list2); diff1.display(); break; case 2: System.out.println("The new set(B-A) is"); linklist diff2=list3.differenceof(list2,list1); diff2.display(); break; } }while(choice==1 || choice==2); } }