VC队列操作

本文以一个简单的入队出队实现队列操作。


首先,新建一个对话框应用程序


然后,添加入队出队相关变量

#define maxsize 10
private:
	bool Ouput(char &Ovalue);
	void Input(char Ivalue);
	bool OutQueue(int &value);
	void InQueue(int value);
	void InitUi();

	int buffer[maxsize];
	int m_nCount;
	int m_nWt;
	int m_nRd;
	int m_nIndex;
	char letter[26];

处理程序

void CQueueDemoDlg::OnTimer(UINT nIDEvent) 
{
	if(200 == nIDEvent)
	{
	/*	InQueue(m_nIndex);
		m_nIndex++;
		if(m_nIndex>=10)
		{
			m_nIndex = 0;
		}*/

		Input(letter[m_nIndex]);
		m_nIndex++;
		if(m_nIndex>=26)
		{
			m_nIndex = 0;
		}
	}
	else
	{
	/*	int a = 0;
		if(OutQueue(a))
		{
			CString str;
			str = "";
			str.Format("%d",a);
			m_sShow = m_sShow + str;
		}*/

		char value = 0;
		if(Ouput(value))
		{
			m_sShow = m_sShow + CString(value);
		}
	}

	UpdateData(0);
	
	CDialog::OnTimer(nIDEvent);
}

void CQueueDemoDlg::InitUi()
{
	m_nIndex = 0;
	m_nWt = 0;
	m_nRd = 0;
	m_nCount = 0;

	letter[0] = 'a';
	letter[1] = 'b';
	letter[2] = 'c';
	letter[3] = 'd';
	letter[4] = 'e';
	letter[5] = 'f';
	letter[6] = 'g';
	letter[7] = 'h';
	letter[8] = 'i';
	letter[9] = 'j';
	letter[10] = 'k';
	letter[11] = 'l';
	letter[12] = 'm';
	letter[13] = 'n';
	letter[14] = 'o';
	letter[15] = 'p';
	letter[16] = 'q';
	letter[17] = 'r';
	letter[18] = 's';
	letter[19] = 't';
	letter[20] = 'u';
	letter[21] = 'v';
	letter[22] = 'w';
	letter[23] = 'x';
	letter[24] = 'y';
	letter[25] = 'z';

	SetTimer(500,500,NULL);
	SetTimer(200,200,NULL);
}

void CQueueDemoDlg::InQueue(int value)
{
	buffer[m_nWt] = value;
	m_nWt++;
	if(m_nWt>=maxsize)
	{
		m_nWt = 0;
	}
	m_nCount++;
	if(m_nCount>maxsize)
	{
		m_nCount=maxsize;
		m_nRd++;
		if(m_nRd>=maxsize)
		{
			m_nRd = 0;
		}
	}
}

bool CQueueDemoDlg::OutQueue(int &value)
{
	if(m_nCount <= 0)
	{
		return false;
	}
	value = buffer[m_nRd];
	m_nRd++;
	if(m_nRd>=maxsize)
	{
		m_nRd = 0;
	}
	m_nCount--;
	return true;
}

void CQueueDemoDlg::Input(char Ivalue)
{
	buffer[m_nWt] = Ivalue;
	m_nWt++;
	if(m_nWt>=maxsize)
	{
		m_nWt = 0;
	}
	m_nCount++;
	if(m_nCount>maxsize)
	{
		m_nCount = maxsize;
		m_nRd++;
		if(m_nRd>=maxsize)
		{
			m_nRd = 0;
		}
	}
}

bool CQueueDemoDlg::Ouput(char &Ovalue)
{
	if(m_nCount <= 0)
	{
		return false;
	}
	else
	{
		Ovalue = buffer[m_nRd];
		m_nRd++;
		if(m_nRd>=maxsize)
		{
			m_nRd = 0;
		}
		m_nCount--;
		return true;
	}
}

运行效果


VC队列操作_第1张图片

源码下载


你可能感兴趣的:(操作,队列,VC)