单链表模板类

虚基类

#pragma once

template <class dataType>

class list
{
public:
	virtual void empty() = 0;
	virtual int getlength() = 0;
	virtual void insert(int i, const dataType& x) = 0;
	virtual void remove(int i) = 0;
	virtual int search(const dataType& x) = 0;
	virtual dataType visit(int i) = 0;
	virtual void traverse() = 0;
};

链表类

#pragma once
#include "list.h"

template <class  dataType>
class node_list : public list<dataType>
{
public:
	node_list();
	node_list(const dataType x);
	node_list(int n, const dataType x);
	~node_list();

	void empty();
	int getlength();
	void insert(int i, const dataType& x);
	void remove(int i);
	int search(const dataType& x);
	dataType visit(int i);
	void traverse();

	struct node
	{
		dataType data;
		node *next;
		node() : next(nullptr) {};
		node(dataType x) :data(x), next(nullptr) {};
	};

	node *head;
	node *tail;
	int current_length;
};

  

#include "pch.h"
#include "node_list.h"

template <class dataType>
node_list<dataType>::node_list()
{
	head = new node;
	tail = new node;
	if ((head == nullptr) || (tail == nullptr))
	{
		std::cout << "动态申请头节点内存失败" << std::endl;
		return;
	}
	head->next = tail;
	current_length = 0;
}

template <class dataType>
node_list<dataType>::node_list(const dataType x) : node_list()
{
	insert(1, x);
}

template <class dataType>
node_list<dataType>::node_list(int n, const dataType x) : node_list()
{
	for (int i = 0; i < n; ++i)
	{
		insert(i, x);
	}
}

template <class dataType>
node_list<dataType>::~node_list()
{
	empty();
	delete head;
	head = nullptr;
	delete tail;
	tail = nullptr;
}

template <class dataType>
void node_list<dataType>::empty()
{
	node *nowPos = head->next;
	while (nowPos != tail)
	{
		node *tmp = nowPos;
		nowPos = nowPos->next;
		delete tmp;
	}
	head->next = tail;
	current_length = 0;
}

template <class dataType>
int node_list<dataType>::getlength()
{
	return current_length;
}

template <class dataType>
void node_list<dataType>::insert(int i, const dataType& x)
{
	if (i < 0 || i > current_length)
	{
		return;
	}

	node *add = new node(x);
	if (i == 0)
	{
		add->next = head->next;
		head->next = add;
	}
	else
	{
		node *nowPos = head->next;
		while (--i)
		{
			nowPos = nowPos->next;
		}

		add->next = nowPos->next;
		nowPos->next = add;
	}
	++current_length;
}

template <class dataType>
void node_list<dataType>::remove(int i)
{
	if (i < 0 || i > current_length)
	{
		return;
	}

	if (i == 0)
	{
		node *tmp = head->next->next;
		delete head->next;
		head->next = tmp;
	}
	else
	{
		node *nowPos = head->next;
		while (--i)
		{
			nowPos = nowPos->next;
		}
		node *tmp = nowPos->next->next;
		delete nowPos->next;
		nowPos->next = tmp;
	}
	--current_length;
}

template <class dataType>
int node_list<dataType>::search(const dataType& x)
{
	node *nowPos = head;
	int i;
	for (i = 0; i < current_length; ++i)
	{
		nowPos = nowPos->next;
		if (nowPos->data == const_cast<dataType&>(x)) break;
	}
	if (i == current_length)
	{
		return -1;
	}
	return i;
}

template <class dataType>
dataType node_list<dataType>::visit(int i)
{
	if (i < 0 || i >= current_length)
	{
		throw 0;
	}

	node *nowPos = head->next;
	while (i--)
	{
		nowPos = nowPos->next;
	}
	return nowPos->data;
}

template <class dataType>
void node_list<dataType>::traverse()
{
	if (current_length == 0)
	{
		return;
	}
	node *nowPos = head->next;
	nowPos = nowPos->next;
	while (nowPos != tail)
	{
		nowPos = nowPos->next;
	}
	return;
}

自定义类(重载了输出和赋值运算符)

#pragma once
#include <string>
#include <iostream>


class server
{
private:
	int m_iPort;
	int m_iCur_Connect;
	int m_iMin_Connections;
	int m_iMax_Connections;
	std::string m_strIP;

public:
	explicit server();
	virtual~server();


public:
	int set_port(int iport)
	{
		m_iPort = iport;
		return 0;
	}

	int set_connections(int iconnections)
	{
		m_iMin_Connections = iconnections;
		return 0;
	}

	int set_max_connections(int max_connections)
	{
		m_iMax_Connections = max_connections;
		return 0;
	}

	int set_IP(std::string strIP)
	{
		m_strIP = strIP;
		return 0;
	}

	int getType()
	{
		return m_iType;
	}

	int getPort()
	{
		return m_iPort;
	}

	int getConnections()
	{
		return m_iMin_Connections;
	}

	int getMax_Connections()
	{
		return m_iMax_Connections;
	}

	std::string getIP()
	{
		return m_strIP;
	}

	int get_connection_count()
	{
		return m_iCur_Connect;
	}

	friend std::ostream& operator<<(std::ostream &os, const server &data)
	{
		os << "IP:"  << const_cast<server&>(data).getIP() << std::endl << "Prot:" << const_cast<server&>(data).getPort() << std::endl << std::endl << "Mix_connections:" << const_cast<server&>(data).getConnections() << std::endl << "Max_connection:" << const_cast<server&>(data).getMax_Connections() << std::endl << "Current_connection:" << const_cast<server&>(data).get_connection_count();
		return os;
	}

	friend bool operator==(server &data1, const server &data2)
	{
		if ((data1.getIP() == const_cast<server&>(data2).getIP()) && (data1.getPort() == const_cast<server&>(data2).getPort()) &&  (data1.getConnections() == const_cast<server&>(data2).getConnections()) && (data1.getMax_Connections() == const_cast<server&>(data2).getMax_Connections()))
			return true;
		else
			return false;
	}
};

  

#include "pch.h"
#include "server.h"


server::server()
{
	m_iPort = 1000;
	m_iMin_Connections = 5;
	m_iMax_Connections = 10;
	m_strIP = "192.168.5.127";
}


server::~server()
{
}

  测试(模板类的头文件和实现文件都要引入)

#include "pch.h"
#include <iostream>
#include "node_list.h"
#include "node_list.cpp"
#include "server.h"

int main()
{
	server ser;
	std::cout << ser << std::endl;
	remote_server ser2;
	ser2 = ser;
	std::cout << ser2 << std::endl;
	node_list<server> *list = new node_list<server>;
	list->insert(0, ser);
	list->insert(1, ser2);
	server ser3 = list->visit(0);

	getchar();
}

  

posted @ 2019-08-29 11:27  N_zero  阅读(246)  评论(0)    收藏  举报