C++算法4:链表去重

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,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值