双链表
#include<list>
void mainxxx1()
{
list<int> mylist{ 1,2,3,4,5 };
mylist.push_back(10); //尾插
mylist.push_front(22);//头插
mylist.pop_back(); //删除尾部
mylist.pop_front();//删除头部
mylist.reverse(); //反转
mylist.sort(); //排序
int a[10]{ 1,2,3,4,5,6,7,8,9,10 };
mylist.assign(a,a+5); //处理填充
//mylist.assign(3, 5); //重新初始化链表 3个5
for (auto ib = mylist.begin(), ie = mylist.end();ib != ie;ib++) //顺序输出
{
if (*ib == 3)
{
mylist.insert(ib,123); //插入
}
if (*ib == 5)
{
mylist.erase(ib); //删除
break;
}
cout << *ib << endl;
}
for (auto rb = mylist.rbegin(), re = mylist.rend();rb != re;rb++) //逆序输出
{
if (*rb == 3)
{
*rb = 40;
}
//cout << *rb << endl;
}
for (auto i : mylist)
{
cout << i << endl;
}
cout << "第一个" << mylist.front() << endl;
cout << "最后一个" << mylist.back() << endl;
cout << "个数" << mylist.size() << endl;
mylist.clear(); //清空链表
list<int> mylist1{ 1,2,10,4,5 };
list<int> mylist2{ 8,5,7,9,4 };
mylist1.sort();
mylist2.sort();
//mylist1.merge(mylist2); //归并排序
mylist2.swap(mylist1); //交换
for (auto i : mylist1)
{
cout << i << endl;
}
cout << "=========================" << endl;
for (auto i : mylist2)
{
cout << i << endl;
}
list<int> mylist01{ 1,2,10,4,5 };
list<int> mylist02{ 8,5,7,9,4 };
list<int> mylist03{ 5,7,9,4 };
list< list<int> > mylistx{mylist01,mylist02,mylist03}; //链表的每一个元素都是链表
for (auto i : mylistx)
{
for (auto j : i)
{
cout << j << " ";
}
cout << endl;
}
cin.get();
}
双链表管理一个类的对象
//类 函数是共享的 数据是每个对象私有的
//空指针 调用成员函数 没有访问数据 可以调用 访问数据 不可以
class myclass; //类的声明
struct info
{
myclass*p; //指针 内存首地址
int n; //代表多少个对象
};
list<info> myclasslist; //双链表数据结构,管理一个类的所有对象
class myclass
{
public:
//int i = 0;
void show()
{
cout << " messsgebox" << endl;
}
myclass()
{
cout << "myclass()" << endl;
}
~myclass()
{
cout << "x`myclass()" << endl;
}
void* operator new(size_t size)
{
cout << "new:"<<size << endl;
void*p = malloc(size); //多个元素
if (p != nullptr)
{
info infonow;
infonow.p = (myclass*)p; //分配了 就记录一下
//infonow.n = 1; //记录地址与个数
if (size / sizeof(myclass) == 1)
{
infonow.n = 1;
}
else
{
infonow.n = (size - 4) / sizeof(myclass);
}
myclasslist.push_back(infonow); //插入链表
return p;
}
else
{
return nullptr;
}
}
void operator delete(void*p)
{
//双链表检索内存 存在删除 不存在不管
for (auto ib = myclasslist.begin(), ie = myclasslist.end();ib != ie;ib++)
{
if (p == (*ib).p)
{
myclasslist.erase(ib); //删除
free(p);
break;
}
}
}
void* operator new[](size_t size)
{
cout <<"new[]:"<< size << endl;
return operator new(size); //回调用new
}
void operator delete[](void*p)
{
return operator delete(p); //回调delete
}
private:
};
void showmem()
{
//显示内存
for (auto i : myclasslist)
{
cout << "内存地址:" << i.p << " " << "个数:" << i.n << endl;
}
}
//访问所有的对象
void callallobject()
{
for (auto i : myclasslist)
{
if (i.n == 1)
{
i.p->show();
}
else
{
//对象数组的第一个地址是数组名
for (int j = 0;j < i.n;j++)
{
(i.p[j + 1]).show();// 跳过对象数组名开头的4字节 (相当于跳过数组名)
}
}
}
}
void mainxxx10()
{
//cout << sizeof(myclass) << endl;
myclass*p1 = new myclass;
//myclass*p2 = new myclass;
//myclass*p3 = new myclass;
//myclass*p4 = new myclass;
//delete p1;
//delete p1;
myclass*pa1 = new myclass[5];
callallobject();
showmem();
delete[]pa1;
cin.get();
}
对象数组
//对象数组在全局 没有数组名的概念
//对象数组在全局 没有数组名的概念
void* operator new(size_t size)
{
cout << "::new:" << size << endl;
void*p = malloc(size);
return p;
}
void* operator new[](size_t size)
{
cout <<"::new[]:"<< size << endl;
return operator new(size);
}
class go
{
public:
int num;
void* operator new(size_t size)
{
cout << size << endl;
void*p = malloc(size);
return p;
}
void* operator new[](size_t size)
{
cout << "new[]:" << size << endl;
return operator new(size);
}
};
void main()
{
go*p1 = new go;
go*p = new go[10]; //44字节
cin.get();
}
网友评论