亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

再進行數據結構隊列篇的時候,遇到意外的 #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;

}


正在回答

2 回答

析構函數中應該是delete []?m_pQueue;

QueueTraverse() 中循環結束條件應該是i<m_iHead+m_iQueueLen

MyQueue.h頭文件中多出了一個#endif // _DEBUG

#ifndef 和?#endif 是一一對應的,條件編譯。

0 回復 有任何疑惑可以回復我~
#1

qq_菜鳥_12 提問者

非常感謝!
2016-11-30 回復 有任何疑惑可以回復我~

還有大小寫敏感,P改成p

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

再進行數據結構隊列篇的時候,遇到意外的 #endif???請問怎么解決?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號