#include<iostream>
using namespace std;
typedef struct list
{
int data;
list* next;
}list,*linklist;
void Newlist(linklist& l)
{
l = new list;
l->next = NULL;
}
void Createlist(linklist& l)
{
cout << "输入链表长度:" << endl;
int n;
cin >> n;
linklist p;
for (int i = 0; i < n; i++)
{
p = new list;
cin >> p->data;
p->next = l->next;
l->next = p;
}
}
void Printlist(linklist l)
{
linklist p = l->next;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void Addlist(linklist& la, linklist& lb,linklist &lc)//合并递增序列
{
linklist pa = la->next, pb = lb->next,pc=lc=la,q;
while (pa && pb)
{
if (pa->data < pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else if (pa->data > pb->data)
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
else
{
pc->next = pa;
pc = pa;
pa = pa->next;
q = pb->next;
delete pb;
pb = q;
}
}
pc->next = pa ? pa : pb;
}
void Intersection(linklist& la, linklist& lb, linklist& lc)//两递增链表求交集
{
linklist pa = la->next, pb = lb->next, pc = lc = la, q;
while (pa && pb)
{
q = new list;
if (pa->data == pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
q = pb;
pb = pb->next;
delete q;
}
else if (pa->data < pb->data)
{
q = pa;
pa = pa->next;
delete q;
}
else
{
q = pb;
pb = pb->next;
delete q;
}
}
while (pa)
{
q = pa;
pa = pa->next;
delete q;
}
while (pb)
{
q = pb;
pb = pb->next;
delete q;
}
pc->next=NULL;
delete lb;
}
int Max(linklist l)//链表中最大值
{
linklist lmax,p=l->next->next;
if (l->next == NULL) return 0;
lmax = l->next;
while (p)
{
if (p->data > lmax->data)
lmax = p;
p = p->next;
}
return lmax->data;
}
void Inverse(linklist& l)//原地逆置
{
linklist p,q;
p = l->next;
l->next = NULL;
while (p)
{
q = p->next;
p->next = l->next;
l->next = p;
p = q;
}
}
void Deleteminmax(linklist& l)//删除递增序列链表大于mink小于maxk的点
{
int mink, maxk;
linklist p,pmin;
pmin = NULL;
p = l->next;
cout << "输入mink:" << endl;
cin >> mink;
cout << "输入maxk:" << endl;
cin >> maxk;
while (p && p->data < mink)
{
pmin = p;
p = p->next;
}
while (p && p->data < maxk)
{
p = p->next;
}
pmin->next = p;
}
int main()
{
linklist L1,L2,L3;
int n;
Newlist(L1);
Newlist(L2);
Newlist(L3);
Createlist(L1);
Createlist(L2);
cout << "链表一:" << endl;
Printlist(L1);
cout << "链表二:" << endl;
Printlist(L2);
cout << "两递增链表合并为一个递增链表:" << endl;
Addlist(L1, L2, L3);
Printlist(L3);
cout << "两递增链表求交集:" << endl;
Intersection(L1, L2, L3);
Printlist(L3);
Inverse(L1);
cout << "逆置后链表:" << endl;
Printlist(L1);
cout << "删除递增序列链表大于等于mink小于maxk的点:" << endl;
Deleteminmax(L1);
Printlist(L1);
}