Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Addition of positive numbers using linked list: C code

Monday, October 6, 2014

Addition of positive numbers using linked list: C code

Addition of large positive integers in C is quite difficult since there will be overflow error once the range of 'int' is crossed. But this problem can be overcome by the use of linked list. Happy addition! 



#include<stdio.h>
#include<stdlib.h>
struct numberList {
    int data;
    struct numberList *nextDigit;
    struct numberList *revDigit;
    };
    typedef struct numberList node;
void inputNumber(node*);
void display(node*,int);   
void addNumber(node*,node*);
int main() {
   
    node *firstNumber,*secondNumber,*sum;
    firstNumber = (node *)malloc(sizeof(node));
    secondNumber = (node *)malloc(sizeof(node));
    printf("\n Enter the first number(one digit at a time) type -1 to terminate the number:");
    printf("\n Enter the digit:");
    scanf("%d",&firstNumber->data);
    inputNumber(firstNumber);
   
    printf("\n Enter the second number(one digit at a time) type -1 to terminate the number:");
    printf("\n Enter the digit:");
    scanf("%d",&secondNumber->data);
    inputNumber(secondNumber);
   
    display(firstNumber,1);
    display(secondNumber,2);
   
    addNumber(firstNumber,secondNumber);   
    return 0;
    }   
void addNumber(node *p,node *q) {
    int len1=0,len2=0,min,i,once=0;
    while (p->nextDigit !=NULL) {
        len1++;
        p = p->nextDigit;
        }
    p = p->revDigit;   
    while (q->nextDigit !=NULL) {
        len2++;
        q = q->nextDigit;
        }
    q = q->revDigit;
    min = len1 <= len2 ? len1:len2;
    node *sum,*tmp,*sum1;   
    sum = (node *)malloc(sizeof(node));
    sum1 = sum;
    sum->revDigit = NULL;
    for ( i = 1; i <= min; i++) {
        sum->data = (p->data+q->data+once)%10;
        tmp = sum;
        once = (p->data+q->data+once)/10;
        p=p->revDigit;
        q=q->revDigit;
        sum->nextDigit = (node *)malloc(sizeof(node));
        sum = sum->nextDigit;
        sum->revDigit=tmp;
        }
    if ( len1 == len2 && once !=0) {
        sum->data = once;
        sum->revDigit=tmp;
        }   
    if (len1 > len2) {
        int t = len1-len2;
        sum = sum->revDigit;
        for ( i = 1; i<=t; i++) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->revDigit=tmp;
            sum->data = (p->data+once)%10;
            once = (p->data+once)/10;
            p = p->revDigit;
            } 
        if ( once !=0) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->data = once;
            sum->revDigit=tmp;
            }
               
        }   
    if (len1 < len2) {
        sum = sum->revDigit;
        int t = len2-len1;
        for ( i = 1; i<=t; i++) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->revDigit=tmp;
            sum->data = (q->data+once)%10;
            once = (q->data+once)/10;
            q = q->revDigit;
            }
        if ( once !=0) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->data = once;
            sum->revDigit=tmp;
            }   
        }   
    sum->nextDigit = NULL;   
    printf("\n Sum of the numbers is:");
    while (sum1->nextDigit !=NULL) {
        //printf("\n%d",sum1->data);
        sum1 = sum1->nextDigit;
        }
    //sum1 = sum1->revDigit;   
    while (sum1 !=NULL) {
        printf("%d",sum1->data);
        sum1 = sum1->revDigit;
        }
    printf("\n");   
    }   
void display(node *p,int tx) {
    if (tx==1)
        printf("\nFirst Number\n");
    else
        printf("\nSecond Number\n");
    while (p->nextDigit !=NULL) {
        printf("%d",p->data);
        p = p->nextDigit;
        }
    p = p->revDigit;   
    /*while (p !=NULL) {
        //printf("%d",p->data);
        p = p->revDigit;
        }*/   
    }   
void inputNumber(node *p) {
    node *q;
    p->revDigit = NULL;
    while (1) {
        q = p;
        p->nextDigit = (node *)malloc(sizeof(node));
        p = p->nextDigit;
        printf("\n Enter the digit:");
        scanf("%d",&p->data);
        p->revDigit = q;
        if (p->data == -1)
            break;
        }
    p->nextDigit = NULL;
    }     

No comments:

Post a Comment