1.给定排序的链表,删除重复元素,只保留重复元素第一次出现的结点。
(1)代码:
# include <iostream>
using namespace std;
//定义节点结构
typedef struct snode
{
int data;
snode *pnext;
snode(int value)
:data(value), pnext(NULL)
{}
}Node;
//声明函数
void OutLink(Node *pHead);
void Destroy(Node *P);
void DelSame(Node *Head);
//输出链表
void OutLink(Node *pHead)
{
//注意判断指针是否为空
if (pHead == NULL)
{
cout << "链表为空";
}
Node *pCur = pHead->pnext;
while (pCur != NULL)
{
cout << "->" << pCur->data;
pCur = pCur->pnext;
}
cout << endl;
}
//删除指针
void Destroy(Node *P)
{
Node *next;
if (P != NULL)
{
next = P->pnext;
delete P;
P = next;
}
}
//删除重复
void DelSame(Node *Head)
{
Node *Now = Head->pnext;
Node *Next;
while (Now)
{
Next = Now->pnext;
if (Next && (Now->data == Next->data))
{
Now->pnext = Next->pnext;
delete Next;
}
else
{
Now = Next;
}
}
}
//主函数
int main()
{
Node *Head = new Node(0);
Node *Next = Head;
int array[13] = { 1,2,2,3,4,4,4,5,6,6,6,6,7};
int size = sizeof(array) / sizeof(int);
for (int i = 0; i < size; i++)
{
Next->pnext = new Node(array[i]);
Next = Next->pnext;
}
cout << "删除之前" << endl;
OutLink(Head);
DelSame(Head);
cout << "删除之后" << endl;
OutLink(Head);
Destroy(Head);
return 0;
}
(2)结果:
删除之前
->1->2->2->3->4->4->4->5->6->6->6->6->7
删除之后
->1->2->3->4->5->6->7
请按任意键继续. . .
2.给定排序的链表,删除重复元素,只保留重复元素最后一次出现的结点。
(1)代码:
# include <iostream>
using namespace std;
//定义节点结构
typedef struct snode
{
int data;
snode *pnext;
snode(int value)
:data(value), pnext(NULL)
{}
}Node;
//声明函数
void OutLink(Node *pHead);
void Destroy(Node *P);
void DelSame(Node *Head);
//输出链表
void OutLink(Node *pHead)
{
//注意判断指针是否为空
if (pHead == NULL)
{
cout << "链表为空";
}
Node *pCur = pHead->pnext;
while (pCur != NULL)
{
cout << "->" << pCur->data;
pCur = pCur->pnext;
}
cout << endl;
}
//删除指针
void Destroy(Node *P)
{
Node *next;
if (P != NULL)
{
next = P->pnext;
delete P;
P = next;
}
}
//删除重复
void DelSame(Node *Head)
{
Node *Pre = Head;
Node *Now = Head->pnext;
Node *Next;
while (Now)
{
Next = Now->pnext;
if (Next && (Now->data == Next->data))
{
Pre->pnext = Next;
delete Now;
}
else
{
Pre = Now;
}
Now = Pre->pnext;
}
}
//主函数
int main()
{
Node *Head = new Node(0);
Node *Next = Head;
int array[14] = { 1,2,2,3,4,4,4,5,6,6,6,6,7 };
int size = sizeof(array) / sizeof(int);
for (int i = 0; i < size-1; i++)
{
Next->pnext = new Node(array[i]);
Next = Next->pnext;
}
cout << "删除之前" << endl;
OutLink(Head);
DelSame(Head);
cout << "删除之后" << endl;
OutLink(Head);
Destroy(Head);
return 0;
}
(2)结果:
删除之前
->1->2->2->3->4->4->4->5->6->6->6->6->7
删除之后
->1->2->3->4->5->6->7
请按任意键继续. . .
3.给定排序的链表,删除所有重复元素。
(1)代码:
# include <iostream>
using namespace std;
//定义节点结构
typedef struct snode
{
int data;
snode *pnext;
snode(int value)
:data(value), pnext(NULL)
{}
}Node;
//声明函数
void OutLink(Node *pHead);
void Destroy(Node *P);
void DelSame(Node *Head);
//输出链表
void OutLink(Node *pHead)
{
//注意判断指针是否为空
if (pHead == NULL)
{
cout << "链表为空";
}
Node *pCur = pHead->pnext;
while (pCur != NULL)
{
cout << "->" << pCur->data;
pCur = pCur->pnext;
}
cout << endl;
}
//删除指针
void Destroy(Node *P)
{
Node *next;
if (P != NULL)
{
next = P->pnext;
delete P;
P = next;
}
}
//删除重复
void DelSame(Node *Head)
{
Node *Pre = Head;
Node *Now = Pre->pnext;
Node *Next;
bool DelNum;
while (Now)
{
Next = Now->pnext;
DelNum = false;
while (Next && (Now->data == Next->data))
{
Pre->pnext = Next;
delete Now;
Now = Next;
Next = Now->pnext;
DelNum = true;
}
if (DelNum)
{
Pre->pnext = Next;
delete Now;
}
else
{
Pre = Now;
}
Now = Next;
}
}
//主函数
int main()
{
Node *Head = new Node(0);
Node *Next = Head;
int array[14] = { 1,2,2,3,4,4,4,5,6,6,6,6,7 };
int size = sizeof(array) / sizeof(int);
for (int i = 0; i < size - 1; i++)
{
Next->pnext = new Node(array[i]);
Next = Next->pnext;
}
cout << "删除之前" << endl;
OutLink(Head);
DelSame(Head);
cout << "删除之后" << endl;
OutLink(Head);
Destroy(Head);
return 0;
}
(2)结果:
删除之前
->1->2->2->3->4->4->4->5->6->6->6->6->7
删除之后
->1->3->5->7
请按任意键继续. . .