#include<iostream>
using namespace std;
struct ListNode
{
int data;
ListNode* next;
//ListNode(int a) :data(a), next(nullptr) {}
};
void printList(ListNode*);
//单链表的就地逆置的定义
void reverseList(ListNode* &head)
{
ListNode* temp;//保存cur的下一个结点
ListNode* cur = head;
ListNode* pre = NULL;
while (cur)
{
temp = cur->next;//保存cur的下一个结点,
cur->next = pre;
pre = cur;
cur = temp;
}
while (pre != NULL)
{
cout << pre->data << " ";
pre = pre->next;
}
cout << endl;
head=pre;
}
//建立非递减有序单链表 思想:把无序链表分成两部分,前半部分有序,再通过插入法把无序的数值
//插入进去,变为有序 (注意:该链表带虚拟头结
void setincreaseList(ListNode*& y)//如果不加&,实参被重置了,原本的实参内容变了,这充分说明了头结点和引用传递的重要性
{
ListNode* dummyNode = new ListNode();
dummyNode->next = y;
ListNode* pre = dummyNode;//每次循环 ,从虚拟头结点开始向后,遍历有序的一部分与要插入的进行比较,使用pre->next->data
//而不是pre->data来进行比较,如果用pre->data来进行比较的话,如果待插入值比比较的值小,就需要用到
//前插法了,反之大的时候,就要用后插法了,如果用pre->next->data来进行比较的话,无论待插入值比比较值大还是小
//都可以用后插法
ListNode* p = y;
ListNode* r = p->next;//r用来保存p的下一个位置
p->next = NULL;
p = r;
while (p != NULL)
{
r = p->next;
pre = dummyNode;
while (pre->next != NULL && pre->next->data < p->data)
{
pre = pre->next;
}
p->next = pre->next;
pre->next = p;
p = r;
}
y = dummyNode->next;//引用传递,可以返回被改变的链表
}
//定义合并两个非递减链表成一个非递减链表 //后插
ListNode* unionincreaseList(ListNode* a, ListNode* b)
{
setincreaseList(a);
setincreaseList(b);
ListNode* dummyNode = new ListNode();//dummyNode=new ListNode();
dummyNode->next = NULL;
ListNode* s, * pa = a, * pb = b;//pa,pb为待插入值
s = dummyNode;
while (pa && pb)
{
if (pa->data <= pb->data)
{
s->next = pa;
pa = pa->next;
s = s->next;
}
else
{
s->next = pb;
pb = pb->next;
s = s->next;
}
}
s->next = pa ? pa : pb;
//delete B;
return dummyNode->next;
}
//定义合并两个非递减序列成一个递减链表 头插法
//算法思想:两个链表已经递增,合并时,从第一个结点开始比较,将小的节点链入链表中,同时后移工作指针
ListNode* uniondecreaeList(ListNode* c, ListNode* d)
{
setincreaseList(c);
setincreaseList(d);
ListNode* dummyNode = new ListNode();
//dummyNode->next = C;
ListNode* r = dummyNode, * s = NULL, * pc = c, * pd = d;//分别为表C D的工作指针
dummyNode->next = NULL;//dummyNode作为结果链表的虚拟头结点,先将结果链表初始化为空
while (pc && pd)//当链表均不为空时循环
{
if (pc->data <= pd->data)
{
s = pc->next;
pc->next = r->next;
r->next = pc;
pc = s;
}
else
{
s = pd->next;
pd->next = r->next;
r->next = pd;
pd = s;
}
}
//接下来处理有一个表已经走到空了
if (pc) pd = pc;
while (pd)
{
s = pd->next;
pd->next = r->next;
r->next = pd;
pd = s;
}
//delete D;
return dummyNode->next;
}
ListNode* setList()
{
int m;
cout << "请输入表长:"; cin >> m;
ListNode* x = new ListNode();
x->next = NULL;
for (int i = 1; i <= m; i++)
{
ListNode* p = new ListNode();
cout << "请输入节点中保存的值:"; cin >> p->data;
p->next = x->next;
x->next = p;
}
ListNode* X = x->next;
return X;
}
//定义打印链表函数
void printList(ListNode* head)
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main()
{
ListNode* x1 = setList();//单链表x1的非递减序列
cout << "输出第一个链表的非递减序列:";
setincreaseList(x1);
printList(x1);
ListNode* y1 = setList();//单链表y1的非递减序列
cout << "输出第二个链表的非递减序列:";
setincreaseList(y1);
printList(y1);
cout << "输出合并后的非递减序列:\n";//合并的 非递减
ListNode* A = unionincreaseList(x1, y1);
printList(A);
cout << "输出合并后的非递增序列:\n";//合并的 非递增
//ListNode* C = uniondecreaeList(x1, y1);
reverseList(A);
printList(A);
return 0;
}