posts - 3, comments - 2, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

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

Posted on 2012-06-25 11:00 BugYang 阅读(398) 评论(0)  编辑 收藏 引用
 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;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理