#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int ElemType;
typedef int Status;
typedef struct Lnode {
ElemType data;
struct Lnode* next;
}Lnode, * LinkList;
Status InitList(LinkList& L) {
L = new Lnode;
L->next = NULL;
return 1;
}
//判断是否为空
Status IsEmpty(LinkList L) {
if (L->next)
{
return 0;
}
else
return 1;
}
//销毁
Status DestroyList(LinkList& L) {
LinkList p;
while (L != NULL)
{
p = L;
L = L->next;
delete p;
}
return 1;
}
//清空
Status ClearList(LinkList& L) {
LinkList p;
p = L->next;
LinkList q;
q = p->next;
while (p != NULL)
{
delete p;
p = q;
q = q->next;
}
L->next = NULL;
return 1;
}
//求链表表长
int LengthList(LinkList& L) {
LinkList p;
p = L->next;
int i = 0;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
//取值
int GetElem(LinkList L, int i, ElemType& e) {
LinkList p;
p = L->next;
int j = 1;
while (p != NULL && j < i)
{
p = p->next;
j++;
}
if (!p || j > i)
{
return 0;
}
e = p->data;
return 1;
}
//查找
Lnode* LocateElem(LinkList L, ElemType e) {
LinkList p;
p = L->next;
while (p != NULL && p->data != e)
{
p = p->next;
}
return p;
}
//插入
Status InsertList(LinkList& L, int i, ElemType e) {
LinkList p;
p = L->next;
int j = 1;
while (p != NULL && j < i - 1)//寻找第i-1个节点
{
j++;
p = p->next;
}
if (p == NULL || j > i - 1)
{
return 0;
}
Lnode* s = new Lnode;
s->data = e;
s->next = p->next;
p->next = s;
return 1;
}
//删除
Status DeleteList(LinkList& L, int i, ElemType& e) {
LinkList p;
p = L->next;
int j = 1;
while (p != NULL && j < i - 1)
{
j++;
p = p->next;
}
if (p == NULL || j > i - 1)
{
return 0;
}
LinkList q;
q = p->next;
p->next = q->next;
e = q->data;
delete q;
return 1;
}
//头插法建立链表
void CreateList1(LinkList& L, int n) {
L = new Lnode;
L->next = NULL;
for (int i = 0; i < n; i++)
{
LinkList p;
p = new Lnode;
p->next = L->next;
L->next = p;
}
}
//尾插法建立链表
void CreateList2(LinkList& L, int n) {
L = new Lnode;
L->next = NULL;
LinkList r;
r = L;
for (int i = 0; i < n; i++)
{
LinkList p = new Lnode;
scanf("%d", &p->data);
p->next = NULL;
r->next = p;
r = p;
}
}
//带尾指针的循环链表的合并
LinkList Connect(LinkList Ta, LinkList Tb) {
LinkList p;
p = Ta->next;
Ta->next = Tb->next->next;
delete Tb->next;
Tb->next = p;
return Tb;
}
//双向链表定义
typedef struct DuLNode {
ElemType data;
struct DuLNode* prior, * next;
}DuLNode, * DuLinkList;
//双向链表中插入新节点
void ListInsert_Du(DuLinkList& L, int i, ElemType e) {
DuLinkList p;
p = L->next->next;
int j = 1;
while (p != NULL && j < i)
{
j++;
p = p->next;
}
if (p == NULL)
{
exit(-1);
}
DuLinkList s = new DuLNode;
s->prior = p->prior;
p->prior->next = s;
p->prior = s;
s->next = p;
}
//删除
void DeleteDuList(DuLinkList& L, int i, ElemType& e) {
DuLinkList p;
p = L->next->next;
int j = 1;
while (p != NULL && j < i)
{
j++;
p = p->next;
}
if (p == NULL)
{
exit(-1);
}
e = p->data;
p->prior->next = p->next;
p->next->prior = p->prior;
delete p;
}
//线性表的合并 La:7,5,3,11 Lb:2 6 3 La和Lb分别表示两个集合A和集合B,先要求一个新的集合A∪B
// 合并后:La:7 5 3 11 2 6
void merge(LinkList& La, LinkList& Lb) {
int La_length = LengthList(La);
int Lb_length = LengthList(Lb);
for (int i = 1; i <= Lb_length; i++)
{
ElemType e;
GetElem(Lb, i, e);
if (LocateElem(La, e) == NULL)
{
//printf("yes");
InsertList(La, ++La_length, e);
}
}
}
//输出链表中的每一个数据
void output(LinkList L) {
int length = LengthList(L);
LinkList p = L->next;
for (int i = 0; i < length; i++)
{
printf("%d ", p->data);
p = p->next;
}
}
//有序表的合并
void merge_list(LinkList& LA, LinkList& LB, LinkList& LC) {
LinkList pa, pb, pc;
pa = LA->next;
pb = LB->next;
pc = LC = LA;
while (pa&&pb)
{
if (pa->data<=pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
pc->next = pa ? pa : pb;
delete LB;
}
}
int main() {
//线性表的合并
LinkList La, Lb;
CreateList2(La, 4);
CreateList2(Lb, 3);
//output(La);
//output(Lb);
//int a = LengthList(La);
//ElemType e;
//GetElem(La, 2, e);
//printf("%d\n%d\n",a, e);
merge(La, Lb);
//int a = LengthList(La);
//printf("%d\n", a);
output(La);
//end
//有序表的合并
return 0;
}
getelem函数中的i是数据在链表中的位置不需要减一,如果for循环中用到getelem函数要注意循环变量应从1开始(合并线性表时踩的坑)