typedef struct _MyListNode
{
int nData;
struct _MyListNode * pNext;
} MyListNode, *PMyListNode;
class MyList
{
public:
MyList()
{
pHead = new MyListNode();
pHead->nData = 0;
pHead->pNext = NULL;
pTail = NULL;
}
~MyList()
{
MyListNode * pTmp = pHead;
while (pTmp != NULL)
{
pHead = pTmp->pNext;
delete pTmp;
pTmp = pHead;
}
pHead = NULL;
}
void Append(int num)
{
MyListNode * pTmp = new MyListNode();
pTmp->nData = num;
pTmp->pNext = NULL;
if (pTail != NULL)
{
pTail->pNext = pTmp;
}
else
{
pHead->pNext = pTmp;
}
pTail = pTmp;
}
void Print()
{
MyListNode * pTmp = pHead->pNext;
while (pTmp != NULL)
{
cout << pTmp->nData << " ";
pTmp = pTmp->pNext;
}
cout << endl;
}
void RemoveRepeat()
{
MyListNode * pNode = pHead->pNext;
while (pNode != NULL)
{
MyListNode * pTmp = pNode->pNext;
while (pTmp != NULL && pNode->nData == pTmp->nData)
{
pNode->pNext = pTmp->pNext;
delete pTmp;
pTmp = pNode->pNext;
}
pNode = pTmp;
}
}
private:
MyListNode * pHead;
MyListNode * pTail;
};
int main()
{
MyList lst;
lst.Append(7);
lst.Append(10);
lst.Append(10);
lst.Append(21);
lst.Append(30);
lst.Append(42);
lst.Append(42);
lst.Append(42);
lst.Append(51);
lst.Append(70);
lst.Print();
lst.RemoveRepeat();
lst.Print();
return 0;
}
非递减单链表删除重复元素
最新推荐文章于 2023-05-12 01:48:40 发布