C++模板实现的单向队列

C++模板实现的单向队列的元素添加,元素删除,元素清除功能

//Queues.h

#ifndef DDXX_QUEUES_H
#define DDXX_QUEUES_H
#include <iostream>
using namespace std;

template<typename Type>
class Queues
{
public:
	Queues();
	~Queues();
public:
	struct Node
	{
		Type	e;
		Node*	next;
		
		Node()
		{
		}
		Node(Type _e)
		{
			e		= _e;
			next	= NULL;
		}
	};
public:
	bool add(Type e);
	bool erase();
	bool isEmpty();
	int  getLength();
	void print();
	void clear();
private:
	int		m_Length;
	Node*	m_head;
	Node*	m_end;
};

template<typename Type> Queues<Type>::Queues()
{
	m_head = new Node;
	m_end = m_head;
	m_head->next = NULL;
	m_Length = 0;
}

// here change Type to Types has no influence to "add" function
template<typename Types> bool Queues<Types>::add(Types e)
{
	Node* ptr = new Node(e);
	if( ptr == NULL )
	{
		cout<<"Allocate memory for new element failed"<<endl;
		return false;
	}
	m_end->next = ptr;
	m_end = ptr;

	m_Length++;
	return true;
}

template<typename Type> bool Queues<Type>::erase()
{
	if ( m_head->next == NULL )
	{
		cout<<"The queue is empty"<<endl;
		return false;
	}
	Node* ptr = m_head->next;
	m_head->next = ptr->next;
	delete ptr;
	ptr = NULL;

	m_Length--;
	return true;
}

template<typename Types> bool Queues<Types>::isEmpty()
{
	return m_head == m_end;
}
template<typename Type> int Queues<Type>::getLength()
{
	return m_Length;
}

template<typename Type> void Queues<Type>::print()
{
	Node* ptr = m_head;
	while(ptr->next != NULL)
	{
		ptr = ptr->next;
		cout<<"element value:"<<ptr->e<<endl;
	}
}

template<typename Type> void Queues<Type>::clear()
{
	Node* ptr = m_head->next;
	while(ptr != NULL)
	{
		ptr = ptr->next;
		delete m_head->next;
		m_head->next = ptr;
		m_Length--;
	}
	m_end = m_head;
}

template<typename Type> Queues<Type>::~Queues()
{
	Node* ptr = m_head->next;
	while(ptr != NULL)
	{
		ptr = ptr->next;
		delete m_head->next;
		m_head->next = ptr;
		m_Length--;
	}
	m_end = m_head;
	delete m_head;
	m_head = NULL;
	m_end = NULL;
}
#endif

// main.cpp

#include <iostream>
#include "Queues.h"
using namespace std;

void main()
{
	cout<<"*****************************test queue init***************************"<<endl;
	Queues<int> Que;
	cout<<"Queues is empty? :"<<Que.isEmpty()<<endl;
	cout<<"Que‘s length is:"<<Que.getLength()<<endl;

	cout<<"*****************************test queue add****************************"<<endl;
	Que.add(1);
	Que.add(2);
	Que.add(3);
	Que.add(4);
	Que.print();
	cout<<"Queues is empty? :"<<Que.isEmpty()<<endl;
	cout<<"Que‘s length is:"<<Que.getLength()<<endl;

	cout<<"*****************************test queue erase**************************"<<endl;
	Que.erase();
	Que.erase();
	Que.print();
	cout<<"Queues is empty? :"<<Que.isEmpty()<<endl;
	cout<<"Que‘s length is:"<<Que.getLength()<<endl;

	cout<<"*****************************test queue clear**************************"<<endl;
	Que.clear();
	Que.print();
	cout<<"Queues is empty? :"<<Que.isEmpty()<<endl;
	cout<<"Que‘s length is:"<<Que.getLength()<<endl;
}
程序的运行结果:





C++模板实现的单向队列,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。