再進(jìn)行數(shù)據(jù)結(jié)構(gòu)隊(duì)列篇的時(shí)候,遇到意外的 #endif???請(qǐng)問(wèn)怎么解決?
/* ? MyQueue.cpp 代碼 */
#include"MyQueue.h"
#include<iostream>
using namespace std;
MyQueue::MyQueue(int queueCapacity) ? //引用隊(duì)列
{
m_iQueueCapacity = queueCapacity; ?//容量賦初值
m_pQueue = new int[m_iQueueCapacity]; //指針初值?
ClearQueue(); ? ? ? ? ? ? ? ? ? ? ? ? //清空隊(duì)列
} ?
MyQueue::~MyQueue() ?//銷毀隊(duì)列
{
delete[m_pQueue];
m_pQueue = NULL;
}
void MyQueue::ClearQueue() //清空隊(duì)列
{
m_iHead = 0;
m_iTail =0;
m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty() const //判斷隊(duì)列是否為空
{
if(m_iQueueLen == 0)
{
return true;
}
else
{
return false;
}
//return m_iQueueLength ==0?true:false;
}
int MyQueue::QueueLength() const ?//隊(duì)列長(zhǎng)度
{
return m_iQueueLen;
}
bool QueueFull() const ? //判斷隊(duì)列是否為滿
{
if(m_iQueueLen == m_iQueueCapacity)
{
return true;
}
else?
{
return false;
}
}
bool MyQueue::EnQueue(int element) ?//將新元素插入隊(duì)列
{
if(QueueFull())
{
return false;
}
else
{
m_pQueue[m_iTail] = element; ?//將參數(shù)傳遞給隊(duì)尾
m_iTail++;
m_iTail = m_iTail%m_iQueueCapacity; ? //讓隊(duì)列的長(zhǎng)度不變
m_iQueueLen++;
return true;?
}
}
bool MyQueue::DeQueue(int &element) ?//將一個(gè)元素出隊(duì)
{
if(QueueEmpty())
{
return false;
}
else
{?
element = m_Queue[m_iHead]; ? ?//對(duì)頭傳遞給參數(shù)?
m_iHead++;
m_iHead = m_iHead%m_iQueueCapacity; ?//取余
m_iQueueLen--;
return true;
}?
}
void MyQueue::QueueTraverse() ? //遍歷隊(duì)列,將數(shù)組打印出來(lái)
{
for(int i = m_iHead;i<m_iQueueLen;i++) ?//從頭開(kāi)始遍歷
{
cout<<m_pQueue[i%m_iQueueCapacity]<<endl;
}
}
/* ?MyQueue.h 的代碼*/
/*?
/* 環(huán)形隊(duì)列C++實(shí)現(xiàn)2015.9 by James */
#ifndef MYQUEUE_H
#define MYQUEUE_H
#endif // _DEBUG
class MyQueue
{
public:
MyQueue(int queueCapacity); //InitQueue(&Q) 創(chuàng)建隊(duì)列
virtual ~MyQueue(); ? ? ? ?//DestroyQueue(&Q); 銷毀隊(duì)列
void ClearQueue(); ? ? ? ? //ClearQueue(&Q); ? 清空隊(duì)列
bool QueueEmpty() const; ? //QueueEmoty(Q); ?判空隊(duì)列(判滿)
bool QueueFull() const;
int QueueLength() const; ? //QueueLength() ? 隊(duì)列長(zhǎng)度
bool EnQueue(int element); //EnQueue(&Q,element) 新元素入隊(duì)
bool DeQueue(int &element); //DeQueue(&Q,&element) 首元素出隊(duì)
void QueueTraverse(); ? ? ?// QueueTraverse visit() 遍歷隊(duì)列
private:
int *m_pQueue; ? ?//隊(duì)列數(shù)組指針
int m_iQueueLen; ? //隊(duì)列元素個(gè)數(shù)
int m_iQueueCapacity; //隊(duì)列數(shù)組容量
int m_iHead;
int m_iTail;
};
#endif
/* ?demo.cpp的代碼 */
#include<iostream>
#include "stdlib.h"
#include "MyQueue.h"
/* ? ? ? ? ? ? 實(shí)現(xiàn)環(huán)形隊(duì)列 ? ? */
int main(void)
{
MyQueue *P = new MyQueue(4);
delete p;
p = NULL;
system("pause");
return 0;
}
2016-11-26
析構(gòu)函數(shù)中應(yīng)該是delete []?m_pQueue;
QueueTraverse() 中循環(huán)結(jié)束條件應(yīng)該是i<m_iHead+m_iQueueLen
MyQueue.h頭文件中多出了一個(gè)#endif // _DEBUG
#ifndef 和?#endif 是一一對(duì)應(yīng)的,條件編譯。
2016-11-26
還有大小寫(xiě)敏感,P改成p