【id:347】【20分】A. 链表类模板
时间限制
1s
内存限制
128MB
题目描述
结点数据类型为int的单链表类CIntList可定义如下:
class CNode
{
public:
int data;
CNode *next;
};
class CIntList
{
private:
CNode *head;
public:
CIntList();
void append(int a); //加到链表最后
void insert(int a, int n); //在第n个结点后加
void remove(int n); //移除第n个结点
int get(int n); //返回第n个结点的数据
void set(int a, int n); //将第n个节点的数据改成a
void print();
~CIntList();
};
试将其改成结点数据类型用参数表示的类模板CList。
输入
第一行输入测试次数
每次测试输入5行,格式为:
数据类型(I:int, D:double, S:string) 数据个数n 数据1 数据2 ... 数据n
插入节点号(0表示插在第1个结点前面) 数据
返回结点号
删除结点号
修改结点号 数据
输出
每次测试输出二行.第1行输出返回操作获得的数据(如出错则输出error),第2行输出所有操作后链表全部结点的数据.
样例查看模式
正常显示
查看格式
输入样例1 <-复制
3
I 5 2 3 5 7 3
1 40
7
7
6 -10
D 6 1.1 2.3 10.05 0.0 -1.8 5.9
4 60.4
5
1
3 -3.7
S 4 this is a test.
0 good
1
8
4 work
输出样例1
error
2 40 3 5 7 -10
60.4
2.3 10.05 -3.7 60.4 -1.8 5.9
good
good this is work test.
在操作链表时,注意下标的取值和含义
在编写析构函数时,删除的是链表的指针头结点时,删除的代码模板需要背
不能直接delete[],否则会报错
template<class T>
CIntList<T>::~CIntList() {
CNode<T> *p;
while (head->next) {
p = head->next;
head->next = p->next;
delete p;
}
delete head;
}
#include "iostream"
using namespace std;
template<class T>
class CNode {
public:
T data;
CNode *next;
CNode() {
next = nullptr;
}
};
template<class T>
class CIntList {
private:
CNode<T> *head;
int size;
public:
CIntList();
void append(T a); //加到链表最后
void insert(T a, int n); //在第n个结点后加
void remove(int n); //移除第n个结点
T get(int n); //返回第n个结点的数据
void set(T a, int n); //将第n个节点的数据改成a
void print();
~CIntList();
};
template<class T>
CIntList<T>::CIntList() {
size = 0;
this->head = new CNode<T>;
}
template<class T>
void CIntList<T>::append(T a) {
CNode<T> *point = head;
while (point->next != nullptr) {
point = point->next;
// cout << 666 << endl;
}
point->next = new CNode<T>;
point->next->data = a;
size++;
}
template<class T>
void CIntList<T>::insert(T a, int n) {
if (n < 0 || n > size) {
return;
}
CNode<T> *point = head;
for (int i = 0; i < n; ++i) {
point = point->next;
}
CNode<T> *temp = new CNode<T>;
temp->data = a;
temp->next = point->next;
point->next = temp;
size++;
}
template<class T>
void CIntList<T>::remove(int n) {
if (n <= 0 || n > size) {
return;
}
n--;
CNode<T> *point = head;
for (int i = 0; i < n; ++i) {
point = point->next;
}
point->next = point->next->next;
size--;
}
template<class T>
T CIntList<T>::get(int n) {
if (n <= 0 || n > size) {
cout << "error" << endl;
return 0;
}
// n--;
CNode<T> *point = head;
for (int i = 0; i < n; ++i) {
point = point->next;
}
cout << point->data << endl;
return point->data;
}
template<class T>
void CIntList<T>::set(T a, int n) {
if (n <= 0 || n > size) {
return;
}
n--;
CNode<T> *point = head;
for (int i = 0; i < n; ++i) {
point = point->next;
}
point->next->data = a;
}
template<class T>
void CIntList<T>::print() {
CNode<T> *point = head->next;
while (point->next != nullptr) {
cout << point->data << " ";
point = point->next;
}
cout << point->data << endl;
}
template<class T>
CIntList<T>::~CIntList() {
CNode<T> *p;
while (head->next) {
p = head->next;
head->next = p->next;
delete p;
}
delete head;
}
int main() {
//第一行输入测试次数
int times;
cin >> times;
//
// 每次测试输入5行,格式为:
//
// 数据类型(I:int, D:double, S:string) 数据个数n 数据1 数据2 ... 数据n
while (times--) {
char type;
cin >> type;
switch (type) {
case 'I': {
CIntList<int> cIntList;
int number;
cin >> number;
int *data = new int[number];
for (int i = 0; i < number; ++i) {
cin >> data[i];
cIntList.append(data[i]);
}
// cout << 666 << endl;
//
// 插入节点号(0表示插在第1个结点前面) 数据
int index, value;
cin >> index >> value;
cIntList.insert(value, index);
//
// 返回结点号
cin >> index;
cIntList.get(index);
//
// 删除结点号
// cIntList.print();
cin >> index;
cIntList.remove(index);
//
// 修改结点号 数据
cin >> index >> value;
cIntList.set(value, index);
cIntList.print();
break;
}
case 'D': {
CIntList<double> cIntList;
int number;
cin >> number;
double *data = new double[number];
for (int i = 0; i < number; ++i) {
cin >> data[i];
cIntList.append(data[i]);
}
//
// 插入节点号(0表示插在第1个结点前面) 数据
int index;
double value;
cin >> index >> value;
cIntList.insert(value, index);
//
// 返回结点号
cin >> index;
cIntList.get(index);
//
// 删除结点号
cin >> index;
cIntList.remove(index);
//
// 修改结点号 数据
cin >> index >> value;
cIntList.set(value, index);
cIntList.print();
break;
}
case 'S': {
CIntList<string> cIntList;
int number;
cin >> number;
string *data = new string[number];
for (int i = 0; i < number; ++i) {
cin >> data[i];
cIntList.append(data[i]);
}
//
// 插入节点号(0表示插在第1个结点前面) 数据
int index;
string value;
cin >> index >> value;
cIntList.insert(value, index);
//
// 返回结点号
cin >> index;
cIntList.get(index);
//
// 删除结点号
cin >> index;
cIntList.remove(index);
//
// 修改结点号 数据
cin >> index >> value;
cIntList.set(value, index);
cIntList.print();
break;
}
}
}
}