C++9-2-链表类模板:链表反转

设计一个链表模板类,具备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);
*/
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值