c++ 链表 详解 for应用

适合非预知长度场景使用;很方便。 

struct Linked {
	Linked *next;
	char deviceId[64];
};

int LinkedJson(cJSON* json, Linked *&lind)
{
	cJSON *list = cJSON_GetObjectItem(json, "list"); 
	cJSON *node = list->child;
	for (; node != NULL; node = node->next)
	{
		cJSON *item = cJSON_GetObjectItem(node, "deviceId"); 
		if (item && item->valuestring)
			memcpy(lind->deviceId, item->valuestring, strlen(item->valuestring));
		else
			return register1;

		lind->next = new Linked;//下一个链表
		lind = lind->next;//lind赋值为lind->next的地址
	}

	return 0;
}

int main(){	
    Linked *title = NULL;//表头地址
    Linked *lind=new Linked;//表头对象
    title = lind;

    cJSON* json = cJSON_Parse("{json}"); //json数据

    err= LinkedJson(json,lind);

	for (; title ->next != NULL;)
	{
		printf(title ->deviceId);
		Linked *temp = title ->next;
		delete title ,title =NULL;
		title = temp;
	}
	delete title ,title =NULL;

    return 0;
}

 

你可能感兴趣的:(技术,c++,链表,循环添加,循环添加链表)