这是我写的一个类,实现简单的栈,但不知道为什么会停止工作。。
#include<iostream.h>struct Node
{
int data;
Node* next;
};
class stack
{
public:
stack() //构造函数初始化类
{
Node* head = new Node;
head->data = 0;
head->next = NULL;
}
void put( int item )
{
Node* pS = new Node;
pS->data = item;
pS->next = head->next;
head->next = pS;
}
int get()
{
int num = 0;
num = head->next->data;
head = head->next;
return num;
}
private:
Node* head;
};
void main()
{
stack lala;
lala.put(10);
lala.put(12);
lala.put(14);
cout<<lala.get()<<endl;
cout<<lala.get()<<endl;
cout<<lala.get()<<endl;
}