C++算法4:链表去重

18 篇文章 1 订阅
15 篇文章 0 订阅

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
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值