2 顺序表ADT模板简单应用算法设计:删除顺序表中的冗余元素
作者: 冯向阳时间限制: 1S章节: DS:线性表
截止日期: 2022-06-30 23:55:00
问题描述 :
目的:使用自行设计的顺序表ADT或STL中的vector模板,设计并实现顺序表应用场合的一些简单算法设计。
应用3:试设计一个算法,删除非空顺序表L中的冗余元素,即使得操作之后的顺序表中只保留操作之前表中所有值都不相同的元素(提纯)。
参考函数原型:
(1)顺序表ADT版本
template<class ElemType>
void Purge_Sq( Sqlist<ElemType> &L );
(2)vector版本
template<class ElemType>
void Purge_Sq( vector<ElemType> &L );
输入说明 :
第一行:顺序表的数据元素类型标记(0:int;1:double;2:char;3:string;其余值:输出err)
第二行:待处理顺序表的数据元素(数据元素之间以空格分隔)
输出说明 :
第一行:提纯前顺序表的遍历结果(数据元素之间以“,”分隔)
空行
第二行:提纯后顺序表的遍历结果(数据元素之间以“,”分隔)
-------------------------------------------------------------------
输入
0
13 5 13 9 32 51 76 5 9 8
---------------------------------------------------------------------
输出
13,5,13,9,32,51,76,5,9,8
13,5,9,32,51,76,8
--------------------------------------------------------------------
很多同学vs和cdblocks上面输出正确
oj上面输出是空的或者溢出了乱码数子。
很多时候问题在cin.get()=='/n'这条语句的判断上面。
不要用这个语句来判断输入的结束。OJ是无法识别的。
你要表示输入停止的话直接强制停止就行,输入完一行然后输入(ctrl+z)就可以停止。
在oj上的话 系统会自动帮你停止输入,在你提交的程序里面就不用写结束判断的语句了。
一定要避免使用cin.get()=='/n' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
或者你也可以用cin.get()!=32来判断
后面几道题我也发了 有用cin.get()!=32来判断的例子(32是空格的asii码)你可以去看看
------------------------------------------------------------------------------------------------------------------
AC代码
#include<iostream>
#include<string>
using namespace std;
template<class T>
struct student
{
T data;
student<T>* next = NULL;
};
template<class T>
void aa(student<T>& a)
{
a.data = 0;
}
template<class T>
void bb(student<T>& b)
{
b.data = "";
}
template<class T>
void creat(student<T>& begin, int& length)
{
student<T>* r = &begin;
T temp;
while (cin >> temp) {
student<T>* p;
p = new student<T>;
p->data = temp;
length++;
r->next = p;
r = p;
}
}
template<class T>
void display(student<T>& begin, int& length)
{
student<T>* p = begin.next;
while (p!=NULL)
{
cout << p->data;
if (p->next != NULL)
{
cout << ",";
}
if (p->next != NULL)
p = p->next;
else
{
cout << endl;
cout << endl;
p = NULL;
return;
}
}
}
template<class T>
void clear(student<T>& a)
{
student<T>* d = a.next;
student<T>* p;
student<T>* q;
while (d != NULL)//删除操作
{
p = d;
q = p->next;//q指向待删除结点
while (q != NULL)
{
if (d->data == q->data)
{
p->next = q->next;//p指向q的下一个结点,删除结点q
q = q->next;
}
else
{
p = q;
q = q->next;
}
}
d = d->next;
}
}
int main()
{
int judge = 0;
cin >> judge;
if (judge == 0)
{
int alength = 0;
student<int> abegin;
aa(abegin);
creat(abegin, alength);
display(abegin, alength);
clear(abegin);
display(abegin, alength);
}
else if (judge == 1)
{
int alength = 0;
student<double> abegin;
aa(abegin);
creat(abegin, alength);
display(abegin, alength);
clear(abegin);
display(abegin, alength);
}
else if (judge == 2)
{
int alength = 0;
student<char> abegin;
aa(abegin);
creat(abegin, alength);
display(abegin, alength);
clear(abegin);
display(abegin, alength);
}
else if (judge == 3)
{
int alength = 0;
student<string> abegin;
bb(abegin);
creat(abegin, alength);
display(abegin, alength);
clear(abegin);
display(abegin, alength);
}
else
{
cout << "err" << endl;
}
return 0;
}