循环队列

SqQueue.h

#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;

#define  MAX_QUEUE_SIZE 20
template<typename T>
class CCircleQueue
{
public:
	CCircleQueue(void);
	~CCircleQueue(void);
public:
	const int getQueueLength();
	bool enQueue(T element);
	bool deQueue(T& element);
	void show();
private:
	struct SData
	{
		T data[MAX_QUEUE_SIZE];
		int front;
		int rear;
	};

	SData m_queue;
};

template<typename T>
CCircleQueue<T>::CCircleQueue(void)
{
	m_queue.front = 0;
	m_queue.rear = 0;
}


template<typename T>
CCircleQueue<T>::~CCircleQueue(void)
{

}

template<typename T>
const int CCircleQueue<T>::getQueueLength()
{
	return ((m_queue.rear-m_queue.front) % MAX_QUEUE_SIZE);
}

template<typename T>
bool CCircleQueue<T>::enQueue(T element)
{
	if((m_queue.rear+1) % MAX_QUEUE_SIZE == m_queue.front) // is full
	{
		return false;
	}

	 m_queue.data[m_queue.rear] = element;

	 m_queue.rear = (m_queue.rear+1) % MAX_QUEUE_SIZE;

	 return true;
}

template<typename T>
bool CCircleQueue<T>::deQueue(T& element)
{
	if(m_queue.front == m_queue.rear) // empty
	{
		return false;
	}

	element = m_queue.data[m_queue.front];
	m_queue.front = (m_queue.front+1)%MAX_QUEUE_SIZE;

	return true;
}

template<typename T>
void CCircleQueue<T>::show()
{
	if(m_queue.front == m_queue.rear)
	{
		return;
	}

	T val;
	int queueLen = getQueueLength();
	int i=0;

	T temp[MAX_QUEUE_SIZE];

	while(deQueue(val) && queueLen)
	{
		cout<<val<<" ";
		queueLen --;
		temp[i] = val;
		i++;
	}
	cout<<endl;

	for (int j=0;j < i;j++)
	{
		enQueue(temp[j]);
	}

}

listQueue.h

#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;


template<typename T>
class CCircleLinkQueue
{
public:
	CCircleLinkQueue(void);
	~CCircleLinkQueue(void);
public:
	bool enQueue(T element);
	bool deQueue(T& element);
	void show();
	 int getQueueLen()
	{
		return m_nLen;
	}
private:
	struct SNode
	{
		struct SNode* next;
		T data;
	};

	struct SQueue
	{
		SNode* front;
		SNode* rear;
	};

	SQueue*  m_pQueue;
	int m_nLen;
};

template<typename T>
CCircleLinkQueue<T>::CCircleLinkQueue(void):m_pQueue(NULL),m_nLen(0)
{

}

template<typename T>
CCircleLinkQueue<T>::~CCircleLinkQueue(void)
{

}

template<typename T>
bool CCircleLinkQueue<T>::enQueue(T element)
{
	if(!m_pQueue)
	{
		m_pQueue = new SQueue;
		m_pQueue->front = new SNode;
		m_pQueue->front->next = NULL;
		m_pQueue->rear = m_pQueue->front;
	}

	SNode* s = new SNode;
	s->data = element;
	s->next = NULL;
	m_pQueue->rear->next = s;
	m_pQueue->rear = s;

	m_nLen++;
	return true;
}

template<typename T>
bool CCircleLinkQueue<T>::deQueue(T& element)
{
	if(m_pQueue->front == m_pQueue->rear) // empty
	{
		return false;
	}

	SNode* p = m_pQueue->front->next;
	element = p->data; // asign the value to element

	m_pQueue->front->next = p->next;
	if(m_pQueue->rear == p)// touch the rear
	{
		 m_pQueue->rear = m_pQueue->front;
	}
	delete p;

	m_nLen--;

	return true;
}

template<typename T>
void CCircleLinkQueue<T>::show()
{
	T temp[20000];
	T val ;
	int i =0;
	int nLen = m_nLen;

	cout<<endl<<"-----------------------------"<<endl;
	while(deQueue(val) && nLen)
	{
		cout<<val<<" ";
		temp[i] = val;
		i++;
		nLen--;
		
	}
	cout<<endl<<"----------------------------"<<endl;

	for (int j=0;j < i;j++)
	{
		enQueue(temp[j]);
	}
}

main.cpp

// dataStruct_Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "SqQueue.h"
#include "listQueue.h"

int _tmain(int argc, _TCHAR* argv[])
{
	CCircleQueue<int> myqueue;
	myqueue.enQueue(1);
	myqueue.enQueue(2);

	myqueue.show();

	myqueue.enQueue(3);
	myqueue.enQueue(4);

	myqueue.show();

	int val=0;
	myqueue.deQueue(val);
	cout<<val<<endl;


	cout<<"______________________"<<endl;

	CCircleLinkQueue<int> linkqueue;
	linkqueue.enQueue(1);
	linkqueue.enQueue(2);
	linkqueue.enQueue(3);
	linkqueue.show();

	linkqueue.enQueue(5);
	linkqueue.show();

	linkqueue.deQueue(val);
	cout<<val<<endl;
	linkqueue.show();

	system("pause");

	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值