线性表的合并
#include <iostream>
using namespace std;#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2typedef int Status;// 定义单链表
typedef struct LNode {int data;struct LNode *next;
}LNode, *LinkList;// 初始化链表
Status InitList_L(LinkList &L) {L = new LNode;L->next = NULL;return OK;
}// 尾插法建立单链表
void CreateList_R(LinkList &L, int n) {L = new LNode;L->next = NULL;LNode *r = L;for (int i = 0; i < n; ++i) {LNode *p = new LNode;cin >> p->data;p->next = NULL;r->next = p;r = p;}
}//单链表的表长
int ListLength(LinkList L) {LinkList p;p = L->next;int i = 0;while (p) {i++;p = p->next;}return i;
}// 遍历打印链表
void Print(LinkList L) {LinkList p;p = L->next;int len = ListLength(L);for (int i = 0; i < len; i++) {cout << p->data << " ";p = p->next;}
}// 取值——取单链表中第i个元素的内容
Status GetElem(LinkList L, int i, int &e) {LinkList p;p = L->next;int j = 1;while (p && j < i) {p = p->next;++j;}if (!p || j > i)return ERROR;e = p->data;return OK;
}// 按值查找——根据指定数据获取该数据所在的位置序号
LNode *LocateElem(LinkList L, int e) {LinkList p;p = L->next;int j = 1;while (p && p->data!=e)p = p->next;return p;
}//插入——在第i个结点前插入值为e的新结点
Status ListInsert(LinkList &L, int i, int e) {LinkList p;p = L;int j = 0;while (p && j < i - 1) {p = p->next;++j;}if (!p || j > i - 1)return ERROR;LNode *s = new LNode;s->data = e;s->next = p->next;p->next = s;return OK;
}// 线性表的合并
void Union_L(LinkList &La, LinkList &Lb) {int La_len = ListLength(La);int Lb_len = ListLength(Lb);for (int i = 1; i <= Lb_len; i++) {int e = 0;GetElem(Lb, i, e);if (!LocateElem(La, e)) ListInsert(La, ++La_len, e);}
}int main() {LinkList La;LinkList Lb;InitList_L(La);InitList_L(Lb);CreateList_R(La, 4);CreateList_R(Lb, 3);Union_L(La, Lb);Print(La);return 0;
}