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

// 定义链表节点 typedef struct node { int data; struct node* next; } Node;

// 创建新节点 Node* newNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = NULL; return node; }

// 向链表中添加元素(去重) void add(Node**head, int data)//head指向指针的指针 { Node* current = head; while (current != NULL) { if (current->data == data) { return ; } current = current->next; }//进行遍历,重复的元素返回,不重复的创建新的节点 Node node = newNode(data); node->next = *head;//从头部插入 *head = node; }

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

// 计算两个集合的交集 Node* intersection(Node* head1, Node* head2) { Node* result = NULL; while (head1 != NULL) { Node* temp = head2; while (temp != NULL) { if (head1->data == temp->data) { add(&result, head1->data); break; } temp = temp->next; } head1 = head1->next; } return result; }

// 计算两个集合的并集 Node* unionSet(Node* head1, Node* head2) { Node* result = NULL; while (head1 != NULL) { add(&result, head1->data); head1 = head1->next; } while (head2 != NULL) { add(&result, head2->data); head2 = head2->next; } return result; }

// 计算两个集合的补集(A-B) Node* complement(Node* head1, Node* head2) { Node* result = NULL; while (head1 != NULL) { int found = 0; Node* temp = head2; while (temp != NULL) { if (head1->data == temp->data) { found = 1; break; } temp = temp->next; } if (!found) { add(&result, head1->data); } head1 = head1->next; } return result; }

int main() { // 创建两个集合A和B Node* A = NULL; Node* B = NULL; int n, x; printf("请输入集合A中元素的个数:"); scanf_s("%d", &n); printf("请输入集合A中的元素:"); for (int i = 0; i < n; i++) { scanf_s("%d", &x); add(&A, x); }

// 从输入中读取集合B
printf("请输入集合B中元素的个数:");
scanf_s("%d", &n);
printf("请输入集合B中的元素:");
for (int i = 0; i < n; i++) {
    scanf_s("%d", &x);
    add(&B, x);
}

int choice = 0;
while (1) {
    system("cls");//清屏操作
    printf("集合A: ");
    printList(A);

    printf("集合B: ");
    printList(B);

    printf("请输入你想要进行的运算:\n");
    printf("1. 交集\n");
    printf("2. 并集\n");
    printf("3. 补集 (A-B)\n");
    printf("0. 退出系统\n");
    scanf_s("%d", &choice);
    switch (choice) {
    case 1:
        // 计算两个集合的交集
    {
        Node* inter = intersection(A, B);
        printf("交集: ");
        printList(inter);
    }
    break;
    case 2:
        // 计算两个集合的并集
    {
        Node* uni = unionSet(A, B);
        printf("并集: ");
        printList(uni);
    }
    break;
    case 3:
        // 计算两个集合的补集(A-B)
    {
        Node* comp = complement(A, B);
        printf("补集 (A-B): ");
        printList(comp);
    }
    break;
    case 0:
    {
        printf("成功退出系统 \n");
        exit(0);
    }
    default:
        printf("无效输入\n");
        break;
    }
    system("pause");//暂停屏幕
}
return 0;
#include stdioh#include stdlibh 定义链表节点typedef struct node int data; struct node next; Node; 创建新节点Node newNodeint data Node node = NodemallocsizeofNode; node-data = data; node-next = N

原文地址: https://www.cveoy.top/t/topic/f3LU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录