集合的运算之链表实现
来源:互联网 发布:js 对象key 编辑:程序博客网 时间:2024/06/10 07:45
#include <iostream>using namespace std;struct Node{int content;Node *next;};Node *input();void output(Node *);Node *A_or_B(Node *, Node *);Node *A_and_B(Node *, Node *);Node *A_not_B(Node *, Node *);bool is_a(int, Node *);int main(){Node *head_A = input();Node *head_B = input();Node *s1 = A_and_B(head_A, head_B);Node *s2 = A_or_B(head_A, head_B);Node *s3 = A_not_B(head_A, head_B);Node *s4 = A_not_B(head_B, head_A);cout << "A and B is: ";output(s1);cout << "A or B is: ";output(s2);cout << "A not B is: ";output(s3);cout << "B not A is: ";output(s4);return 0;}Node *input(){Node *head = NULL, *tail = NULL;int x;cin >> x;while (x != -1){Node *p = new Node;p->content = x;p->next = NULL;if (head == NULL)head = tail = p;else{tail->next = p;tail = p;}cin >> x;}return head;}void output(Node *head){for (Node *p = head; p != NULL; p = p->next)cout << p->content << " ";cout << endl;}Node *A_and_B(Node *h1, Node *h2){if (h1 == NULL || h2 == NULL)return NULL;else{Node *head = NULL;for (Node *h = h2; h != NULL; h = h->next)if (is_a(h->content, h1)){Node *p = new Node;p->content = h->content;if (head == NULL){head = p;p->next = NULL;}else{p->next = head;head = p;}}return head;}}Node *A_or_B(Node *h1, Node *h2){if (h1 == NULL)return h2;else if (h2 == NULL)return h1;else{Node *head = NULL, *tail = NULL;for (Node *h = h1; h != NULL; h = h->next){Node *p = new Node;p->content = h->content;p->next = NULL;if (head == NULL)head = tail = p;else{tail->next = p;tail = p;}}for (Node *h = h2; h != NULL; h = h->next)if (!(is_a(h->content, h1))){Node *p = new Node;p->content = h->content;p->next = NULL;if (head == NULL)head = tail = p;else{tail->next = p;tail = p;}}return head;}}Node *A_not_B(Node *h1, Node *h2){if (h1 == NULL)return NULL;else if (h2 == NULL)return h1;else{Node *head = NULL, *tail = NULL;for (Node *h = h1; h != NULL; h = h->next)if (!(is_a(h->content, h2))){Node *p = new Node;p->content = h->content;p->next = NULL;if (head == NULL)head = tail = p;else{tail->next = p;tail = p;}}return head;}}bool is_a(int i, Node *head)//此函数判断某一个数是否是一个链表中的结点值{Node *h = head;while (h != NULL){if (i == h->content)return true;h = h->next;}return false;}
0 0
- 集合的运算之链表实现
- 两个链表实现集合的运算
- 21天学通C++之集合的链表实现及其运算实现
- 链表实现集合及多项式运算
- java实现集合的运算
- 多项式的运算之链表实现
- 顺序表实现集合运算
- 利用顺序表实现集合的并运算
- (C)顺序表实现集合运算
- 在以单链表表示的正整数的有序集合上,实现集合的并、交和差运算
- STL实现集合交集运算
- 用位向量实现的集合及其常用运算
- 【数据结构_链表_List_1045】集合的交运算实现
- 集合的运算
- 集合的运算
- 集合的运算
- Matlab的集合运算
- sql的集合运算
- Wc2016酱油记
- 程序无法启动ALL_BUILD 拒绝访问
- Java手动创建一个内存泄漏的程序
- VB.NET入门了解
- 小众软件
- 集合的运算之链表实现
- 2416开发记录十二:ioremap
- 1770: [Usaco2009 Nov]lights 燈 高斯消元解异或方程组+dfs
- 状态压缩DP解POJ3254-Corn Fields
- COGS495窗口
- Linux统计文件夹和文件数量
- 开源免费接口的使用
- 7.8 使用GROUPING_ID()来扩展报告
- 7.11 小结