数据结构实验之链表一:顺序建立链表(STL库list)

来源:互联网 发布:js隐藏某个标签 编辑:程序博客网 时间:2024/05/16 14:00

数据结构实验之链表一:顺序建立链表
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
Input
第一行输入整数的个数N;
第二行依次输入每个整数。
Output
输出这组整数。
Example Input

8
12 56 4 6 55 15 33 62

Example Output

12 56 4 6 55 15 33 62

这几天正在学习STL库的list 正好找了几道关于链表的题目
做一做 一块介绍一下一个算法for_each(迭代器开始位置,迭代器结束位置,一元谓词)
for_each就是遍历迭代器 这里是c++已经为我们写好了的
一元谓词就是函数调用 这里所调用的函数必须返回值bool类型才可以

#include<iostream>#include<list>#include<algorithm>using namespace std;struct OUPUT{    int mm;    OUPUT()    {        mm = 0;    }    void operator()(const int &tt)    {        mm==0?cout<<tt:cout<<" "<<tt;        mm++;    }};int main(){    int n;    cin>>n;    list<int> ma;    for(int i=0;i<n;i++)    {        int tt;        cin>>tt;        ma.push_back(tt);    }    for_each(ma.begin(),ma.end(),OUPUT());//函数调用 如果用结构体这里得需要生成对象 如果是函数直接出传入函数名字就行了    cout<<endl;    return 0;}
阅读全文
0 0
原创粉丝点击