【数据结构】C语言实现两链表相加

以下是C语言实现两个链表相加的示例代码:

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

// 定义链表节点结构体
struct ListNode {
    int val;
    struct ListNode *next;
};

// 创建链表节点
struct ListNode* createNode(int val) {
    struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
    node->val = val;
    node->next = NULL;
    return node;
}

// 创建链表
struct ListNode* createList(int* nums, int numsSize) {
    struct ListNode* head = NULL;
    struct ListNode* tail = NULL;
    for (int i = 0; i < numsSize; i++) {
        struct ListNode* node = createNode(nums[i]);
        if (head == NULL) {
            head = node;
            tail = node;
        } else {
            tail->next = node;
            tail = node;
        }
    }
    return head;
}

// 打印链表
void printList(struct ListNode* head) {
    while (head != NULL) {
        printf("%d ", head->val);
        head = head->next;
    }
    printf("\n");
}

// 两个链表相加
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* head = NULL;
    struct ListNode* tail = NULL;
    int carry = 0;
    while (l1 != NULL || l2 != NULL) {
        int val1 = l1 != NULL ? l1->val : 0;
        int val2 = l2 != NULL ? l2->val : 0;
        int sum = val1 + val2 + carry;
        carry = sum / 10;
        struct ListNode* node = createNode(sum % 10);
        if (head == NULL) {
            head = node;
            tail = node;
        } else {
            tail->next = node;
            tail = node;
        }
        
        if (carry > 0) {
     	   	struct ListNode* node = createNode(carry);
        	tail->next = node;
        	tail = node;
        	carry = 0;
	    }
	    
        if (l1 != NULL) l1 = l1->next;
        if (l2 != NULL) l2 = l2->next;
        
    }
    
    return head;
}

int main() {
    int nums1[] = {2, 4, 3};
    int nums2[] = {5, 6, 4};
    struct ListNode* l1 = createList(nums1, 3);
    struct ListNode* l2 = createList(nums2, 3);
    struct ListNode* result = addTwoNumbers(l1, l2);
    printList(result);
    return 0;
}

该代码中,首先定义了链表节点结构体struct ListNode,包括节点值val和指向下一个节点的指针next。然后定义了创建节点和创建链表的函数createNodecreateList,以及打印链表的函数printList

接着定义了两个链表相加的函数addTwoNumbers,该函数使用了两个指针l1l2分别指向两个链表的头节点,使用一个循环遍历两个链表,将对应节点的值相加,并将结果存储到新的链表中。需要注意的是,如果相加的结果大于等于10,则需要进位。最后,如果最高位有进位,则需要在新链表的末尾添加一个节点。

最后,在main函数中创建两个链表,调用addTwoNumbers函数相加两个链表,并打印结果链表。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言程序,使用链表实现多项式相加: ```c #include <stdio.h> #include <stdlib.h> // 定义多项式的节点 typedef struct Node { int coef; // 系数 int exp; // 指数 struct Node *next; // 下一个节点 } Node; // 创建一个新节点 Node *createNode(int coef, int exp) { Node *node = (Node *) malloc(sizeof(Node)); node->coef = coef; node->exp = exp; node->next = NULL; return node; } // 插入节点到多项式中 void insert(Node **head, int coef, int exp) { Node *node = createNode(coef, exp); if (*head == NULL) { *head = node; } else { Node *curr = *head; while (curr->next != NULL) { curr = curr->next; } curr->next = node; } } // 打印多项式 void print(Node *head) { Node *curr = head; while (curr != NULL) { printf("%dx^%d", curr->coef, curr->exp); curr = curr->next; if (curr != NULL) { printf(" + "); } } printf("\n"); } // 多项式相加 Node *add(Node *p1, Node *p2) { Node *head = NULL; while (p1 != NULL || p2 != NULL) { if (p1 == NULL) { insert(&head, p2->coef, p2->exp); p2 = p2->next; } else if (p2 == NULL) { insert(&head, p1->coef, p1->exp); p1 = p1->next; } else if (p1->exp > p2->exp) { insert(&head, p1->coef, p1->exp); p1 = p1->next; } else if (p1->exp < p2->exp) { insert(&head, p2->coef, p2->exp); p2 = p2->next; } else { insert(&head, p1->coef + p2->coef, p1->exp); p1 = p1->next; p2 = p2->next; } } return head; } int main() { // 创建多项式1: 2x^3 + 4x^2 + 3x^1 + 1x^0 Node *p1 = NULL; insert(&p1, 2, 3); insert(&p1, 4, 2); insert(&p1, 3, 1); insert(&p1, 1, 0); printf("p1 = "); print(p1); // 创建多项式2: 3x^3 + 2x^2 + 1x^1 + 5x^0 Node *p2 = NULL; insert(&p2, 3, 3); insert(&p2, 2, 2); insert(&p2, 1, 1); insert(&p2, 5, 0); printf("p2 = "); print(p2); // 计算多项式相加 Node *p3 = add(p1, p2); printf("p3 = "); print(p3); return 0; } ``` 输出结果为: ``` p1 = 2x^3 + 4x^2 + 3x^1 + 1x^0 p2 = 3x^3 + 2x^2 + 1x^1 + 5x^0 p3 = 5x^3 + 6x^2 + 4x^1 + 6x^0 ``` 该程序使用了链表来存储多项式,其中每个节点包含了系数和指数。`createNode()` 函数用于创建一个新节点,`insert()` 函数用于将节点插入到多项式中,`print()` 函数用于打印多项式,`add()` 函数用于计算多项式相加。在 `add()` 函数中,我们通过比较指数的大小来决定将哪个节点的系数插入到结果多项式中。最后,我们调用 `add()` 函数来计算多项式相加,并打印结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值