顺序表的合并与链表的合并

顺序表的合并

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

#define MAX_SIZE 100

typedef struct {
    int data[MAX_SIZE]; // 数据存储区
    int length;         // 当前长度
} SeqList, * PSeqList;
// 将两个有序表合并成一个有序表
void merge_seq_list(PSeqList list1, PSeqList list2, PSeqList result) {
    int i = 0, j = 0, k = 0;
    while (i < list1->length && j < list2->length) {
        if (list1->data[i] <= list2->data[j]) {
            result->data[k++] = list1->data[i++];
        }
        else {
            result->data[k++] = list2->data[j++];
        }
    }
    while (i < list1->length) {
        result->data[k++] = list1->data[i++];
    }
    while (j < list2->length) {
        result->data[k++] = list2->data[j++];
    }
    result->length = k;
}

链表的合并

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

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

//定义链表合并函数
struct node* merge(struct node* head1, struct node* head2) {
    struct node* p = head1;
    while (p->next != NULL) {
        p = p->next;
    }
    p->next = head2;
    return head1;
}

//定义创建链表函数
struct node* create() {
    int data;
    struct node* head, * p, * tail;
    head = (struct node*)malloc(sizeof(struct node));
    if (head == NULL) {
        printf("分配内存失败\n");
        exit(1);
    }
    tail = head;
    tail->next = NULL;
    printf("请输入节点的数据,输入-1结束:");
    int result = scanf_s("%d", &data);
    while (data != -1) {
        if (result != 1) {
            printf("输入错误,请重新输入:");
            continue;
        }
        p = (struct node*)malloc(sizeof(struct node));
        if (p == NULL) {
            printf("分配内存失败\n");
            exit(1);
        }
        p->data = data;
        tail->next = p;
        tail = p;
        tail->next = NULL;
        printf("请输入节点的数据,输入-1结束:");
        result = scanf_s("%d", &data);
    }
    return head;
}

//定义遍历链表函数
void traverse(struct node* head) {
    struct node* p = head->next;
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

int main() {
    struct node* head1, * head2, * head3;
    //创建链表1
    printf("创建链表1:\n");
    head1 = create();
    printf("链表1的元素为:");
    traverse(head1);
    //创建链表2
    printf("创建链表2:\n");
    head2 = create();
    printf("链表2的元素为:");
    traverse(head2);
    //合并链表
    printf("合并链表1和链表2:\n");
    head3 = merge(head1, head2);
    printf("合并后的链表元素为:");
    traverse(head3);
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++链表合并多项式是指将两个或多个多项式链表合并成一个新的多项式链表。每个多项式链表由一系列节点组成,每个节点包含一个系数和指数,示多项式中的一个项。 下面是C++链表合并多项式的基本步骤: 1. 定义一个节点结构体,包含系数和指数两个成员变量。 2. 定义一个链表结构体,包含一个指向头节点的指针。 3. 创建两个或多个多项式链表,并按照指数从大到小的顺序插入节点。 4. 定义一个新的链表用于存储合并后的多项式。 5. 遍历两个或多个多项式链表,比较当前节点的指数大小,将较小的节点插入新链表,并更新指针。 6. 如果有一个多项式链表遍历完了,将另一个多项式链表剩余的节点直接插入新链表。 7. 返回合并后的多项式链表。 以下是一个示例代码: ```cpp #include <iostream> struct Node { int coefficient; int exponent; Node* next; }; struct LinkedList { Node* head; }; LinkedList mergePolynomials(LinkedList poly1, LinkedList poly2) { LinkedList mergedPoly; Node* p1 = poly1.head; Node* p2 = poly2.head; Node* current = nullptr; while (p1 != nullptr && p2 != nullptr) { Node* newNode = new Node; if (p1->exponent > p2->exponent) { newNode->coefficient = p1->coefficient; newNode->exponent = p1->exponent; p1 = p1->next; } else if (p1->exponent < p2->exponent) { newNode->coefficient = p2->coefficient; newNode->exponent = p2->exponent; p2 = p2->next; } else { newNode->coefficient = p1->coefficient + p2->coefficient; newNode->exponent = p1->exponent; p1 = p1->next; p2 = p2->next; } newNode->next = nullptr; if (current == nullptr) { mergedPoly.head = newNode; current = newNode; } else { current->next = newNode; current = newNode; } } if (p1 != nullptr) { current->next = p1; } if (p2 != nullptr) { current->next = p2; } return mergedPoly; } int main() { // 创建多项式链表1 LinkedList poly1; Node* node1 = new Node{3, 2, nullptr}; Node* node2 = new Node{4, 1, nullptr}; Node* node3 = new Node{2, 0, nullptr}; poly1.head = node1; node1->next = node2; node2->next = node3; // 创建多项式链表2 LinkedList poly2; Node* node4 = new Node{5, 3, nullptr}; Node* node5 = new Node{1, 1, nullptr}; poly2.head = node4; node4->next = node5; // 合并多项式链表 LinkedList mergedPoly = mergePolynomials(poly1, poly2); // 打印合并后的多项式链表 Node* current = mergedPoly.head; while (current != nullptr) { std::cout << current->coefficient << "x^" << current->exponent << " "; current = current->next; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值