-1
我創建班級隊列,我需要找到最小值和最大值。最大值是正確的,但最小值不是。爲了得到正確的結果,我應該更改 頭部和尾部的值的數量?如果我改變他們爲零,第一個元素成爲對象被創建後最大和最小 queue找到我自己的班級隊列的最小值
class myQueue
{
private:
int size, head, tail, *data;
public:
myQueue(int size = 1) :
size(size)
{
data = new int[size];
head = -1;
tail = -1;
}
~myQueue() //
{
delete[] data;
}
void myEnqueue(int el)
{
data[tail] = el;
tail = (tail + 1) % size;
}
int maxEl()
{
int *temp = &data[head];
int max = *temp;
while (*temp != data[tail])
{
temp++;
max = (max > *temp) ? max : *temp;
}
return max;
}
int minEl()
{
int *temp = &data[head];
int min = *temp;
while (*temp != data[tail])
{
temp++;
min = (min < *temp) ? min : *temp;
}
return min;
}
};
int main()
{
myQueue q(5);
int n = 0, el;
while (n < 5)
{
cin >> el;
q.myEnqueue(el);
n++;
}
cout << "The arithmetic mean of the elements = " << q.srArifm(5) << "\n";
cout << "Maximum queue element = " << q.maxEl() << "\n";
cout << "Minimum queue element =" << q.minEl() << "\n";
system("pause");
return 0;
}
什麼號碼,我應該改變頭的值並尾巴得到正確的結果?如果我將它們更改爲零,則第一個元素將變爲最大值和最小值 – choko
@choko使用修補程序編輯 – mpiatek
@choko檢查我的最新編輯。該版本在隊列正確填滿時處理替換元素 – mpiatek