【C++】模板类链表

C++模板类实现单链表 c++模板类实现单链表#include<iostream> using namespace std; #include<assert.h> #include<string> template<class T> struct LinkNode { LinkNode<T>* _next; T _data; LinkNode(const T& x) :_data(x 阅读详情

上一篇博客介绍了模板类顺序表,此篇博客主要是模板类链表的实现。

#pragma once
#include<iostream>
#include<string>
using namespace std;

template<typename T>
struct Node
{
public:
	Node(const T&x)
		:_data(x), _next(NULL), _prev(NULL)
	{}
	T _data;
	Node<T>*_next;
	Node<T>*_prev;
};


template<typename T>
class LinkList
{
	template<typename T>
	friend ostream&operator<<(ostream&os, const LinkList<T>&list);
public:
	LinkList()
		:_head(NULL), _tail(NULL), _sz(0)
	{}

	~LinkList()
	{
		Release();
	}
	LinkList(const LinkList<T>&list);   //拷贝构造
	void PushBack(T x);  //尾插
	void PopBack();   //尾删
	void PushFront(T x);  //头插
	void PopFront();  //头删
	Node<T>* Find(T x);      //查找指定元素的地址
	void Insert(Node<T>* node, T x); //指定位置插入指定数据
	void Remove(T x);     //删除指定元素
	void RemoveAll(T x);  //删除链表里出现的所有指定数据
	void Erase(Node<T>* node);      //指定位置删除
	void BubbleSort();           //冒泡排序
	int& GetSz()
	{
		return _sz;
	}
public:
	void Release()
	{
		Node<T>* cur = _head;
		while (cur)
		{
			Node<T>*tmp = cur;
			cur = cur->_next;
			delete tmp;
			tmp = NULL;
		}
		_head = NULL;
		_tail = NULL;
		_sz = 0;
	}
	Node<T>*_head;
	Node<T>*_tail;
	int _sz;
};
template<typename T>
ostream&operator<<(ostream&os, const LinkList<T>&list)
{
	Node<T>*cur = list._head;
	while (cur)
	{
		os << cur->_data << "-";
		cur = cur->_next;
	}
	cout << "end" << endl;
	return os;
}

template<typename T>
LinkList<T>::LinkList(const LinkList<T>&list)
{
	Node<T>* cur = list._head;
	while (cur)
	{
		PushBack(cur->_data);
		_sz++;
		cur = cur->_next;
	}
}
template<typename T>
void LinkList<T>::PushBack(T x)
{
	Node<T>* node = new Node<T>(x);
	if (NULL == _head)
	{
		_head = node;
		_tail = node;
	}
	else
	{
		_tail->_next = node;
		node->_prev = _tail;
		_tail = node;
	}
	_sz++;
}

template<typename T>
void LinkList<T>::PopBack()   //尾删
{
	if (NULL == _head)
	{
		throw string("no data in list");
	}
	else if (NULL == _head->_next)
	{
		Release();
	}
	else
	{
		_tail = _tail->_prev;
		delete _tail->_next;
		_tail->_next = NULL;
		_sz--;
	}
}

template<typename T>
void LinkList<T>::PushFront(T x)  //头插
{
	Node<T>*NewNode = new Node<T>(x);
	if (NULL == _head)
	{
		_head = NewNode;
		_tail = NewNode;
	}
	else
	{
		NewNode->_next = _head;
		_head->_prev = NewNode;
		_head = NewNode;
	}
	_sz++;
}

template<typename T>
void LinkList<T>::PopFront()  //头删
{
	if (NULL == _head)
	{
		throw string("no data in list");
	}
	else if (NULL == _head->_next)
	{
		Release();
	}
	else
	{
		_head = _head->_next;
		delete _head->_prev;
		_head->_prev = NULL;
		_sz--;
	}
}

template<typename T>
Node<T>* LinkList<T>::Find(T x)      //查找指定元素的地址
{
	Node<T>* cur = _head;
	while (cur)
	{
		if (cur->_data == x)
		{
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}

template<typename T>
void LinkList<T>::Insert(Node<T>* node, T x) //指定位置插入指定数据
{
	Node<T>* cur = _head;
	Node<T>* NewNode = new Node<T>(x);
	if (node == _head)
	{
		PushFront(x);
	}
	else
	{
		while (cur)
		{
			if (cur == node)
			{
				NewNode->_next = cur;
				NewNode->_prev = cur->_prev;
				cur->_prev = NewNode;
				NewNode->_prev->_next = NewNode;
				_sz++;
			}
			cur = cur->_next;
		}
	}
}

template<typename T>
void LinkList<T>::Remove(T x)     //删除指定元素
{
	Node<T>*cur = _head;
	if (_head->_data == x)
	{
		PopFront();
	}
	else
	{
		while (cur && (cur->_data != x))
		{
			cur = cur->_next;
		}
		if (cur == NULL)
		{
			cout << "no such data" << endl;
		}
		else
		{
			cur->_prev->_next = cur->_next;
			cur->_next->_prev = cur->_prev;
			cur->_next = NULL;
			cur->_prev = NULL;
			delete cur;
			_sz--;
		}
	}
}

template<typename T>
void LinkList<T>::RemoveAll(T x) //删除链表里出现的所有指定数据
{
	Node<T>*cur = _head;
	while (cur)
	{
		if ((cur->_data == x) && (cur->_next != NULL) && (cur->_prev != NULL))
		{
			Node<T> *del = cur;
			cur = cur->_next;
			del->_prev->_next = del->_next;
			del->_next->_prev = del->_prev;
			del->_next = NULL;
			del->_prev = NULL;
			delete del;
			_sz--;
		}
		else if ((cur->_data == x) && (cur->_next == NULL))
		{
			//PopBack();
			_tail = cur->_prev;
			cur->_prev = NULL;
			_tail->_next = NULL;
			delete cur;
			cur = NULL;
		}
		else if ((cur->_data == x) && (cur->_prev == NULL))
		{
			//PopFront();
			Node<T>*del = cur;
			cur = cur->_next;
			_head = _head->_next;
			_head->_prev = NULL;
			del->_next = NULL;
			delete del;
			del = NULL;
		}
		else
			cur = cur->_next;
	}
}

template<typename T>
void LinkList<T>::Erase(Node<T>* node)      //指定位置删除
{
	Node<T>* cur = _head;
	while (cur)
	{
		if ((cur == node) && (cur->_next != NULL) && (cur->_prev != NULL))
		{
			Node<T> *del = cur;
			cur = cur->_next;
			del->_prev->_next = del->_next;
			del->_next->_prev = del->_prev;
			del->_next = NULL;
			del->_prev = NULL;
			delete del;
			_sz--;
		}
		else if ((cur == node) && (cur->_next == NULL))
		{
			_tail = cur->_prev;
			cur->_prev = NULL;
			_tail->_next = NULL;
			delete cur;
			cur = NULL;
		}
		else if ((cur == node) && (cur->_prev == NULL))
		{
			//PopFront();
			Node<T>*del = cur;
			cur = cur->_next;
			_head = _head->_next;
			_head->_prev = NULL;
			del->_next = NULL;
			delete del;
			del = NULL;
		}
		else
			cur = cur->_next;
	}
}

template<typename T>
void LinkList<T>::BubbleSort()           //冒泡排序
{
	Node<T>* cur = _head;
	Node<T>* EndOk = NULL;
	while (cur->_next)
	{
		Node<T>*incur = _head;
		while (incur->_next != EndOk)
		{
			if (incur->_data > incur->_next->_data)
			{
				T tmp = incur->_data;
				incur->_data = incur->_next->_data;
				incur->_next->_data = tmp;
			}
			incur = incur->_next;
		}
		EndOk = incur;
		cur = cur->_next;
	}
}


void test1()
{
	LinkList<int>list;
	list.PushBack(1);
	list.PushBack(2);
	list.PushBack(3);
	list.PushBack(4);
	//LinkList<int>list1(list);
	cout << list << endl;
	list.PopBack();
	cout << list << endl;
	list.PopBack();
	cout << list << endl;
	list.PopBack();
	cout << list << endl;
	try
	{
		list.PopBack();
	}
	catch (string e)
	{
		cout << e << endl;
	}
	cout << list << endl;
	try
	{
		list.PopBack();
	}
	catch (string e)
	{
		cout << e << endl;
	}
	cout << list << endl;
}

void test2()
{
	LinkList<int> list;
	list.PushFront(1);
	list.PushFront(2);
	list.PushFront(3);
	cout << list << endl;
	try{ list.PopFront(); }
	catch (string e)
	{
		cout << e << endl;
	}
	cout << list << endl;
	try{ list.PopFront(); }
	catch (string e)
	{
		cout << e << endl;
	}
	cout << list << endl;
	try{ list.PopFront(); }
	catch (string e)
	{
		cout << e << endl;
	}
	cout << list << endl;
	try{ list.PopFront(); }
	catch (string e)
	{
		cout << e << endl;
	}
	cout << list << endl;
}

void test3()
{
	LinkList<int> list;
	list.PushBack(1);
	list.PushBack(2);
	list.PushBack(3);
	cout << list << endl;
	Node<int>* ret = list.Find(3);
	list.Insert(ret, 9);
	cout << list << endl;
	LinkList<string> list1;
	list1.PushBack("ajffas");
	list1.PushBack("adgadgagasdjga");
	cout << list1 << endl;
	LinkList<string> list2(list1);
	cout << list2 << endl;
}

void test4()
{
	LinkList<int> list;
	list.PushBack(2);
	list.PushBack(1);
	list.PushBack(2);
	list.PushBack(3);
	list.PushBack(2);
	list.PushBack(2);
	cout << list << endl;
	/*list.Remove(2);
	cout << list << endl;*/
	list.RemoveAll(2);
	cout << list << endl;
}

void test5()
{
	LinkList<int> list;
	list.PushBack(1);
	list.PushBack(2);
	list.PushBack(3);
	list.PushBack(4);
	cout << list << endl;
	/*Node<int>*ret = list.Find(2);
	list.Erase(ret);*/
	cout << list << endl;
}

void test6()
{
	LinkList<int> list;
	list.PushFront(1);
	list.PushFront(2);
	list.PushFront(3);
	list.PushFront(4);
	cout << list << endl;
	list.BubbleSort();
	cout << list << endl;
}
     当然,模板也有利弊,虽然它复用了代码,节省资源,可以更快的迭代开发,增强了代码的灵活性。但是模板让代码变得错综复杂,不易维护,编译代码时间变长。而且,当出现错误时,错误信息非常凌乱,亲自感受一下就能知道,每次发生一个错误,真的要好久才能找到错在哪里,不易定位错误。

C++基于链表队列模板类 链表模板类 阅读详情

相关推荐

C++中链表实现的队列模板类

链表中的节点通常由键值对组成,其中键用于存储数据,值指向下一个节点的地址。这种数据结构的特点是动态内存分配,即在运行时根据需要增加或删除节点,而不必事先知道数据的大小。队列是一种先进先出(First-In-First-Out,简称FIFO)的数据结构,它有两个主要的操作:入队(enqueue)和出队(dequeue)。新元素总是被添加到队列的末尾,而元素的移除总是从队列的前端开始。队列的这种特性使得它特别适合用于处理按顺序到达的请求。队列模板类的声明包括类名、构造函数、析构函数以及成员函数的声明。

weixin_33812391的博客 872

c++单链表类模板及其可视化界面

单链表是一种常见的数据结构,用于存储和组织数据。它由一个个节点组成,每个节点包含两部分:数据域和指针域。数据域存储节点中实际的数据,可以是任意类型的数据,例如整数、字符、对象等。指针域则用于指向下一个节点的地址,将多个节点按顺序连接起来形成链表。链式存储:单链表使用指针链接节点,而不需要连续的内存空间。这使得单链表能够灵活地插入和删除节点,不受固定大小的限制。动态大小:单链表的大小可以根据需要动态增长或缩小,不像数组需要预先指定大小并占用连续的内存空间。

chen666946的博客 1551

Python 使用Pygame库实现足球游戏:创建一个球员和一个足球,并使玩家能够控制球员的移动。游戏将包括碰撞检测、简单的物理模拟,以及基本的用户输入处理

本文将介绍如何使用Python和Pygame库创建一个简单的足球游戏。我们将搭建一个基本的游戏环境,创建一个球员和一个足球,并使玩家能够控制球员的移动。游戏将包括碰撞检测、简单的物理模拟,以及基本的用户输入处理。这个简单的足球游戏展示了Pygame的一些基本功能。虽然这是一个简单的游戏,但它包含了创建游戏循环、用户输入处理、物理模拟、碰撞检测等重要概念。

程序熊的博客 743

c++学习:类模板+万能版链表实战

实战 设计一个单向链表类模板 --万能版链表。如果还要比较其他不同数据的类,那需要很多个类。思考类模板能不能作为基类被继承。设计一个任意数据类型的数组模板。解决方法 创建类模板。

weixin_59669309的博客 833

实测对比:DINOv2、CLIP、ResNet在图像相似度任务上谁更强?(含Python代码与避坑指南)

本文通过实测对比DINOv2、CLIP和ResNet在图像相似度任务中的表现,提供Python代码实现与避坑指南。实验结果显示,DINOv2在精度上表现最佳,CLIP在多模态场景中优势明显,而ResNet则在计算效率上更优。文章还针对不同业务场景给出了具体选型建议,帮助开发者在图像搜索和版权检测等项目中做出更明智的技术决策。

weixin_30588381的博客 286

保姆级教程:手把手教你用RKISP TURN为RV1106生成和配置IQfile(附AIQ版本匹配避坑指南)

本文提供RV1106 ISP调试的完整指南,详细讲解如何使用RKISP TURN工具生成和配置IQfile,包括环境搭建、参数调试、设备树配置及常见问题解决方案。特别强调AIQ版本匹配的重要性,帮助开发者避免常见陷阱,提升图像信号处理效率。

weixin_42520796的博客 496

数据结构之单项链表

//节点类的创建//#ifndef NODE_H_#define NODE_H_#include #include template class LinkList;templateclass Node{public:friend class LinkList;public:T value;Node* next;public://Node(NULL):next(NULL){NULL;};Node()

songlipeng的专栏 816

SPL06气压温度传感器SPI驱动与补偿算法详解

气压传感器与温度传感器是嵌入式环境感知系统的核心组件,其测量精度高度依赖于MEMS芯片的校准参数与非线性补偿机制。SPL06作为集成式高精度数字传感器,采用24位Σ-Δ ADC与片内补偿引擎,需结合唯一出厂校准系数(即‘芯片指纹’)进行多阶数学修正,才能将原始ADC值转化为符合计量要求的帕斯卡(Pa)与摄氏度(℃)物理量。该过程涵盖SPI通信协议解析、寄存器时序控制、大端数据拼接、二进制补码处理及浮点/定点混合运算等典型嵌入式工程实践。广泛应用于无人机高度计、智能穿戴海拔监测与IoT气象终端中。

weixin_34640289的博客 143

数据结构——链表——模板类实现双向链表——先完成再完美——持续更

链表:概念,实现,《数据结构》这里实现是基于模板的 C++语言基础,指针,引用。模板。《C++Primer》有些进阶用法放在语言学习的目录 LeetCode应用,会更新在LeetCode150,目前这个系列先暂停,数据结构更新一期,再更对应的题目解题。 新手村入门,可以根据下面实现的过程中,忘记的知识点,再回头看。

qq_36914923的博客 1305

博通PEX89000 PCIe 5.0交换芯片:如何为AI集群构建高效GPU互联架构

本文深入解析了博通PEX89000 PCIe 5.0交换芯片如何为AI集群构建高效的GPU互联架构。该芯片通过提供高带宽、低延迟的PCIe 5.0交换能力,打破传统GPU通信的带宽瓶颈,并支持多主机共享、资源池化及软件定义拓扑,从而显著提升大规模AI训练与推理任务的效率与资源利用率。

weixin_29214559的博客 412

Windows Server 2016远程桌面服务配置和授权激活

Server 2016默认远程桌面连接数是2个用户,如果多余两个用户进行远程桌面连接时,系统就会提示超过连接数,可以通过添加远程桌面授权解决:1、添加远程桌面授权服务第一步:服务器管理 - 添加角色和功能打开添加角色和功能向导窗口,选择基于角色或给予功能安装:第二步:添加远程桌面会话主机和远程桌面授权功能:以上配置完成后即可使用多于2用户同时登陆,但使用期限为120天,再次登陆会有如下提示:2、添...

mahailiang的专栏 6万+

链表类模板

每次测试输出二行.第1行输出返回操作获得的数据(如出错则输出error),第2行输出所有操作后链表全部结点的数据.数据类型(I:int, D:double, S:string) 数据个数n 数据1 数据2 ... 数据n。//将第n个节点的数据改成a。//在第n个结点后加。//返回第n个结点的数据。插入节点号(0表示插在第1个结点前面) 数据。第一次自己手搓链表成功。

qmsdeqmy111的博客 185

外部PLC触发VisionMaster多流程运行

上一讲,介绍了海康VisionMaster与西门子PLC1200进行S7通信。在前一讲的基础上,讲解如何通过PLC触发,实现VisionMaster的多流程运行。

haier123888的博客 4654

C++ 模板类链表

运行环境:CodeBlocks 本程序使用模板类创建链表,并实现添加数据,删除数据,反转链表,连接两链表功能。 主程序 main.cpp //Written by Xuebi //本程序使用模板类创建链表,并实现添加数据,删除数据,反转链表,连接两链表功能 #include &amp;amp;amp;lt;iostream&amp;amp;amp;gt; #include &amp;amp;quot;List.h&amp;amp;quot; using name

God_Xuebi的博客 5529

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++模板类实现链表

注意 链表中要包含Node的结构体,用来把节点放进去 只有出现Node就要加<T> ,不然会报错 有类模板的重新编译记得清空再重新生成 清除链表时使用的循环清空头结点的思想需要记得(C语言中一样循环调用清除0号元素也可以) Debug和Release编译不同,一步一步执行时顺序不一样,不知道为啥了,不过不重要,结果一样 自己写的清空函数 template <typename ...

allensu374125056的博客 1126

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

C++ 简单实现链表模板类

目的在于熟悉 C++ 实现链表类、熟悉模板类 #include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; using namespace std; //前置申明模板类 template &lt;class T&gt; class List; //定义节点模板类 template &lt;cl...

m0_37579906的博客 2915

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

C++单向链表模板类

#include <iostream> #include <stdio.h> using namespace std; template<typename T> class Node{ public: T data; Node<T>* next; Node<T>* last; Node(T d=0) {data=d;cout<<"Node Constructor run"<<endl;} .

y0205yang的博客 509

C++ 模板类 单链表

C++作业,用模板类写链表,完成了对基本的单链表的插入,删除等。 #include <iostream> using namespace std; template<typename T> struct Node { T data; Node<T> *next; }; template<typename T> class LinkedLis...

Ich/liebe/dich的编程せかい 396

C++ 利用模板类链表实现栈结构

C++ 利用模板类链表实现栈结构 运行环境: Dev-c++ 5.11 日期:2018.10.11 主程序 main.cpp //Written by Xuebi //本程序利用链表实现栈结构,实现Pop,Push,Top基本功能 //链表采用前插法构建,不存在空结点 #include &lt;iostream&gt; using namespace std; template &lt;cla...

God_Xuebi的博客 1205

链表C++模板类封装实现

链表的封装+泛型编程+抽象数据类型测试

WSTONECH的博客 893

c++队列链表模板类

/* *队列链表模板类 */ #include #include using namespace std; template class Node{ private: Node *next; //指向后继的节点的指针 public: T data; //数据域 Node(const T &item=0,Node *ptrnext=NULL);

u013303425的专栏 1013

封装一个链表模板类 c++ linux

封装一个链表模板类 c++ linux

本人喜欢编程(c/c++/嵌入式/单片机硬件制作)、听音乐、偶尔看电影、看书、爱研究、有好奇心 446

C++ 单向链表模板类

模板编程方便只改变数据类型、而不改变基本操作的情况,STL基本都是基于模板进行编程,因此这里尝试修改面向int型的单向链表为模板,适用于其他类型的链表。 头文件 #pragma once #include&amp;lt;iostream&amp;gt; using namespace std; template&amp;lt;typename T&amp;gt; struct Node { T value; Node* ne...

weixin_42333471的博客 638

C++实现双链表(使用模板类)

C++实现双链表(使用模板类)

weixin_56493138的博客 1254
上一篇: 【C++】模板类顺序表
下一篇: 【操作系统】C语言模拟操作系统优先数调度算法
pointer_y
博客等级 码龄11年 157粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值