Can we add two long number using linked list. If Yes then please write a simple code......

Dani AI

Generated

Linked lists are a fine way to implement arbitrary-precision addition for learning or when native types overflow. This thread request from can be solved by storing digits (or digit blocks) in nodes and performing per-digit addition with carry. As observed, this is mainly an educational approach — production code normally uses big-integer libraries — and as suggested, a concise, self-contained example is provided below so anyone can compare it to their attempt.

The sample keeps one decimal digit per node and stores the least-significant digit (LSD) at the list head. That makes addition simple (traverse both lists from head, sum digits + carry, append result digits to a result list), then reverse the result for printing. The code reads two newline-terminated decimal strings from stdin, validates digits, builds lists, adds, prints, and frees memory.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct Node { int d; struct Node *next; } Node;

Node* push_head(Node* head, int d) {
    Node* n = malloc(sizeof(Node));
    if (!n) { perror("malloc"); exit(EXIT_FAILURE); }
    n->d = d; n->next = head; return n;
}

Node* append_tail(Node* head, Node** tailp, int d) {
    Node* n = malloc(sizeof(Node));
    if (!n) { perror("malloc"); exit(EXIT_FAILURE); }
    n->d = d; n->next = NULL;
    if (!head) { *tailp = n; return n; }
    (*tailp)->next = n; *tailp = n; return head;
}

Node* build_list(const char* s) {
    Node* head = NULL;
    for (size_t i = 0; s[i]; ++i) if (isdigit((unsigned char)s[i])) head = push_head(head, s[i]-'0');
    return head;
}

Node* add_lists(Node* a, Node* b) {
    Node *res = NULL, *tail = NULL;
    int carry = 0;
    while (a || b || carry) {
        int ai = a ? a->d : 0;
        int bi = b ? b->d : 0;
        int sum = ai + bi + carry;
        carry = sum / 10;
        res = append_tail(res, &tail, sum % 10);
        if (a) a = a->next; if (b) b = b->next;
    }
    return res; /* LSD at head */
}

Node* reverse(Node* head) {
    Node *prev = NULL;
    while (head) { Node* nxt = head->next; head->next = prev; prev = head; head = nxt; }
    return prev;
}

void print_list(Node* head) {
    if (!head) { printf("0\n"); return; }
    head = reverse(head);
    for (Node* p = head; p; p = p->next) putchar('0' + p->d);
    putchar('\n');
}

void free_list(Node* head) { while (head) { Node* t = head->next; free(head); head = t; } }

int main(void) {
    char A[10005], B[10005];
    if (!fgets(A, sizeof A, stdin) || !fgets(B, sizeof B, stdin)) return 0;
    A[strcspn(A, "\r\n")] = '\0'; B[strcspn(B, "\r\n")] = '\0';
    Node* a = build_list(A); Node* b = build_list(B);
    Node* r = add_lists(a, b);
    print_list(r);
    free_list(a); free_list(b); free_list(r);
    return 0;
}

Notes: check malloc returns in larger projects, strip non-digit characters or handle signs separately, and consider using a larger base per node (e.g., 1e9) to reduce node count for very long numbers. Time and space are O(n) where n is digit count. For production-grade big integers prefer established libraries (GMP, libtommath). This example complements the earlier remarks in the thread and provides a compact, runnable reference implementation.

Recommended Answers

All 3 Replies

Welcome aboard. If you have specific problems when trying to do a program...please post the source code here along with what you think may be wrong. Also post the errors you get, so we can take a look and help you. We are not in the business of doing your home-work for you! You must TRY/show some effort!!

The question itself is wierd. Why do you want to use a linked list for adding two longs when you can do the same without using it?

He doesn't. He just want attention by posting lame questions that are mostly unintelligible. He probably has refused to read The Rules and other helpful posts to aid in asking good, well formatted questions..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.