C++ STL list 链表

#include 
using namespace std;

void print(int e)
{
    cout< l;
    int choice,e; 
    while(true)
    {
        cout<<"1.头部插入元素\n"
              "2.尾部插入元素\n"
              "3.删除第一个元素\n"
              "4.删除最后一个元素\n" 
              "5.遍历\n"
              "6.清空所有元素\n"
              "7.判断是否为空\n"
              "8.输出第一个元素\n"
              "9.输出最后一个元素\n";
        cin>>choice;
        switch(choice)
        {
            case 1:cout<<"输入要插入的元素:";cin>>e;l.push_front(e);cout<<"插入成功\n";break;
            case 2:cout<<"输入要插入的元素:";cin>>e;l.push_back(e);cout<<"插入成功\n";break;
            case 3:if(!l.empty()){l.pop_front();cout<<"删除成功\n";}else cout<<"链表为空\n";break;
            case 4:if(!l.empty()){l.pop_back();cout<<"删除成功\n";}else cout<<"链表为空\n";break; 
            case 5:if(!l.empty())for_each(l.begin(),l.end(),print);else cout<<"链表为空";cout<<"\n";break;
            //case 5:if(!l.empty())for(list::iterator i=l.begin();i!=l.end();i++)cout<<*i<<" ";else cout<<"链表为空";cout<<"\n";break;
            case 6:l.clear();cout<<"清除成功\n";break;
            case 7:cout<<(l.empty()?"空\n":"不空\n");break; 
            case 8:!l.empty()?cout<

你可能感兴趣的:(C++ STL list 链表)