// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <stdio.h>
#include<cstdlib>
using namespace std;
struct data_
{
int number;
std::string name;
std::string sex;
};
struct listPoint
{
data_ information;
std::shared_ptr<listPoint> next;
std::shared_ptr<listPoint> last;
};
std::shared_ptr<listPoint> create_normal_list(int n )
{
std::shared_ptr<listPoint> head = std::make_shared<listPoint>();
std::shared_ptr<listPoint> end;
end = head;
for (auto i = 0; i < n; i++)
{
std::shared_ptr<listPoint> normal = std::make_shared<listPoint>();
normal->information.number = i;
normal->information.name = "z";
normal->information.sex = "male";
end->next = normal;
normal->last = end;
end = normal;
}
end->next = NULL;
head->last = NULL;
return head;
}
//typedef struct data_
//{
// int number;
// string name;
// string sex;
//}data;
//
//typedef struct listpoint
//{
// data_* information;
// listpoint* next;
// listpoint* last;
//}listpoint;
//
//
//listpoint* create_normal_list(int n) /*链表每一个节点都是指向 listpoint结构的指针,所以返回值是listpoint *,n是指创建的链表的节点数目*/
//{
// listpoint* head, * normal, * end;/*创建头节点,其他节点,和尾节点*/
// head = new listpoint();
// head->information = new data_();
// /*分配内存*/
// end = head;/*最开始最后一个节点就是头节点,注意因为通过指针可以直接对地址上的东西进行操作,此时end和head指向同一个地址,对end所指向地址进行操作,等同于对head地址所做的操作*/
// for (int i = 0; i < n; i++)
// {
// normal = new listpoint();
// normal->information = new data_();
// /*给新节点分配内存*/
// normal->information->number = 5;
// normal->information->name = "z";
// normal->information->sex = "male";
// /* 往新节点存入数据,注意我们只给后面的节点存入数据,head不存数据*/
// end->next = normal;/*往end后增添新节点*/
// normal->last = end;/*新节点的上一个节点就是end*/
// end = normal;/*最后一个节点变成新节点*/
// }
// end->next = NULL;/*链表的最后指向一个新地址*/
// head->last = NULL;/*链表最开始的节点没有上一个节点*/
// return head;
//}
int main()
{
//listpoint* test = create_normal_list(5);
//std::cout << test->information->number<<"\n";
//std::cout << test->information->number << test->next->information->number<< test->next->next->information->number << test->next->next->next->information->number;
//
//
std::shared_ptr<listPoint> test = create_normal_list(5);
std::cout << test->next->information.number << test->next->next->information.number << test->next->next->next->information.number << test->next->next->next->next->information.number;
}
[C++]链表 智能指针实现一个普通链表
最新推荐文章于 2024-01-26 21:56:30 发布