// test_shared.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <memory>
#include <string>
#include <iostream>
typedef struct stage_tag {
int data_ready; /* Data present */
long data; /* Data to process */
struct stage_tag *next; /* Next stage */
} stage_t;
// 高效率的链表创建方式
stage_t* TestLinkCreateClever(int stages)
{
stage_t *head = NULL,*new_stage = NULL,*tail = NULL;
stage_t **link = &head; // 区别在这个指针地址变量上,它起到绑定新的stage的作用.
for(int i =0; i<stages;++i)
{
new_stage = (stage_t*)malloc(sizeof(stage_t));
new_stage->data_ready = 0;
new_stage->data = i;
*link = new_stage; // 把新的stage赋值给link指向的指针地址
link = &new_stage->next; // 绑定下一个的指针地址
}
tail = new_stage;
*link = NULL;
return head;
}
// 低效率的链表创建方式
stage_t* TestLinkCreateStupid(int stages)
{
stage_t *head = NULL,*new_stage = NULL,*tail = NULL;
for(int i =0; i<stages;++i)
{
new_stage = (stage_t*)malloc(sizeof(stage_t));
new_stage->data_ready = 0;
new_stage->data = i;
new_stage->next = NULL;
if(tail)
tail->next = new_stage;
else
head = new_stage;
tail = new_stage;
}
return head;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "=== TestLinkCreateClever ===" << std::endl;
auto first = TestLinkCreateClever(10);
while(first)
{
std::cout << "data: " << first->data << std::endl;
first = first->next;
}
std::cout << "=== TestLinkCreateStupid ===" << std::endl;
auto second = TestLinkCreateStupid(10);
while(second)
{
std::cout << "data: " << second->data << std::endl;
second = second->next;
}
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有