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

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

求大佬幫助

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Customer
{
public:
??? Customer(string name,int age);
??? Customer();
??? void printInfo()const;
private:
??? string m_strName;
??? int m_iAge;
};
Customer::Customer(string name, int age)
{
?m_strName = name;
?m_iAge = age;
}
Customer::Customer()
{
??? m_strName = "null";
??? m_iAge = 0;
}
void Customer::printInfo()const
{
??? cout<<m_strName<<" : "<<m_iAge<<endl;
}

class Queue
{
private:
??? Customer* m_pQueue;????????? //隊列數組指針
??? int m_iQueueLen;?????????? //隊列數組長度
??? int m_iQueueCapacity;????? //隊列數組容量
??? int m_iHead;
??? int m_iTail;

public:
??? Queue(int queueCapacity)//創建隊列
??? {
??????? m_pQueue = new Customer[m_iQueueCapacity];
??????? ClearQueue();
??????? m_iQueueCapacity = queueCapacity;

??? }
??? virtual ~Queue()????????? //銷毀隊列
??? {
??????? delete []m_pQueue;
??????? m_pQueue = NULL;
??? }
??? void ClearQueue()???????? //清空隊列
??? {
??????? m_iHead = 0;
??????? m_iTail = 0;
??????? m_iQueueLen = 0;
??? }
??? bool QueueEmpty() const?? //判空隊列
??? {
??????? if(m_iQueueLen == 0){
??????????? return true;
??????? }
??????? else return false;
??? }
??? bool QueueFull() const??? //判斷是否為滿
??? {
??????? if(m_iQueueLen == m_iQueueCapacity){
??????????? return true;
??????? }
??????? else return false;
??? }
??? int QueueLength() const?? //隊列長度
??? {
??????? return m_iQueueLen;
??? }
??? bool EnQueue(Customer element) //新元素入列
??? {
??????? if(QueueFull())
??????? {
??????????? return false;
??????? }
??????? else
??????? {
??????????? m_pQueue[m_iTail] = element;
??????????? m_iTail++;
??????????? m_iQueueLen++;
??????????? m_iTail = m_iTail % m_iQueueCapacity;
??????????? return true;
??????? }
??? }
??? bool DeQueue(Customer &element)//首元素出列
??? {
??????? if(QueueEmpty())
??????? {
??????????? return false;
??????? }
??????? else
??????? {
??????????? element = m_pQueue[m_iHead];
??????????? m_iHead++;
??????????? m_iQueueLen--;
??????????? return true;
??????? }
??? }
??? void QueueTraverse()????? //遍歷隊列
??? {
??????? for(int i = m_iHead;i < m_iQueueLen + m_iHead;i++)
??????? {
??????????? cout << "前面還有" << (i-m_iHead) << "個人" <<endl;
??????????? m_pQueue[i % m_iQueueCapacity].printInfo();
??????? }
??? }

};
int main()
{
??? Queue *p = new Queue(4);
??? Customer c1("Letme",1);
??? Customer c2("Uzi",2);
??? Customer c3("Ming",3);
??? Customer c4("Mlxg",4);
??? p->EnQueue(c1);
??? cout<<"?"<<endl;
??? p->EnQueue(c2);
??? p->EnQueue(c3);
??? p->EnQueue(c4);
??? p->QueueTraverse();
??? return 0;
}


這段代碼的遍歷和其他都沒問題,但是往里面加數據時無法成功加入


正在回答

1 回答

我也看了半天,發現問題出在這里,

? Queue(int queueCapacity)//創建隊列
??? {
??????? m_pQueue = new Customer[m_iQueueCapacity];//這里的m_iQueueCapacity并沒有初始值,無法分配數組,應該是筆誤,應該改成queueCapacity
??????? ClearQueue();
??????? m_iQueueCapacity = queueCapacity;

??? }

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

舉報

0/150
提交
取消
數據結構探險—隊列篇
  • 參與學習       110125    人
  • 解答問題       183    個

與現實最為貼近的數據結構-隊列,帶大家進入數據結構的美妙世界

進入課程

求大佬幫助

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

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

幫助反饋 APP下載

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

公眾號

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