0
爲什麼僅循環3次?For loop loop only 3次
for (size_t i = 0; i < 10; i++)
{
std::cout << "Dodany element kolejki: " << i << std::endl;
queue.Enqueue(i);
}
for循環老是打斷我等於3 這裏是隊列類代碼:爲節點結構
#ifndef QUEUE_H
#define QUEUE_H
#include "Node.h"
#include <stdexcept>
template<class T>
class Queue
{
public:
Queue()
{
this->FirstElement = nullptr;
this->LastElement = nullptr;
}
void Enqueue(T value)
{
if (0 == Size())
{
FirstElement = new Node<T>(value);
LastElement = FirstElement;
}
else
{
LastElement->Previous = new Node<T>(value);
LastElement = LastElement->Previous;
}
}
T Dequeue()
{
if (0 == Size())
{
throw std::out_of_range("Out of range ! Empty stack !");
}
T element(FirstElement->Value);
if (nullptr != FirstElement->Previous)
{
Node<T>* temp = FirstElement->Previous;
delete FirstElement;
FirstElement = temp;
}
else
{
delete FirstElement;
FirstElement = nullptr;
LastElement = nullptr;
}
return element;
}
int Size()
{
int queueSize = 0;
Node<T>* element = FirstElement;
if (nullptr != FirstElement)
{
while (nullptr != FirstElement->Previous)
{
++queueSize;
}
++queueSize; //Uwzglednienie ostatniego elementu, ktory nie jest policzony w petli
}
return queueSize;
}
private:
Node<T>* FirstElement;
Node<T>* LastElement;
};
#endif // !QUEUE_H
和代碼:
#ifndef NODE_H
#define NODE_H
template<class T>
struct Node
{
public:
Node(T value)
{
this->Previous = nullptr;
this->Value = value;
}
Node<T>* Previous;
T Value;
};
#endif // !NODE_H
這裏輸出:
Dodany element kolejki: 0
Dodany element kolejki: 1
Dodany element kolejki: 2
當我刪除循環的enqueue方法的調用10次。沒有來自編譯器或拋出的異常的警告或任何其他錯誤。由於某種原因,它只是循環3次。
請提供[最小,完整,可驗證的示例](https://stackoverflow.com/help/mcve)。 – Ron