题目描述:用链表实现以下功能
0 显示链表中的内容
1 后跟两个数字 i j 表示在第i个位置插入J
2 后跟一个数组 k 表示删除第k个位置内的内容
#include<iostream>
#include<cstdio>
#include<list>
using namespace std;
int main()
{
int a;
list<int>listint ;
list<int>::iterator pos; //好像定义了一个指针
while(~scanf("%d",&a))
{
if(a == 0)
{
for(pos = listint.begin(); pos != listint.end(); pos++) //指向首地址,首地址对应的是0
{
cout<<*pos; //输出相应地址对应的数据
printf(" ");
}
printf("\n");
}
else if(a == 1)
{
int dos,num;
scanf("%d %d",&dos,&num);
pos = listint.begin();
//在dos位置插入一个数值为num的数
//pos对应的下标应该是dos-1
for(int i = 1; i < dos; i++)
pos++;
listint.insert(pos,num);
}
else
{
int dos;
scanf("%d",&dos);
//删除操作 删除位置为dos的数据
//pos对应的下标应该为dos-1
pos = listint.begin();
for(int i = 1; i < dos; i++)
pos++;
listint.erase(pos);
}
}
return 0;
}
功能实现
链表的一些其他正常操作
(一)创建list对象:
(1)创建没有任何元素的list对象:list<int > l;
(2)创建具有n个元素的list对象:list<int > l(10); //创建具有10个整形元素的list对象l 。
(二)插入元素:
(1)使用push_back()方法从尾部插入元素,链表自动扩张;
(2)使用push_front()方法从头部插入元素,链表自动扩张;
(3)使用insert()方法向迭代器位置插入新元素,链表自动扩张。
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
int main()
{
int i;
list<int> l;
list<int>::iterator pos;
//使用push_back()方法从尾部插入元素
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.push_back(4);
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//使用push_front()方法从头部插入元素
l.push_front(0);
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//使用insert()方法向迭代器位置插入新元素
pos=l.begin();
pos++;
l.insert(pos,9);
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
return 0;
}
输出结果:
1 2 3 4
0 1 2 3 4
0 9 1 2 3 4
(三)遍历元素:
(1)前向遍历:以前向迭代器的方式遍历;
(2)反向遍历:使用反向迭代器进行遍历。
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
int main()
{
int i;
list<int> l;
list<int>::iterator pos;
list<int>::reverse_iterator pos1;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.push_back(4);
//前向遍历
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//反向遍历
for(pos1=l.rbegin();pos1!=l.rend();pos1++)
cout<<*pos1<<" ";
cout<<endl;
return 0;
}
输出结果:
1 2 3 4
4 3 2 1
(四)删除元素:
(1)remove(元素值),删除链表中的元素,值相同的元素都会被删除;
(2)pop_front()删除链表首元素;
(3)pop_back()删除链表尾元素;
(4)erase()删除迭代器位置的元素;
(5)clear()清空链表容器。
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
int main()
{
int i;
list<int> l;
list<int>::iterator pos;
l.push_back(1);
l.push_back(3);
l.push_back(2);
l.push_back(4);
l.push_back(-1);
l.push_back(0);
l.push_back(1);
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//remove(元素值),值相同的元素都会被删除
l.remove(1);
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//pop_front() 删除链表首元素
l.pop_front();
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//pop_back() 删除链表尾元素
l.pop_back();
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//erase() 删除迭代器位置的元素
pos=l.begin();
pos++;
l.erase(pos);
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
//clear()清空链表容器
l.clear();
cout<<"size="<<l.size()<<endl;
return 0;
}
输出结果:
1 3 2 4 -1 0 1
3 2 4 -1 0
2 4 -1 0
2 4 -1
2 -1
size=0
list排序:使用list的成员函数sort()实现升序排序
#include<iostream>
#include<list>
using namespace std;
int main()
{
int i;
list<int> l;
list<int>::iterator pos;
l.push_back(1);
l.push_back(3);
l.push_back(2);
l.push_back(4);
l.push_back(-1);
l.push_back(0);
l.push_back(1);
l.sort();
for(pos=l.begin();pos!=l.end();pos++)
cout<<*pos<<" ";
cout<<endl;
return 0;
}
输出结果:
-1 0 1 1 2 3 4