0
我已經編寫了一個用於堆棧數據結構的程序。 顯示堆棧元素時顯示錯誤。當堆棧具有多於2個值在C++中顯示堆棧元素時出現無限循環
#include <iostream>
#include <string>
using namespace std;
節點類
class Node
{
public:
int data;
Node *previous;
};
class Stack
{
private:
Node* head,start;
public:
Stack();
Node* getNode();
void parseValue();
void push(Node *);
void display();
};
堆棧類
Stack::Stack()
{
head = NULL;
}
void Stack::parseValue()
{
char choice;
Node *newNode = NULL;
while (1)
{
cout << "Enter Data in the List (Enter N to cancel) ";
cin >> choice;
if (choice == 'n' || choice == 'N')
{
break;
}
newNode = getNode();
push(newNode);
}
}
int main()
{
Stack a;
a.parseValue();
a.display();
}
老實說,我想告訴你更好的堆棧模型,但它超出範圍,這個問題... – 2015-04-11 05:13:12
這剛剛工作。 顯示我想知道更好的堆棧模型。 – 2015-04-11 05:47:39
你應該知道這只是爲了學習,這不會用在真實的項目中;我們使用STL(如std :: stack)。 –
2015-04-11 09:00:18