我已经为堆栈数据结构编写了一个程序。 显示堆栈元素时显示错误。当堆栈具有多于2个值时,在调用显示函数时启动无限循环
#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 Class
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();
}
答案 0 :(得分:1)
试试这个。
#include <iostream>
#include <string>
using namespace std;
class Node
{
public:
int data;
Node *previous;
};
class Stack
{
private:
Node *head, *start;
// Node* head,start; // This is same with "Node start, *head";
public:
Stack();
Node* getNode();
void parseValue();
void push(Node *);
void display();
};
Stack::Stack()
{
head = NULL;
}
Node *Stack::getNode()
{
Node *temp = new Node;
cin >> temp->data;
temp->previous = NULL;
return temp;
}
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);
}
}
void Stack::push(Node* newNode)
{
if(head == NULL)
{
head = newNode;
cout << newNode->data << " added to stack" << endl;
}
else
{
// change sequencs to this
newNode->previous = head;
head = newNode;
/*
head = newNode; // if you do this first,
newNode->previous = head; // then newNode becomes head node
// and results like this: head->previous = head,
// so head->head->head->...
*/
}
}
void Stack::display()
{
if(head == NULL)
{
cout << "No data in the stack to display.";
}
else
{
Node * temp = head;
while(temp != NULL) // changed; you should check temp's state because it points the ndoe you want
{
cout << temp->data << "\t";
temp = temp->previous;
}
}
}
int main()
{
Stack a;
a.parseValue();
a.display();
return 0; // it is better to end main with return statement
}
编辑:
这是更好的堆栈模型。试试吧。
#include <iostream>
using namespace std;
// I don't know what you learned, so I'll try
// to explain my code as easy as possible.
// define stack's status constant
const int STACK_IS_EMPTY = -1; // -1 or whatever you want
// Node
// If you learned C++ template, replace it with that
class Node {
// It is better to encapsulate member variable(field)
// than to let fields in pulic
int _data;
Node *_next;
public:
// constructor
// * advice: try to find 'member initializer'
Node(int data) {
_data = data;
}
/* same constructor written with member initializer
Node(int data): _data(data) {}
*/
// member getter & setter method to encapsulation
int data() { return _data; }
void setData(int data) { _data = data; }
Node *next() { return _next; }
void setNext(Node *next) { _next = next; }
};
// Stack
class Stack {
// fields
Node *head;
public:
Stack(); // constructor
~Stack(); // destructor
// push and pop functions are stack basic functions
void push(int data);
int pop();
// convenient functions
bool is_empty(); // return true when stack is empty, false when not
int top(); // return stack's top node data
};
// constructor
Stack::Stack() {
// * advice: C++11 or higher supports nullptr keywords; try this after
// head = nullptr;
head = NULL;
}
void Stack::push(int data) {
// make new node
Node *newNode = new Node(data);
if (is_empty() == true) { // if stack is empty
head = newNode;
}
else { // stack is not empty
newNode->setNext(head);
head = newNode;
}
}
int Stack::pop() {
if (is_empty() == true) {
return STACK_IS_EMPTY;
}
Node *delNode = head;
head = delNode->next();
int data = delNode->data();
delete delNode;
return data;
}
// convenient functions
bool Stack::is_empty() {
return (head == NULL);
}
int Stack::top() {
if (head == NULL) {
return STACK_IS_EMPTY;
}
return head->data();
}
// destructor
Stack::~Stack() {
// you must deallocate dynamically allocated object
// if you don't do this, it results to serious leak of memories
if (is_empty() == true) { // if stack is empty
return; // just return
}
// deallocate all nodes
while (is_empty() == false) { // while stack is not empty
pop(); // keep popping
}
}
// Demonstration of stack usage
int main() {
int input;
Stack stack;
while (true) { // keep getting input number and pushing it into stack
cout<<"Enter natural number (0 when quit): ";
cin>>input;
if (input == 0) { // when quit number has entered
break; // quit
}
stack.push(input);
}
while (stack.is_empty() == false) { // while stack is not empty
int data = stack.pop(); // pop stack's node and get it's data
cout<<data<<endl; // print it
}
return 0;
}