// DeleteDuplicateNode.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
typedef struct SNode {
int value;
SNode * pNext;
SNode(int v):value(v),pNext(NULL){}
}SNode;
void Print(SNode* head) {
SNode* p = head->pNext;
while (p)
{
cout << p->value;
p = p->pNext;
if (p)
cout << "->";
}
cout << endl;
}
void Delete(SNode* head) {
while (head)
{
SNode* t = head;
head = head->pNext;
delete t;
}
}
void DeleteDuplicateNode(SNode* head) {
SNode* p = head->pNext;
SNode* p1 = p;
p = p->pNext;
while(p)
{
if (p1->value == p->value) {
SNode* temp = p;
p = p->pNext;
p1->pNext = p;
delete temp;
}
else {
p1 = p;
p = p->pNext;
}
}
}
int main()
{
SNode* head = new SNode(0);
for (int i = 0; i < 15; i++) {
SNode* p = new SNode(rand() % 6);
p->pNext = head->pNext;
head->pNext = p;
}
cout << "Before:\t";
Print(head);
DeleteDuplicateNode(head);
cout << "After:\t";
Print(head);
Delete(head);
system("pause");
return 0;
}
[C++]链表中删除连续重复出现的结点
最新推荐文章于 2024-08-11 18:47:00 发布