[C++]链表 智能指针实现一个普通链表

// 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;

}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值