效果图及代码如下,如有帮助,请帮忙采纳一下,谢谢。
代码:
#include <iostream>
using namespace std;
struct StNode
{
int data;
struct StNode* next;
};
//创建节点
StNode* CreateNode(int d)
{
StNode* node = new StNode;
node->data = d;
node->next = 0;
return node;
}
//创建链表
StNode* CreateList()
{
StNode* head,*p,*t;
head = 0;
p = head;
t = head;
int data;
cout << "请输入链表中各节点的值(在一行中输入多个数字,以空格分隔,以回车结束):" <<endl;
while(1)
{
cin >> data;
t = CreateNode(data);
if(head ==0)
{
head = t;
p = head;
}
else
{
p->next = t;
p = t;
}
if (cin.get() == '\n')
break;
}
return head;
}
//打印链表
void Display(StNode* head)
{
cout << "打印链表:" << endl;
while(head)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
//判断是否重复
int isRepeat(StNode* head,StNode* p)
{
while(head)
{
if(head == p) return 0; //只判断p之前的
if(head->data == p->data) return 1;
else
head = head->next;
}
return 0;
}
//去重
StNode* Quchong(StNode* head)
{
StNode *p,*t,*k,*pre;
p = head->next;
pre = head;
while(p)
{
if(isRepeat(head,p))
{
t = p->next;
pre->next = t;
delete p;
p = t;
}else
{
pre = p;
p = p->next;
}
}
return head;
}
//插入递增链表
StNode* InsertDz(StNode* head, StNode* p)
{
StNode* t,*pre;
if(head == 0) return p;
if(p->data < head->data)
{
p->next = head;
head = p;
return head;
}
pre = head;
t = head->next;
while(t && t->data < p->data)
{
pre = t;
t = t->next;
}
pre->next = p;
p->next = t;
return head;
}
//拆分列表
void SplitList(StNode* head,StNode* &L1,StNode* &L2)
{
StNode* p;
L1 = 0;
L2 = 0;
p = head;
while (head)
{
p = head->next;
head->next = 0;
if(head->data%2==0)
{
L1 = InsertDz(L1,head);
}else
{
L2 = InsertDz(L2,head);
}
head = p;
}
}
void Free(StNode* head)
{
StNode* p = head;
while(head)
{
p = head->next;
delete head; head = p;
}
}
int main()
{
StNode* A,*B,*C;
A = CreateList();
Display(A);
cout << "去重,";
A = Quchong(A);
Display(A);
SplitList(A,C,B); //B是奇数 C是偶数
cout << "分裂后的链表:"<< endl;
Display(B);
Display(C);
//释放空间
Free(B);
Free(C);
return 0;
}
逐个节点遍历链表,相同的删除,处理完所有节点即可。可同时根据奇偶情况产生两个链表,产生的同时进行排