设计一个链表模板类,具备push (向链表尾部添加元素),reverse (反转链表的链接),print (单行从链表头到尾部顺序打印链表,以空格隔开)。
输入描述
每个测试用例共三行,第一行为两个整数,分别表示整型和双精度浮点型链表长度。第二行和第三行分别为两个链表的元素。
输出描述
输出共四行。前两行为整型链表反转前,反转后打印元素。后两行为双精度浮点型打印元素。元素按从链表头到尾顺序打印,以空格隔开。
示例1:
输入:5 5
1 2 3 4 5
1.1 1.2 1.3 1.4 1.5
输出:1 2 3 4 5
5 4 3 2 1
1.1 1.2 1.3 1.4 1.5
1.5 1.4 1.3 1.2 1.1
#include<iostream>
#include<bits/stdc++.h> // STL万能头文件
using namespace std;
//创建一个list容器的实例LISTINT
typedef list<int> LISTINT;
//创建一个list容器的实例LISTDOUBLE
typedef list<double> LISTDOUBLE;
int main()
{
int m, n; // 整数链表长度为m,浮点数链表长度为n。
cin >> m >> n;
//用list容器处理整型数据
LISTINT listInt; // 用LISTINT创建一个名为listInt的list对象
LISTDOUBLE listDouble; // 用LISTDOUBLE创建一个名为listDouble的list对象
LISTINT::iterator i; // 声明i为LISTINT的迭代器
LISTDOUBLE::iterator j; // 声明j为LISTDOUBLE的迭代器
for(int k=0; k<m; k++) // 循环输入m个整数,作为int型链表的元素
{
int tempi;
cin >> tempi;
//从后面向listInt容器中添加数据
listInt.push_back (tempi);
}
for(int h=0; h<n; h++) // 循环输入n个浮点数,作为double型链表的元素
{
double tempd;
cin >> tempd;
//从后面向listDouble容器中添加数据
listDouble.push_back (tempd);
}
//从前向后显示listInt中的数据
for (i = listInt.begin(); i != listInt.end(); i++)
cout << *i << " ";
cout << endl;
//从后向前显示listInt中的数据
LISTINT::reverse_iterator ir;
for (ir = listInt.rbegin(); ir != listInt.rend(); ir++)
cout << *ir << " ";
cout << endl;
//从前向后显示listDouble中的数据
for (j = listDouble.begin(); j != listDouble.end(); j++)
cout << *j << " ";
cout << endl;
//从后向前显示listDouble中的数据
LISTDOUBLE::reverse_iterator jr;
for (jr = listDouble.rbegin(); jr != listDouble.rend(); jr++)
cout << *jr << " ";
cout << endl;
/*
list<int> li;
list<double> ld;
li.push_back(e);
ld.push_back(e);
// l.push_front(e);
*/
}