题目描述
给定n个整数,按输入顺序建立单链表,删除其中的重复数字,输出结果链表。(要求不可以构建新结点,不可以定义新链表。在原链表上删除。)
输入
测试次数t
每组测试数据一行:
n(表示有n个整数),后跟n个数字
输出
对每组测试数据,输出删除重复数字后的结果链表表长和每个元素,具体格式见样例。
输入输出样例
输入样例1 <-复制
3
10 1 2 3 4 1 2 10 20 30 20
5 1 1 1 1 1
6 20 22 22 22 22 20
输出样例1
7: 1 2 3 4 10 20 30
1: 1
2: 20 22
AC代码
#include <iostream>
using namespace std;
#define OK 0
#define ERROR -1
//结点类定义
class ListNode {
public:
int data;
ListNode* next;
ListNode()
{
data = -9999, next = NULL;
}
ListNode(int item, ListNode* pt)
{
data = item, next = pt;
}
};
class LinkList {
//带头结点的单链表,位置从0到n,0是头结点,1是首结点,n是尾结点
private:
ListNode* head; //头结点
int size; //表长
ListNode* index(int i); //定位函数,返回第i个结点
public:
LinkList(); //构造函数,创建头结点
//~LinkList(); //析构函数,逐个结点回收
int LL_insert(int item, int i); //第i位置插入元素,操作成功或失败返回OK或ERROR
int LL_del(int i); //删除第i位置的元素,操作成功或失败返回OK或ERROR
int LL_get(int i); //获取位置i的元素的数值,操作成功或失败返回OK或ERROR
void LL_print(); //打印单链表所有数据
int huan(int a, int b)
{
if (a <= 0 || b <= 0 || a > size || b > size)
return 0;
ListNode* p = head,*q=head;
int i;
for (i = 1; i < a; i++)
p = p->next;
for (i = 1; i < b; i++)
q = q->next;
ListNode* r = p->next, * s = q->next;
ListNode w = *p;
p->next = q->next;
q->next = w.next;
w = *r;
r->next = s->next;
s->next = w.next;
return 1;//注意要有返回值!!!!!哭死debug了半天
}
void show()
{
ListNode* p = head;
int i;
cout << size << ": ";
for (i = 0; i < size; i++)
{
p = p->next;
cout << p->data;
if (i != size - 1)
cout << " ";
else
cout << endl;
}
}
ListNode* tou()
{
return head;
}
int LL_merge(ListNode* La, ListNode* Lb)
{
ListNode* p = La->next, * q = Lb->next;
ListNode* s=La;
while (p&&q)
{
if (p->data <= q->data)
{
s->next = p;
s = p;
p = p->next;
}
else
{
s->next = q;
s = q;
q = q->next;
}
}
s->next = p ? p : q;
int shu = 0;
p = La;
while (p->next != NULL)
{
shu++;
p = p->next;
}
size = shu;
return 1;
}
void sanchu()
{
ListNode* p = head->next;
ListNode* q,*s;
while (p != NULL)
{
q = p->next;
while (q!= NULL)
{
if (p->data == q->data)
{
s = p;
while (s->next != q)
s = s->next;
s->next = q->next;
size--;
q = s->next;
}
else
q = q->next;
}
p = p->next;
}
}
};
//提示:index函数可以不使用。如果不用要在填空区域定义一个空函数体保证语法正确性
LinkList::LinkList() : size(0)
{
head = new ListNode();
}
/*
LinkList::~LinkList() //要逐个结点回收
{
ListNode* p, * q;
p = head->next;
while (p != NULL) //此处会出错所以注释掉
{
q = p; p = p->next;
delete q;
}
size = 0;
head->next = NULL;
}
//以下完成其他类函数定义
*/
/********** Write your code here! **********/
ListNode* LinkList::index(int i)//这个函数用不到所以乱写的
{
ListNode* s,a;
s = &a;
return s;
}
int LinkList::LL_insert(int item, int i)
{
if (i <= 0 || i > size+1)
return ERROR;
ListNode* p;
p = head;
int j;
for (j = 1; j < i; j++)
p = p->next;
ListNode* s = new ListNode(item,p->next);
p->next = s;
size++;
return OK;
}
int LinkList::LL_del(int i)
{
if (i <= 0 || i > size)
return ERROR;
ListNode* p;
p = head;
int j;
for (j = 1; j < i; j++)
p = p->next;
ListNode* s = p->next;
p->next = s->next;
delete s;
size--;
return OK;
}
int LinkList::LL_get(int i)
{
if (i <= 0 || i > size)
return ERROR;
ListNode* p;
p = head;
int j;
for (j = 1; j < i; j++)
p = p->next;
return p->next->data;
}
void LinkList::LL_print()
{
ListNode* p;
p = head->next;
int j;
for (j = 0; j < size; j++)
{
cout << p->data;
if (j != size - 1)
cout << " ";
else
cout << endl;
p = p->next;
}
}
/*******************************************/
//主函数定义
int main()
{
int t;
cin >> t;
int n, i;
int zhi;
while (t--)
{
cin >> n;
LinkList biao;
for (i = 1; i <= n; i++)
{
cin >> zhi;
biao.LL_insert(zhi, i);
}
biao.sanchu();
biao.show();
}
}
(by 归忆)