C++ stl容器list的底层模拟实现

目录

前言:

1.创建节点

2.普通迭代器的封装

3.反向迭代器的封装

为什么要对正向迭代器进行封装?

4.const迭代器

5.构造函数

6.拷贝构造

7.赋值重载

8.insert

9.erase

10.析构

11.头插头删,尾插尾删

12.完整代码+简单测试

总结:


前言:

模拟实现list,本篇的重点就是由于list是一个双向循环链表结构,所以我们对迭代器的实现不能是简单的指针的++,--了,因为我们知道,链表的存储不一定是连续的,所以直接++,--是链接不起来节点的,所以我们要对迭代器也就是对节点的指针进行封装。结尾会附上完整的代码。

1.创建节点

	template<class T>struct list_node{list_node<T>* _prev;list_node<T>* _next;T _data;list_node(const T& x= T())//这里不给缺省值可能会因为没有默认构造函数而编不过:_prev(nullptr),_next(nullptr),_data(x){}};

注意给缺省值,这样全缺省就会被当做默认构造了,不会因为没有默认构造而报错。

我们实现的list是带哨兵位的,它同时是迭代器的end()(因为是双向循环的list)。

 

2.普通迭代器的封装

	template<class T,class Ref,class Ptr>struct _list_iterator{typedef list_node<T> node;typedef _list_iterator<T, Ref, Ptr> self;node* _node;//对迭代器也就是节点的指针进行封装,因为list迭代器是不能直接++的_list_iterator(node* n):_node(n){}Ref operator*()//返回的必须是引用,不然改变不了外面的对象的成员,要支持对自己解引用改变值就要用应用{return _node->_data;}Ptr operator->(){return &(_node->_data);//返回地址,再解引用直接访问数据}self& operator++(){_node = _node->_next;return *this;}self operator++(int){self tmp(*this);//默认的拷贝构造可以,因为没有深拷贝_node = _node->_next;return tmp;}self& operator--(){_node = _node->_prev;return *this;}self operator--(int){self tmp(*this);_node = _node->_prev;return tmp;}bool operator!=(const self& s){return _node != s._node;}bool operator==(const self& s){return _node == s._node;}};

注意list是双向迭代器,可以++,--,不能+,-

这里对迭代器的实现就如我们开始所说的, 迭代器的实现就是使用节点的指针实现的,而我们不能直接对list创建出的节点进行++,--,所以要进行一层封装;然后再对节点指针初始化。

重载解引用时要注意返回的是引用,不然对自己解引用的时候,返回值如果是临时的,是改变不了内部的data的。

对于箭头的解引用,是为了支持这样的场景:

struct AA{int _a1;int _a2;AA(int a1=0,int a2=0):_a1(a1),_a2(a2){}};void test_list2(){list<AA> lt;lt.push_back(AA(1,1));lt.push_back(AA(2, 2));lt.push_back(AA(3, 3));list<AA>::iterator it = lt.begin();while (it != lt.end()){//cout << (*it)._a1 << " "<<(*it)._a2<<endl;cout << it->_a1 << " " << it->_a2 << endl;//面对这样的类型,需要重载->,.也可以访问,但是有点别扭++it;}cout << endl;}

迭代器遇到箭头,返回对象的地址也就是节点数据的地址,再解引用找到成员。或者说node中的data就是存放的是对象(也就是用来初始化的数据),然后重载的->拿到对象的地址,再->去访问里面的成员变量_a1。

对于前置后置++与--,前置就返回对象的引用,是传引用返回;后置需要进行拷贝给一个临时的对象,再对调用对象++--,返回的是tmp也就是没有改变的对象,是传值返回。注意区分前置后置,后置要加上参数int。

3.反向迭代器的封装

namespace my_iterator
{template<class Iterator,class Ref,class Ptr>struct ReverseIterator{typedef ReverseIterator<Iterator,Ref,Ptr> self;Iterator _cur;ReverseIterator(Iterator it):_cur(it){}Ref operator*(){Iterator tmp = _cur;//因为要--,而解引用是不能改值的,所以用tmp改并返回--tmp;return *tmp;}Ptr operator->(){return &operator*();//&this->operator*()}self operator++(){--_cur;//直接的++--就能直接改了,所以可以直接返回原对象,--(this->_cur)return *this;}self operator--(){++_cur;return *this;}bool operator!=(const self& s){return _cur != s._cur;}};
}

第一个模版参数就是任意类型的迭代器区间,因为我们实现反向迭代器需要现有正向迭代器。

一样的不能直接++--,所以进行一层封装,此时_cur就指向传的迭代器的位置。

对解引用的重载一样是要返回引用,不然返回的是一个临时的变量对自己解引用就没用了,也只有返回的是引用才能修改。例如我们要传的是begin(),那反向迭代器就应该从哨兵位开始,所以要先对传过来的迭代器进行--。

箭头就是返回当前位置迭代器的地址,所以是直接复用上面的。

++--与正向的迭代器相反,而_cur的类型就是传过来的迭代器类型,++--会调用传过来迭代器类型的重载。

 

为什么要对正向迭代器进行封装?

4.const迭代器

	typedef list_node<T> node;
public:typedef _list_iterator<T, T&, T*> iterator;typedef _list_iterator<T, const T&, const T*> const_iterator;typedef ReverseIterator<iterator,T&,T*> reverse_iterator;typedef ReverseIterator<iterator, const T&, const T*> const_reverse_iterator;const_iterator begin() const//本身const迭代器是让迭代器指向的内容不能修改,但是这样用const修饰迭代器本身也不能修改了{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head);}

 提供const版本,供const修饰的对象调用,防止权限的放大。

那为什么提供完const版本了,const版本已经可以供普通迭代器与const迭代器使用,还单独提出来这个版本?和因为const迭代器还需要迭代器也就是节点指针指向的内容不能修改。例如it是const类型迭代器的对象,*it就可以,++it也可以,但是(*it)++就不可以。

5.构造函数

		void empty_Init(){_head = new node;_head->_next = _head;_head->_prev = _head;}list(){empty_Init();}template<class Iterator>list(Iterator first, Iterator end){empty_Init();//别忘加上哨兵位,没有哨兵位识别不了endwhile (first != end){push_back(*first);first++;//这里的++first会调用重载的,因为传过来的是一个迭代器}}

哨兵位是空的,不放数据,但是哨兵位是正向迭代器的end,要加上。

默认无参构造就只有哨兵位,提供的迭代器的构造也要有哨兵位。

first++不用担心,first是迭代器类型的,所以会调用迭代器的++。 

6.拷贝构造

		//传统的拷贝构造//list(const list<T>& lt)//{//	empty_Init();//	for (auto e : lt)//	{//		push_back(e);//this->push_back(e)//	}//}void swap(list<T>& tmp)//要使用库中的swap,而库中的swap就不带const;况且交换的是头节点,const修饰的就不能修改指向{std::swap(_head, tmp._head);}//现代的拷贝构造list(const list<T>& lt){empty_Init();list<T> tmp(lt.begin(), lt.end());//为什么还要多一个变量,因为下面swap的参数没有const,而拷贝构造要加constswap(tmp);//this->swap(tmp)}

拷贝构造,直接使用库中的swap,交换头节点也就是哨兵位的指向就行,因为链表后面的关系都通过头节点找到,所以也就相当于都交换了。

注意库中swap的参数:

 

7.赋值重载

		list<T>& operator=(list<T> lt)//参数不能使用引用,使用引用再使用swap交换,原来赋值的值就被改了{swap(lt);return *this;}

一样是使用库中的swap,但是赋值的参数不能是引用,例如L1=L3,用引用再加上使用swap交换头节点的指向,L3就被改了,我们要求的是赋值是不能改变赋过来的对象的,内置类型也是(a=b)。 

 

8.insert

		void insert(iterator pos,const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}

链接节点即可,注意插入的值可能是任意类型,所以要用模版参数并且带上const与引用,防止是内置类型的值是const,传过来权限放大。

插入pos位置,也就是在pos前和pos位置之间插入。 

 

9.erase

		iterator erase(iterator pos){assert(pos != end());node* cur = pos._node;node* prev = cur->_prev;node* next = cur->_next;prev->_next = next;next->_prev = prev;delete pos._node;return iterator(next);}

注意删除完返回删除数据的下一个迭代器位置。

删除就是找前找后,删除节点,链接前后。

_node是new出来的,注意配套使用。

 

10.析构

void clear()
{iterator it = begin();while (it != end()){it= erase(it);//删除后返回的是下一个数据的位置,所以循环就走起来了}
}~list()
{clear();delete _head;_head = nullptr;
}

注意迭代器的erase删除后返回的是删除数据的下一个迭代器位置,所以用it接收就不怕迭代器失效了,同时循环也走起来了。 

11.头插头删,尾插尾删

		void push_back(const T& x){/*node* tail = _head->_prev;node* newnode = new node(x);tail->_next = newnode;newnode->_prev = tail;_head->_prev = newnode;newnode->_next = _head;*/insert(end(), x);}void push_front(const T& x){insert(begin(),x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}

直接复用即可。 

12.完整代码+简单测试

封装的反向迭代器: 

#pragma oncenamespace my_iterator
{template<class Iterator,class Ref,class Ptr>struct ReverseIterator{typedef ReverseIterator<Iterator,Ref,Ptr> self;Iterator _cur;ReverseIterator(Iterator it):_cur(it){}Ref operator*(){Iterator tmp = _cur;//因为要--,而解引用是不能改值的,所以用tmp改并返回--tmp;return *tmp;}Ptr operator->(){return &operator*();}self operator++(){--_cur;//直接的++--就能直接改了,所以可以直接返回原对象return *this;}self operator--(){++_cur;return *this;}bool operator!=(const self& s){return _cur != s._cur;}};
}
#pragma once
#include "my_iterator.h"#include <iostream>
#include <assert.h>
#include <list>using namespace my_iterator;
using namespace std;namespace my_list
{template<class T>struct list_node{list_node<T>* _prev;list_node<T>* _next;T _data;list_node(const T& x= T())//这里不给缺省值可能会因为没有默认构造函数而编不过:_prev(nullptr),_next(nullptr),_data(x){}};template<class T,class Ref,class Ptr>struct _list_iterator{typedef list_node<T> node;typedef _list_iterator<T, Ref, Ptr> self;node* _node;//对迭代器也就是节点的指针进行封装,因为list迭代器是不能直接++的_list_iterator(node* n):_node(n){}Ref operator*()//返回的必须是引用,不然改变不了外面的对象的成员,要支持对自己解引用改变值就要用应用{return _node->_data;}Ptr operator->(){return &(_node->_data);//返回地址,再解引用直接访问数据}self& operator++(){_node = _node->_next;return *this;}self operator++(int){self tmp(*this);//默认的拷贝构造可以,因为没有深拷贝_node = _node->_next;return tmp;}self& operator--(){_node = _node->_prev;return *this;}self operator--(int){self tmp(*this);_node = _node->_prev;return tmp;}bool operator!=(const self& s){return _node != s._node;}bool operator==(const self& s){return _node == s._node;}};template<class T>class list{typedef list_node<T> node;public:typedef _list_iterator<T, T&, T*> iterator;typedef _list_iterator<T, const T&, const T*> const_iterator;typedef ReverseIterator<iterator,T&,T*> reverse_iterator;typedef ReverseIterator<iterator, const T&, const T*> const_reverse_iterator;void empty_Init(){_head = new node;_head->_next = _head;_head->_prev = _head;}list(){empty_Init();}template<class Iterator>list(Iterator first, Iterator end){empty_Init();//别忘加上哨兵位,没有哨兵位识别不了endwhile (first != end){push_back(*first);first++;//这里的++first会调用重载的,因为传过来的是一个迭代器}}//传统的拷贝构造//list(const list<T>& lt)//{//	empty_Init();//	for (auto e : lt)//	{//		push_back(e);//this->push_back//	}//}void swap(list<T>& tmp)//要使用库中的swap,而库中的swap就不带const;况且交换的是头节点,const修饰的就不能修改指向{std::swap(_head, tmp._head);}//现代的拷贝构造list(const list<T>& lt){empty_Init();list<T> tmp(lt.begin(), lt.end());//为什么还要多一个变量,因为下面swap的参数没有const,而拷贝构造要加constswap(tmp);//this->swap(tmp)}list<T>& operator=(list<T> lt)//参数不能使用引用,使用引用再使用swap交换,原来赋值的值就被改了{swap(lt);return *this;}void clear(){iterator it = begin();while (it != end()){it= erase(it);//删除后返回的是下一个数据的位置,所以循环就走起来了}}~list(){clear();delete _head;_head = nullptr;}iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);//哨兵位就是end}const_iterator begin() const//本身const迭代器是让迭代器指向的内容不能修改,但是这样用const修饰迭代器本身也不能修改了{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head);}reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}void push_back(const T& x){/*node* tail = _head->_prev;node* newnode = new node(x);tail->_next = newnode;newnode->_prev = tail;_head->_prev = newnode;newnode->_next = _head;*/insert(end(), x);}void push_front(const T& x){insert(begin(),x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}void insert(iterator pos,const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}iterator erase(iterator pos){assert(pos != end());node* cur = pos._node;node* prev = cur->_prev;node* next = cur->_next;prev->_next = next;next->_prev = prev;delete pos._node;return iterator(next);}private:node* _head;};void print_list(const list<int>& lt){list<int>::const_iterator it = lt.begin();//不能直接这样写,传递过来的this指针也是const list<int>*,权限放大了,要提供const版本while (it != lt.end()){cout << *it << " ";++it;}cout << endl;}void test_list1(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list<int>::iterator it = lt.begin();//=调用默认的拷贝构造,是浅拷贝,但是可以,让it也指向begin的位置while (it != lt.end()){cout << *it << " ";++it;}cout << endl;for (auto e : lt){cout << e << " ";}cout << endl;print_list(lt);}struct AA{int _a1;int _a2;AA(int a1 = 0, int a2 = 0):_a1(a1), _a2(a2){}};void test_list2(){list<AA> lt;lt.push_back(AA(1, 1));lt.push_back(AA(2, 2));lt.push_back(AA(3, 3));list<AA>::iterator it = lt.begin();while (it != lt.end()){//cout << (*it)._a1 << " "<<(*it)._a2<<endl;cout << it->_a1 << " " << it->_a2 << endl;//面对这样的类型,需要重载->,.也可以访问,但是有点别扭++it;}cout << endl;}void test_list3(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);auto pos = lt.begin();++pos;lt.insert(pos, 20);for (auto e : lt){cout << e << " ";}cout << endl;lt.push_back(100);lt.push_front(1000);for (auto e : lt){cout << e << " ";}cout << endl;lt.pop_back();lt.pop_front();for (auto e : lt){cout << e << " ";}cout << endl;}void test_list4(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);for (auto e : lt){cout << e << " ";}cout << endl;lt.clear();for (auto e : lt){cout << e << " ";}cout << endl;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(40);for (auto e : lt){cout << e << " ";}cout << endl;}void test_list5(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);for (auto e : lt){cout << e << " ";}cout << endl;list<int> lt2(lt);for (auto e : lt2){cout << e << " ";}cout << endl;list<int> lt3;lt3.push_back(10);lt3.push_back(20);lt3.push_back(30);for (auto e : lt3){cout << e << " ";}cout << endl;lt2 = lt3;for (auto e : lt2){cout << e << " ";}cout << endl;}void test_list6(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list<int>::iterator it = lt.begin();//=调用默认的拷贝构造,是浅拷贝,但是可以,让it也指向begin的位置while (it != lt.end()){(*it) *= 2;cout << *it << " ";++it;}cout << endl;list<int>::reverse_iterator rit = lt.rbegin();while (rit != lt.rend()){cout << *rit << " ";++rit;}cout << endl;/*for (auto e : lt){cout << e << " ";}cout << endl;print_list(lt);*/}}

总结:

重点在迭代器与反向迭代器的的封装,其它的内容与其它的容器大致相同。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/823320.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

你也许不知道的 Confluence 快捷操作

Confluence 是一种企业知识管理和协作平台&#xff0c;用于创建、共享和组织团队的文档、知识和想法。它支持团队成员进行实时协作、评论和编辑文档&#xff0c;提供了强大的搜索功能&#xff0c;方便用户快速找到需要的信息。 Confluence 快捷键解析&#xff0c;标注了对应的…

北斗导航 | ARAIM算法的原理和性能测试

===================================================== github:https://github.com/MichaelBeechan CSDN:https://blog.csdn.net/u011344545 ===================================================== ARAIM算法的原理和性能测试 针对高级接收机自主完好性监视(ARAIM)算法…

创新力作 | 模块化快建办公训练中心盛大开业

在上海国际旅游度假区的湖畔&#xff0c;由优积科技建造的城市赛艇中心如同一幅动人的画卷&#xff0c;展现在世人面前。这座赛艇中心不仅是赛艇运动的圣地&#xff0c;更是一个融合了技术创新与建筑美学的多功能交流平台&#xff0c;体现了上海这座城市的精神底色和对赛艇文化…

基于springboot实现人口老龄化社区服务与管理系统项目【项目源码+论文说明】计算机毕业设计

基于springboot实现人口老龄化社区服务与管理系统演示 摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了人口老龄化社区服务与管理平台的开发全过程。通过分析人口老龄化社区服务与管理平台方面的不足&#xff…

实习学习内容-帧同步

帧同步是一种在多人在线游戏&#xff08;尤其是实时策略游戏和战斗游戏&#xff09;中常见的网络同步技术&#xff0c;用于确保所有玩家的游戏状态完全一致。帧同步的目的是在所有参与的玩家之间提供一个统一的、无差异的游戏体验&#xff0c;即使他们分布在不同的地理位置。 …

网络编程(现在不重要)

目录 网络编程三要素与InetAddress类的使用 软件架构 面临的主要问题 网络编程三要素&#xff08;对应三个问题&#xff09; InetAddress的使用 TCP与UDP协议剖析与TCP编程案例&#xff08;了解&#xff09; TCP协议 UDP协议 例子 UDP、URL网络编程 URL&#xff1a;&…

后端自测帮助指南

问题&#xff1a; 前端反馈联调时间过长&#xff0c;原因是后端接口质量不高&#xff0c;联调时反复出问题&#xff0c;然后花时间去修改bug然后发布后前端才能调&#xff0c;如此一次至少也半个小时了。测试阶段&#xff0c;后端花太多时间配合&#xff0c;测试的冒烟测试往往…

《Linux C/C++服务器开发实践》之第7章 服务器模型设计

《Linux C/C服务器开发实践》之第7章 服务器模型设计 7.1 I/O模型7.1.1 基本概念7.1.2 同步和异步7.1.3 阻塞和非阻塞7.1.4 同步与异步和阻塞与非阻塞的关系7.1.5 采用socket I/O模型的原因7.1.6&#xff08;同步&#xff09;阻塞I/O模型7.1.7&#xff08;同步&#xff09;非阻…

一夜爆红的4款国产软件,却一度被大众误以为是外国人开发

在现今高度信息化的时代&#xff0c;计算机已经深深地渗透到了我们生活的每一个角落。 从日常的办公学习到娱乐休闲&#xff0c;几乎都离不开计算机技术的支持。而在这背后&#xff0c;软件作为计算机的灵魂&#xff0c;其发展历史可谓波澜壮阔。 中国软件产业经过多年的积累和…

node express 请求参数接收方式汇总

express 安装使用 express官网 express 是node.js 中写后端服务比较流行的框架。 安装express npm install -g express安装 express-generator 相当于vue的cli 用来快速生成express项目 npx express-generator生成项目mynode -e是使用ejs模版 express -e mynodeexpress生成器生…

Unity Android 2023 Release-Notes

&#x1f308;Unity Android 2023 Release-Notes 本文信息收集来自自动搜集工具&#x1f448; 版本更新内容2023.2.17Android: Fixed an issue where a black frame flashes when returning to Unity Game Activity from the home screen.(UUM-58966)2023.2.17Android: Fixed …

前端网络---http协议和https协议的区别

http协议和https的区别 1、http是超文本传输协议&#xff0c;信息是明文传输&#xff0c;https则是具有安全性的ssl加密传输协议。 2、http和https使用的端口不一样&#xff0c;http是80&#xff0c;https是443。 3、http的连接很简单&#xff0c;是无状态的&#xff08;可以…

L2-009抢红包

用结构体来存储变量&#xff0c;定义排序规则&#xff0c;对题目所讲的模拟一遍即可。没有什么很深入得内容 #include <bits/stdc.h> using namespace std;typedef struct {int num;double sum;int count; } Node; bool cmp(Node node1, Node node2) {if (node1.sum no…

2024电容笔专业对比评测:西圣、倍思、绿联哪款平替电容笔更好用?

在当今学习和工作环境中&#xff0c;iPad作为一种多功能的学习和生产力工具&#xff0c;受到越来越多人的青睐与需求。然而&#xff0c;要充分发挥iPad的功能&#xff0c;一个优质的电容笔是必不可少的配件之一。电容笔不仅可以帮助用户进行手写笔记、绘画创作&#xff0c;还能…

1373:鱼塘钓鱼(fishing)

【算法分析】 解法1&#xff1a;区间动规 该人只会从编号小的鱼塘走到编号大的鱼塘&#xff0c;不存在往回走的情况&#xff08;从编号大的鱼塘走到编号小的鱼塘&#xff09;。 如果他仅仅往回走但不在任何鱼塘停留&#xff0c;那么这与不往回走钓到的鱼的数量相同&#xff0…

新手做抖音小店,想要快速起店,抓住这两点很关键

大家好&#xff0c;我是电商笨笨熊 抖音小店一定是近几年来爆火的电商项目&#xff0c;凭借着直播电商的方式在短短几年内迅速崛起&#xff0c;成为现在人尽皆知的电商项目。 然而在抖店里&#xff0c;不少进入的玩家都是新手&#xff0c;甚至都是盲目入店&#xff0c;没有任…

【Unity】Feature has expired(H0041)

【背景】 在一台很久不用的电脑上更新了个人License&#xff0c;并导入了云项目&#xff0c;打开时却报错&#xff1a; 【分析】 网上查说要删缓存等等&#xff0c;试过都不行。重装Hub也不行。 这种环境类型的原因很难从信息入手定位错误。 所以我自己检查项目上有什么问题…

MATLAB 浮点数 转化为 定点数

a fi(v,s,w,f) 一个 fi 对象&#xff0c;其值为 v&#xff0c;符号性为 s&#xff0c;字长为 w&#xff0c;小数长度为 f。 AD9361 a fi(0.707,1,12,11)

Qt实现XYModem协议(二)

1 概述 XMODEM协议是一种使用拨号调制解调器的个人计算机通信中广泛使用的异步文件运输协议。这种协议以128字节块的形式传输数据&#xff0c;并且每个块都使用一个校验和过程来进行错误检测。使用循环冗余校验的与XMODEM相应的一种协议称为XMODEM-CRC。还有一种是XMODEM-1K&am…

在列表b是在列表a的首位(末尾)增加了‘x‘元素,要求分别输出列表a(原列表)和列表b

在列表b是在列表a的首位增加了0元素&#xff0c;要求分别输出列表a&#xff08;原列表&#xff09;和列表b 1.创建副本的形式实现 如果你想要在列表 b 中增加元素 0&#xff0c;而不影响原始列表 a&#xff0c;你需要创建 b 的一个副本&#xff0c;而不是让 b 直接指向 a。这…