list构造

#include <iostream>
#include <string>
#include <list>//链表容器
//STL是双向循环链表
using namespace std;
void show(list <int> l1) {
	for (auto a : l1)
		cout << a << " ";
	cout << endl;
}
int main() {
	list <int> l1;
	for (int i = 0; i < 5; i++)
		l1.push_back(i + 1);//尾插
	for (int i = 5; i < 10; i++)
		l1.push_front(i + 1);//头插
	show(l1);
	//这个要写一起,不能分开写
	list <int> l2(l1.begin(), l1.end());
	show(l2);
	list <int> l3(l2);
	show(l3);
	list <int> l4(11,5);
	show(l4);
}

在这里插入图片描述

赋值和交换

#include <iostream>
#include <string>
#include <list>//链表容器
//STL是双向循环链表
using namespace std;
void show(list <int> l1) {
	for (auto a : l1)
		cout << a << " ";
	cout << endl;
}
int main() {
	list <int> l1;
	for (int i = 0; i < 5; i++)
		l1.push_back(i + 1);//尾插
	show(l1);
	list <int> l2;
	l2 = l1;
	show(l2);
	list <int> l3 ;
	l3.assign(l2.begin(),l2.end());
	show(l3);
	list <int> l4;
	l4.assign(5, 1);
	show(l4);
	//交换
	cout << "交换前" << endl;
	cout << "l1->"; show(l1);
	cout << "l2->"; show(l4);
	l4.swap(l1);
	cout << "交换后" << endl;
	cout << "l1->"; show(l1);
	cout << "l2->"; show(l4);
}

在这里插入图片描述

判断空,个数和指定大小

#include <iostream>
#include <string>
#include <list>//链表容器
//STL是双向循环链表
using namespace std;
void show(list <int> l1) {
	for (auto a : l1)
		cout << a << " -> ";
	cout << "NULL" << endl;
}
void isempty(list <int> l1) {
	//这里没有写引用所以不改变实参中l1的值
	//void isempty(list <int> &l1) 会改变下面的值
	//下面会变成15个元素长度
	if (l1.empty()) {
		cout << "线性表为空."<<"正写入默认值"<<endl;
		l1.resize(10, 5);
		isempty(l1);
	}
	else {
		cout << "线性表不为空" << endl;
		cout << "线性表的元素个数:" << l1.size() 
			<<endl<< "l1 -> " ;
		show(l1);
	}

	cout << endl;
}
int main() {
	list <int> l1;
	isempty(l1);
	for (int i = 0; i < 5; i++)
		l1.push_back(i + 1);//尾插
	isempty(l1);
	
}

在这里插入图片描述

插入和删除

#include <iostream>
#include <string>
#include <list>//链表容器
//STL是双向循环链表
using namespace std;
void show(list <int> l1) {
	for (auto a : l1)
		cout << a << " -> ";
	cout << "NULL" << endl;
}
void isempty(list <int> l1) {
	//这里没有写引用所以不改变实参中l1的值
	//void isempty(list <int> &l1) 会改变下面的值
	//下面会变成15个元素长度
	if (l1.empty()) {
		cout << "线性表为空."<<"正写入默认值"<<endl;
		l1.resize(10, 5);
		isempty(l1);
	}
	else {
		cout << "线性表不为空" << endl;
		cout << "线性表的元素个数:" << l1.size() 
			<<endl<< "l1 -> " ;
		show(l1);
	}

	cout << endl;
}
int main() {
	list <int> l1;
	isempty(l1);
	for (int i = 0; i < 5; i++)
		l1.push_back(i + 1);//尾插
	for (int i = 0; i < 5; i++)
		l1.push_front(i + 1);//头插
	//5 4 3 2 1 1 2 3 4 5
	isempty(l1);
	l1.pop_back();//尾删
	isempty(l1);
	l1.pop_front();//头删
	isempty(l1);
	//list是双向迭代器(不能用l1.begin()+x)
	l1.insert(++++l1.begin(), 10);//插在第3个位置
	//(.begin(),10)插在第1个位置
	isempty(l1);
	cout << "删除操作" << endl;
	l1.erase(++++l1.begin());//要用前置
	isempty(l1);
	cout << "移除操作(利用remove删除指定数字1)" << endl;
	l1.remove(1);
	isempty(l1);
	system("pause");
	system("cls");
	isempty(l1);
	cout << "清空操作" << endl;
	l1.clear();
	isempty(l1);
}

在这里插入图片描述
在这里插入图片描述

数据存取

#include <iostream>
#include <string>
#include <list>//链表容器
#include <time.h>
//STL是双向循环链表
using namespace std;
void show(list <int> l1) {
	for (auto a : l1)
		cout << a << " -> ";
	cout << "NULL" << endl;
}
void isempty(list <int> l1) {
	//这里没有写引用所以不改变实参中l1的值
	//void isempty(list <int> &l1) 会改变下面的值
	//下面会变成15个元素长度
	if (l1.empty()) {
		cout << "线性表为空."<<"正写入默认值"<<endl;
		l1.resize(10, 5);
		isempty(l1);
	}
	else {
		cout << "线性表不为空" << endl;
		cout << "线性表的元素个数:" << l1.size() 
			<<endl<< "l1 -> " ;
		show(l1);
	}

	cout << endl;
}
int main() {
	list <int> l1;
	show(l1);
	for (int i = 0; i < 5; i++)
		l1.push_back(i + 1);//尾插
	for (int i = 0; i < 5; i++)
		l1.push_front(i + 1);//头插
	//5 4 3 2 1 1 2 3 4 5
	show(l1);
	cout << "l1 的头结点数据:";
	cout << l1.front() << endl;
	cout << "l1 的尾结点数据:";
	cout << l1.back() << endl;
	cout << ++l1.front()<<endl;
	//利用随机数模拟尾删或头删
	//设置随机数种子
	srand(time(0));
	while (!l1.empty()) {
		int suiji = rand() % 2;
		if (suiji == 0) {
			cout << "本次操作是头删" << endl;
			l1.pop_front();
		}
		else {
			cout << "本次操作是尾删" << endl;
			l1.pop_back();
		}
		show(l1);
	}
}

在这里插入图片描述

反转和排序

#include <iostream>
#include <string>
#include <list>//链表容器
#include <time.h>
#include <algorithm>
//STL是双向循环链表
int cmp(int a, int b) {//首部int是不因类型变化而变化
	return a > b;//若为double则需要加?1:-1;
}
using namespace std;
void show(list <int> l1) {
	for (auto a : l1)
		cout << a << " -> ";
	cout << "NULL" << endl;
}
void isempty(list <int> l1) {
	//这里没有写引用所以不改变实参中l1的值
	//void isempty(list <int> &l1) 会改变下面的值
	//下面会变成15个元素长度
	if (l1.empty()) {
		cout << "线性表为空."<<"正写入默认值"<<endl;
		l1.resize(10, 5);
		isempty(l1);
	}
	else {
		cout << "线性表不为空" << endl;
		cout << "线性表的元素个数:" << l1.size() 
			<<endl<< "l1 -> " ;
		show(l1);
	}

	cout << endl;
}
int main() {
	list <int> l1;
	srand(time(0));
	cout << "反转l1" << endl;
	for (int i = 0; i < 5; i++)
		l1.push_back(rand()%100+1);//尾插
	cout << "反转前:";
	show(l1);
	l1.reverse();//reverse反转reserve扩容
	cout << "反转后:";
	show(l1);
	cout << "排序" << endl;
	//sort(l1.begin(), l1.end());//不可以这样写
	//不支持随机访问的容器不能用sort
	l1.sort();//升序
	cout << "升序:";
	show(l1);
	l1.sort(cmp);//降序,自己写函数
	cout << "降序:";
	show(l1);
}

在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐