您的位置:首页 > 其它

自己写的一个简单的队列类

2012-07-05 14:10 411 查看
1 #pragma once

3 #include<stdio.h>
4 #include<stdlib.h>
5 #include<iostream>
6
7 class CStack
8 {
9 public:
10 CStack();
11 ~CStack();
12 public:
13 typedef struct QUEUE
14 {
15 int nHead; //队头
16 int nTail; //队尾
17 double* dbData; //数据
18 };
19
20 QUEUE m_queue;
21
22 int m_nQueueSize;
23
24 public:
25 bool SetFree();
26 bool SetArraySize(int nSize);
27 bool PutValue(double dbValue);
28 bool PopValue();
29 bool IsEmpty();
30 bool IsFull();
31
32 int Size();
33
34 double GetValue(int nPos);
35 };

//实现文件

#include "stack.h"

CStack::CStack()
{
m_queue.nHead = 0;
m_queue.nTail = -1;
}

CStack::~CStack()
{
}

bool CStack::SetFree()
{
if(IsEmpty())
{
AfxMessageBox(_T("队列已空!"));
return false;
}
else
{
m_queue.nHead = 0;
m_queue.nTail = -1;
delete m_queue.dbData;
}

return true;
}

bool CStack::SetArraySize(int nSize)
{
m_queue.nHead = 0;
m_queue.nTail = -1;
m_queue.dbData = new double[nSize];

m_nQueueSize = nSize;

return true;
}

bool CStack::PutValue(double dbValue)
{
if(IsFull())
{
for(int i = 0; i < m_nQueueSize -1; i++)
{
m_queue.dbData[i +1] = m_queue.dbData[i];
}
m_queue.dbData[0] = dbValue;
}
else if(IsEmpty())
{
m_queue.nTail++;
m_queue.dbData[0] = dbValue;
}
else
{
m_queue.nHead++;
m_queue.dbData[m_queue.nHead] = dbValue;
}

return true;
}

bool CStack::PopValue()
{
if(IsEmpty())
{
AfxMessageBox(_T("队列已经为空!"));
return false;
}
else
{
m_queue.dbData[m_queue.nHead] = 0;
m_queue.nHead--;
}

return true;
}

bool CStack::IsEmpty()
{
if(m_queue.nTail == -1)
return true;
else
return false;
}

bool CStack::IsFull()
{
if(m_queue.nHead - m_queue.nTail >= m_nQueueSize - 1)
return true;
else
return false;
}

double CStack::GetValue(int nPos)
{
if(nPos > m_nQueueSize - 1)
{
AfxMessageBox(_T("越界!"));
return -1;
}

double dbValue = m_queue.dbData[nPos];

return dbValue;
}

int CStack::Size()
{
if(IsEmpty())
return 0;

int nSize = m_queue.nHead - m_queue.nTail +1;

return nSize;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: