#include<iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
typedef Node* List;
int InitList(List& list)//初始化
{
list = new Node;
list->next = NULL;
return 0;
}
int DestroyList(List& list)//销毁列表
{
Node* p = list;
while (p != NULL)
{
Node* temp = p;
p = p->next;
delete temp;
}
list = NULL;
return 0;
}
int AddNode(List& list, int data)//添加新的节点
{
Node* newnode = new Node;
newnode->data = data;
newnode->next = NULL;
Node* p = list;
while (p->next != NULL)
{
p = p->next;
}
p->next = newnode;
return 0;
}
void RemoveDuplicate(List& list)//移除重复的元素
{
Node* p = list->next;
while (p != NULL)
{
Node* q = p;
while (q->next != NULL)
{
if (q->next->data == p->data)
{
Node* temp = q->next;
q->next = q->next->next;
delete temp;
}
else
{
q = q->next;
}
}
p = p->next;
}
}
void PrintList(const List& list)//打印输出
{
Node* p = list->next;
if (p == NULL) {
cout << "list is empty" << endl;
}
else {
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
}
cout << endl;
}
int main() {
int n;
while (cin >> n) {
if (n == 0) {
cout << "list is empty" << endl;
continue;
}
List list;
InitList(list); // 初始化单链表
for (int i = 0; i < n; i++) {
int v;
cin >> v;
AddNode(list, v); // 在单链表的末尾增加节点
}
PrintList(list); // 输出链表
RemoveDuplicate(list); // 删除重复元素
PrintList(list); // 再次输出链表
DestroyList(list); // 销毁单链表
}
return 0;
}