# 数据结构——队列篇

318人阅读 评论(0)

数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。

MyQueue.h

#ifndef MyQueue_H
#define MyQueue_H
#include "Customer.h"
class MyQueue
{
public:
MyQueue (int quequeCapacity);
virtual ~MyQueue ();
void ClearQueue ();
bool QueueEmpty ()const;
bool QueueFull ()const;
int QueueLength () const;
bool EnQueue (Customer element);
bool DeQueue (Customer &element);
void QueueTraverse ();
private:
int *m_pQueue;   //可以是自己定的类别 ，比如Customer *m_pQueue;
int m_iQueuelen;
int m_QueueCapacity;
int  m_iTail;
};
#endif;

MyQueue.cpp

#include <iostream>
using namespace std;
#include "MyQueue.h"
MyQueue::MyQueue (int quequeCapacity)
{
m_QueueCapacity = quequeCapacity;
m_iTail = 0;
m_iQueuelen = 0;
m_pQueue = new int[m_QueueCapacity];       //m_pQueue = new Customer[m_QueueCapacity];
ClearQueue ();
}
MyQueue::~MyQueue ()
{
delete[]m_pQueue;
m_pQueue = NULL;
}
void MyQueue::ClearQueue ()
{
m_iTail = 0;
m_iQueuelen = 0;
}
bool MyQueue::QueueEmpty ()const
{
if ( m_iQueuelen == 0 )
{
return true;
}
else
{
return false;
}
}
int  MyQueue::QueueLength () const
{
return m_iQueuelen;
}
bool MyQueue::QueueFull ()const
{
if ( m_iQueuelen == m_QueueCapacity )
{
return true;
}
return false;
}
bool MyQueue::EnQueue (int element)              //bool MyQueue::EnQueue (Customer element)
{
if ( QueueFull () )
{
return false;
}
else
{
m_pQueue[m_iTail] = element;
m_iTail++;
m_iTail = m_iTail% m_QueueCapacity;
m_iQueuelen++;
return true;
}
}
bool MyQueue::DeQueue (int &element)                      //bool MyQueue::DeQueue (Customer &element)
{
if ( QueueEmpty () )
{
return false;
}
m_iQueuelen--;
return true;
}
void MyQueue::QueueTraverse ()
{
for ( int i = m_iHead; i < m_iQueuelen + m_iHead; i++ )
{
cout<<m_pQueue[i% m_QueueCapacity].<<endl;
}

}


demo.cpp

#include <iostream>
using namespace std;
#include "MyQueue.h"
#include "Customer.h"
int main (void)
{

MyQueue*p = new MyQueue (4);
p->EnQueue (10);
p->EnQueue (12);
p->EnQueue (14);
p->EnQueue (16);
p->EnQueue (10);      //这个不会成功一共四个
p->QueueTraverse ();
int e = 0;
p->DeQueue (e);
cout <<  endl;
cout << e << endl;
p->DeQueue (e);
cout << endl;
cout << e << endl;
cout << endl;
p->QueueTraverse ();
p->ClearQueue ();
//p->QueueTraverse ();
delete p;
p = NULL;
system ("pause");


0
0

个人资料
• 访问：19864次
• 积分：419
• 等级：
• 排名：千里之外
• 原创：20篇
• 转载：2篇
• 译文：1篇
• 评论：15条
文章分类
阅读排行
最新评论