Saturday, April 16, 2011

Merging two Link List


import java.util.Scanner;
import java.util.Stack;
public class MergeList
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        SingleLL s1=new SingleLL();
        SingleLL s2=new SingleLL();
        int i,j;
        System.out.println("Enter 5 Elements for Link 1");
        for(i=0;i<5;i++)
            s1.AddEnd(in.nextInt());
        System.out.println("Enter 5 Elements for Link 2");
        for(i=0;i<5;i++)
            s2.AddEnd(in.nextInt());
        SingleLL s3=new SingleLL();
        s3=s1;
        s3.end.next=s2.start;
        Stack s=new Stack();
        s3.curr=s3.start;
        while(s3.curr!=null)
        {
            s.push(s3.curr.info);
            s3.curr=s3.curr.next;
        }
        for(j=0;j<10;j++)
            System.out.print(s.pop() + " -> ");
    }
}


No comments:

Post a Comment