两个链表存储的数进行相加问题

本文章分成几个部分:

问题,代码,测试

问题:请用代码实现,两个链表,分别存储一个数字的各个位数,请计算两个链表的相加结果。

如:
l1: 1->3->4
l2: 8->9
输出结果为: 223

代码

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include "my_list.h"

#define max(a,b) (((a) > (b)) ? (a) : (b))

struct ListNode* reverse_list(struct ListNode* head) {
    if (head == NULL || head->next == NULL)
        return head;

    struct ListNode *pre = head;
    struct ListNode *cur = head->next;
    struct ListNode *tmp = head->next->next;

    while(cur) {
        tmp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = tmp;
    }
    head->next = NULL;

    return pre;
}

void add_list_node(struct ListNode *head, struct ListNode *node){
    if (!node)
        return;

    node->next = head->next;
    head->next = node;
    head->val++;
}

/* caller should free ListNode memory */
struct ListNode * get_list_sum(struct ListNode* list1, struct ListNode* list2){
    if (list1 == NULL || list2 == NULL)
        return NULL;

    struct ListNode *head1 = reverse_list(list1);
    struct ListNode *head2 = reverse_list(list2);
    struct ListNode *head = malloc(sizeof(struct ListNode));
    struct ListNode *node = NULL;
    int add1 = 0;
    int add2 = 0;
    int sum = 0;
    int c = 0;

    head->val = 0;
    head->next = NULL;

    while(head1 != NULL || head2 != NULL)  {
        if (head1)
            add1 = head1->val;

        if (head2)
            add2 = head2->val;

        sum = (add1 + add2 + c)%10;
        c = (add1 + add2 + c)/10;

        node = malloc(sizeof(struct ListNode));
        node->val = sum;
        node->next = NULL;
        add_list_node(head, node);

        if (head1)
            head1 = head1->next;

        if (head2)
            head2 = head2->next;

        add1 = 0;
        add2 = 0;
    }
    if (c != 0) {
        node = malloc(sizeof(struct ListNode));
        node->val = c;
        node->next = NULL;
        add_list_node(head, node);
    }

    return head;
}

struct ListNode * create_list(int *array, int n) {
    int i;
    struct ListNode *head = malloc(sizeof(struct ListNode));
    struct ListNode *node;
    struct ListNode *cur = head;
    head->val = array[0];
    head->next = NULL;

    for(i = 1; i < n; i++) {
        node = malloc(sizeof(struct ListNode));
        node->val = array[i];
        node->next = NULL;
        cur->next = node;
        cur = node;
    }

    return head;
}

void print_list(struct ListNode * head){
    struct ListNode *cur = head;
    while(cur) {
        printf("%d ", cur->val);
        cur = cur->next;
    }
    printf("\n");
}

void free_list(struct ListNode *head){
    struct ListNode * tmp;
    while(head)
    {
        tmp = head;
        head = tmp->next;
        free(tmp);
    }
}

my_list.h头文件如下所示:

/* This is my_list.h */

struct ListNode {
    int val;
    struct ListNode *next;
};

/* caller should free ListNode memory */
struct ListNode * get_list_sum(struct ListNode* list1, struct ListNode* list2);
struct ListNode * create_list(int *array, int n);
void print_list(struct ListNode * head);
void free_list(struct ListNode *head);

写了下测试代码

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include "my_list.h"

#define max(a,b) (((a) > (b)) ? (a) : (b))

bool isnum(char * str, int n){
    int i;
    for(i = 0; i < n; i++)
        if (str[i] < '0' || str[i] > '9')
            return false;

    return true;
}
void print_addnum(struct ListNode *list1, int a1, struct ListNode *list2, int a2){
    int i;
    if (a1 > a2) {
        printf("  ");
        print_list(list1);
        printf("+ ");
        for(i = 0; i < a1-a2; i++)
            printf("  ");
        print_list(list2);
        printf("=\n");
        return;
    }
    printf("  ");
    for(i = 0; i < a2-a1; i++)
        printf("  ");

    print_list(list1);
    printf("+ ");
    print_list(list2);
    printf("=\n");
    return;
}

void print_result(struct ListNode *result_head, int len1, int len2){
    if (result_head->val == max(len1,len2))
        printf(" ");
    printf(" ");
    print_list(result_head->next);
}
int main(int argc, char *argv[]){ if (argc != 3) { printf("error: The number of parameters is incorrect\n"); return 1; } if (argv[1] == NULL || argv[2] == NULL) { printf("error: The parameter content is empty\n"); return 1; } char *add1 = argv[1]; int len1 = strlen(add1); char *add2 = argv[2]; int len2 = strlen(add2); int *array1 = malloc(len1 * sizeof(int)); int *array2 = malloc(len2 * sizeof(int)); int i; if (isnum(add1, len1) != true || isnum(add2, len2) != true) { printf("error: Parameter is not number. num1: %s num2: %s\n", add1, add2); return 1; } for (i = 0; i < len1; i++) array1[i] = add1[i] - '0'; for(i = 0; i < len2; i++) array2[i] = add2[i] - '0'; struct ListNode *head; struct ListNode * list1 = create_list(array1, len1); struct ListNode * list2 = create_list(array2, len2);
print_addnum(list1, len1, list2, len2); head
= get_list_sum(list1, list2); print_result(head, len1, len2); free_list(list1); free_list(list2); free_list(head);
return 0; }

下面我们来看一下测试结果:

linux-eFsRxl:/mnt # ./list_add 204820948230948230498230482304923840929340294830948230948 203482039482333775738867028967028967028967028223133223126383126343060950621
                                      2 0 4 8 2 0 9 4 8 2 3 0 9 4 8 2 3 0 4 9 8 2 3 0 4 8 2 3 0 4 9 2 3 8 4 0 9 2 9 3 4 0 2 9 4 8 3 0 9 4 8 2 3 0 9 4 8
+ 2 0 3 4 8 2 0 3 9 4 8 2 3 3 3 7 7 5 7 3 8 8 6 7 0 2 8 9 6 7 0 2 8 9 6 7 0 2 8 9 6 7 0 2 8 2 2 3 1 3 3 2 2 3 1 2 6 3 8 3 1 2 6 3 4 3 0 6 0 9 5 0 6 2 1
=
  2 0 3 4 8 2 0 3 9 4 8 2 3 3 3 7 7 5 9 4 3 6 8 7 9 7 7 1 9 7 9 7 7 1 9 7 5 2 7 1 9 7 5 1 0 5 2 8 0 5 7 0 6 4 0 5 5 7 2 3 4 2 1 1 7 4 0 0 9 1 8 1 5 6 9
linux-eFsRxl:/mnt # ./list_add 1 0
  1
+ 0
=
  1
linux-eFsRxl:/mnt # ./list_add 5 5
  5
+ 5
=
 1 0
linux-eFsRxl:/mnt # ./list_add 190 986
  1 9 0
+ 9 8 6
=
 1 1 7 6

猜你喜欢

转载自www.cnblogs.com/xingmuxin/p/11319245.html