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