Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
105 views

Java Program For Set Intersection

The document contains code for a linked list data structure in Java. It defines Node and LinkedList classes to implement a basic linked list with methods to create, display, insert, delete, search and intersect nodes in the list.

Uploaded by

seenoos12
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Java Program For Set Intersection

The document contains code for a linked list data structure in Java. It defines Node and LinkedList classes to implement a basic linked list with methods to create, display, insert, delete, search and intersect nodes in the list.

Uploaded by

seenoos12
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 void insert1(int p,int x)


{
Node current=first;
Node previous=null;
Node newnode=new Node(x);
if(p==1)
{
newnode.next=first;
first=newnode;
}
int n=2;
while(current!=null)
{
previous=current;
current=current.next;
if(n==p)
{
newnode.next=current;
previous.next=newnode;
}
n=n+1;

public void insert2(int key,int num)


{

Node current=first;
Node newnode=new Node(num);
while(current.data!=key && current!=null)
{
current=current.next;
}
newnode.next=current.next;
current.next=newnode;
}

public void delete(int x)


{
Node current=first;
Node previous=null;
while(current!=null && current.data!=x)
{

previous=current;
current=current.next;
}
if(previous==null)
{
first=current.next;
}
else if(current==null)
System.out.println("The element is not present");
else
{
previous.next=current.next;
}
}

public Node search(int x)


{
Node current=first;
while(current!=null)
{
if(current.data==x)
return current;
current=current.next;
}
return null;
}

/*public linklist concat(linklist x)


{
linklist list3=new linklist();
Node current=first;
while(current.next!=null)
{
list3.createlist(current.data);
current=current.next;
}
current.next=x.first;
while(current!=null)
{

list3.createlist(current.data);
current=current.next;
}
return list3;
}*/

public void intersect(linklist x,linklist y)


{
linlklist list3=new linklist();
Node temp=x.first;
while(temp!=null)
{
Node current=y.first;
while(current!=null)
{
if(temp.data==current.data)
list3.createlist(temp.data);
current=current.next;
}
temp=temp.next;
}

}
}

public class Intersection


{
public static void main(String args[])throws IOException
{
String temp;
int ch;
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 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();
list1.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();
list2.createlist(Integer.parseInt(temp));
}

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


list1.display();
System.out.println();
list2.display();
/*linklist list3=list1.intesect(list2);
System.out.println("the list after concatenation");
list3.display();*/

do
{
System.out.println("MENU:");
System.out.println("1:insert 2:delete 3:sorted insert 4:search ");
temp=br.readLine();
ch=Integer.parseInt(temp);
switch(ch)
{
case 1:
System.out.println("position");
temp=br.readLine();
k=Integer.parseInt(temp);
System.out.println("Enter the node");
temp=br.readLine();
m=Integer.parseInt(temp);

list3.insert1(k,m);
list3.display();

break;
case 2:
System.out.println("element");
temp=br.readLine();
list3.delete(Integer.parseInt(temp));
list3.display();
break;

case 3:

System.out.println("position");
temp=br.readLine();
int pos=Integer.parseInt(temp);
System.out.println("Enter the element");
temp=br.readLine();
int num=Integer.parseInt(temp);
list3.insert2(pos,num);
list3.display();

break;

case 4:

System.out.println("element");
temp=br.readLine();
Node n=list3.search(Integer.parseInt(temp));
if(n!=null)
System.out.println("present ");
else
System.out.println("not present");
break;
case 5:
System.out.println("After union");

list3.intersect(list1,list2);
list3.display();

}
}while(ch==1||ch==2||ch==3||ch==4);

}
}

You might also like