#include <bits/stdc++.h>
using namespace std;
void print(int e)
{
cout<<e<<" ";
}
int main()
{
list<int> 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<int>::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<<l.front()<<"\n":cout<<"链表为空\n";break;
case 9:!l.empty()?cout<<l.back()<<"\n":cout<<"链表为空\n";break;
default:cout<<"输入错误,请重新输入!\n";
}
}
return 0;
}
/************************************************
* 小柳学渣
* 2019/1/23 13:16
************************************************/
网友评论