Friday, August 19, 2011

Java Program to Add two Numbers using Linked List


The Program below inserts two numbers to a Linked list and Adds them to produce the output.
 import java.io.*;  
 import java.lang.*;  
 class node  
 {  
   int data;  
   node next;  
   node prev;  
   node(int d)  
   {  
     data=d;  
     next=null;  
     prev=null;  
   }  
 }  
 class list  
 {  
   node first=null;  
   node curr=null;  
   void insert(int d)  
   {  
     node n1=new node(d);  
     curr=first;  
     if(curr==null)  
       first=n1;  
     else  
     {  
       while(curr.next!=null)  
         curr=curr.next;  
       curr.next=n1;  
     }  
   }  
   void display()  
   {  
     curr=first;  
     if(curr==null)  
       System.out.println("no list");  
     else  
     {  
       while(curr!=null)  
       {  
         System.out.print(curr.data);  
         curr=curr.next;  
       }  
     }  
   }  
 }  
 public class Sum {  
   public static void main(String[] args) {  
    DataInputStream get=new DataInputStream(System.in);  
    list l1=new list();  
    list l2=new list();  
    list l3=new list();  
    node curr1,curr2,curr3;  
    int n1,n2,n3;  
    int p,q,d,e;  
    try  
    {  
      System.out.println("Enter 1st no:");  
      n1=Integer.parseInt(get.readLine());  
      System.out.println("Enter 2nd no:");  
      n2=Integer.parseInt(get.readLine());  
      p=0;  
      while(n1>0)  
      {  
        d=n1%10;  
        p++;  
        l1.insert(d);  
        n1=n1/10;  
      }  
      n3=n2;  
      q=0;  
      while(n3>0)  
      {  
        d=n3%10;  
        q++;  
        l2.insert(d);  
        n3=n3/10;  
      }  
      while(p>q)  
      {  
        l2.insert(0);  
        p--;  
      }  
      while(q>p)  
      {  
        l1.insert(0);  
        q--;  
      }  
      e=0;  
      curr1=l1.first;  
      curr2=l2.first;  
      while(curr1!=null&&curr2!=null)  
      {  
        e=e+curr1.data+curr2.data;  
        if(e>=10)  
        {  
         d=e%10;  
         l3.insert(d);  
         e=e/10;  
        }  
        else  
        {  
          l3.insert(e);  
          e=0;  
        }  
      curr1=curr1.next;  
      curr2=curr2.next;  
      }  
      node temp=null;  
      int s=0;  
      int i,j;  
      curr3=l3.first;  
      while(curr3!=null)  
      {  
        temp=curr3;  
        s++;  
        curr3=curr3.next;  
      }  
      curr3=l3.first;  
      s=s/2;  
      for(j=0;j<s;j++)  
      {  
        i=curr3.data;  
        curr3.data=temp.data;  
        temp.data=i;  
        curr3=curr3.next;  
        temp=temp.prev;  
      }  
      System.out.println("Sum=");  
      l3.display();  
    }  
    catch(Exception k)  
    {  
      System.out.println(k.getMessage());  
    }  
   }  
 }  

2 comments:

  • Young Coder says:
    March 30, 2012 at 11:18 AM

    how would you change this to read the numbers in from a txt file? I'm trying to get a list of numbers like :

    765 321

    and show the sum as 765 + 321 = 1086
    Please help me.

  • Techman says:
    March 31, 2012 at 5:25 AM

    BufferedReader myReader =
    new BufferedReader(new FileReader("myfile.txt"));


    while ((line = myReader.readLine()) != null) {
    //do what you want with the Integer.parseInt function

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.