不用C++模版实现queue



//定义队列的节点结构  
typedef struct Data
{
	int data1;
	char data2;
	char *data3;

}T_Data;

typedef struct queue
{
	struct queue *next;
	T_Data elementData;

}T_queueNode;


class MyQueue
{
public:
	int Maxsize;
	int count;
	T_queueNode *Head;
	T_queueNode *Tail;

	MyQueue(int size = 10);
	int	 isEmpty();
	int  isFull();
	int  DeleteQueue();
	bool  InserQueue(int temp1, char temp2, char* temp3);
	T_queueNode*  getHeadElemt();
	int	 getSize();
	void Clear();
	void Display();   
};

#include "StdAfx.h"
#include "MyQueue.h"
#include "LogTool.h"

/************************************************************************/
/* 功  能:构造函数,给队列容量赋值 
返回值:无														*/
/************************************************************************/
MyQueue::MyQueue(int size)
{
	Maxsize = size;
	count = 0;
	Head = NULL;
	Tail = NULL;
}

  /************************************************************************/
  /*功  能:判断队列是否为空   
    返回值:队列为空,返回 1
            队列不为空,返回 0                                                   */
  /************************************************************************/
int MyQueue::isEmpty()
{
	if (0 == count)
	{
		return 1;
	}
	return 0;
}

/************************************************************************/
/*功 能:判断队列是否为满        
返回值:队列为满,返回 1
<span style="white-space:pre">	</span>队列不为满,返回 0                                                    */
/************************************************************************/
int MyQueue::isFull()
{
	if (Maxsize == count)
	{
		return 1;
	}
	return 0;
}

/************************************************************************/
/* 功  能:取队首元素    
返回值:队列为空,返回0,取队首元素失败
<span style="white-space:pre">	</span>队列不为空,队首元素  														*/
/************************************************************************/
T_queueNode* MyQueue::getHeadElemt()
{
	if (isEmpty())
	{
		return 0;  // 因为队列中的元素也是int型,这里用0返回不妥当,你自己用一个可以表示为空的数返回就可以了
	}
	return Head;
}

/************************************************************************/
/* 功能:进队
返回:队列已满,返回 0
      队列没满,进行入队,返回 1  														*/
/************************************************************************/
bool MyQueue::InserQueue(int temp1, char temp2, char *temp3)
{ 
	if (isFull())
	{
		return false;
	}
	else
	{
		T_queueNode *node;
		node = (T_queueNode *)new T_queueNode;
		node->elementData.data1 = temp1;
		node->elementData.data2 = temp2;
		node->elementData.data3 = temp3;

		node->next = NULL;

		if (!isEmpty())
		{
			Tail->next = node;
			Tail = node;
		}

		if (isEmpty())
		{
			Head = node;
			Tail = node;
		}
		count++;
		return true;
	}
}

/************************************************************************/
/* 功  能:出队
返回值:队列为空,返回 0,出队失败
<span style="white-space:pre">	</span>队列不为空,返回 1,出队成功														*/
/************************************************************************/
int MyQueue::DeleteQueue()
{        // 在队列不为空的情况下
	if (!isEmpty())
	{    // 队列中不止一个元素
		if (NULL != Head->next)
		{
			T_queueNode *node;
			node = Head;
			Head = Head->next;
			delete node;
		}
		// 队列中只有一个结点
		if (NULL == Head->next)
		{
// 			delete Head;
// 			Head = NULL;
// 			Tail = NULL;
		}
		count--;
		return 1;
	}
	// 队列为空直接返回 0
	return 0;       
}

//取得队列元素个数  
int MyQueue::getSize()  
{  
	int count(1);  

	T_queueNode *node = Head;  

	while (node != Tail)  
	{  
		node = node->next;  
		count++;  
	}  
	return count;  
} 

/************************************************************************/
/* 功  能:清空
返回值:队列不为空,清空操作,返回 1
<span style="white-space:pre">	</span>队列为空,清空失败,返回 0														*/
/************************************************************************/
void MyQueue::Clear()
{
	Head = NULL;
	Tail = NULL;
	count = 0;

}

/************************************************************************/
/* 功  能:显示队列元素
返回值:无      													*/
/************************************************************************/
void MyQueue::Display()
{
	if (!isEmpty())
	{
		T_queueNode *node;
		node = Head;
		while (NULL != node )
		{
			CLogTool::Instance()->WriteLogFile("%d, %c, %s", node->elementData.data1,  
				node->elementData.data2, node->elementData.data3);
			node = node->next;
		}
	}
}



                
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值