题目内容
给定一个升序的单链表,删除单链表中的重复数据项,只保留独立的元素。
问题相关代码
#include <iostream>
using namespace std;
template <class T>
structhttps://img-mid.csdnimg.cn/release/static/image/mid/ask/591763467946145.png "#left")
Node
{
T data = 0;
struct Node <T>* next;
Node() :data(0), next(NULL){}
void print()
{
cout << data <<" ";
}
};
template<class T>
class LinkList
{
public:
LinkList()
{
front = new Node <T>;
front->next = NULL;
}
LinkList(T a[], int n)
{
front = new Node<T>;
int i;
for (i = n - 1; i >= 0; i--)
{
Node <T>* s = new Node<T>;
s->data = a[i];
s->next = front->next;
front->next = s;
}
}
void PrintList();
void DeleteRe();
private:
Node <T>* front;
};
template<class T>
void LinkList<T>::PrintList()
{
if (front->next == NULL)
{
cout << "empty" ;
}
else
{
Node<T>* p = front->next;
while (p)
{
p->print();
p = p->next;
}
}
}
template<class T>
void LinkList<T>::DeleteRe()
{
if (front == NULL)
{
return;
}
Node<T>* p = front ;
while (p->next&&p->next ->next)
{
if (p->next->data ==p->next->next->data)
{
Node<T>* q = p->next->next;
while (q && q->data == p->next ->data)
{
q = q->next;
}
p->next = q;
}
else
{
p = p->next;
}
}
}
int main()
{
int len;
cin >> len;
if (len <= 0)
{
return 0;
}
else
{
int *array=new int [len];
for (int i = 0; i < len; i++)
{
cin >> array[i];
}
LinkList<int> L1(array, len);
L1.DeleteRe();
L1.PrintList();
return 0;
}
}
运行结果及报错内容
oj上看不到问题到底是什么