#include<iostream>
using namespace std;
struct node{
	int data;
	node* next;
};
class Stack{
	private:
		node* top;
	public:
		Stack();
		~Stack();
		int Push(int);
		int Pop(int&);
		int isEmpty();
		void Clear();
		void Print();
};
Stack::Stack(){
	top=new node;
	top->data=0;
	top->next=NULL;
}
Stack::~Stack(){
	Clear();
}
int Stack::Push(int e){
	node* tmp=new node;
	tmp->data=e;
	tmp->next=top;
	top=tmp;
}
int Stack::Pop(int &e){
	if(top==NULL)
		return -1;
	e=top->data;
	node* tmp=top;
	top=top->next;
	delete tmp;
}
int Stack::isEmpty(){
	return (top==NULL)?-1:0;
}
void Stack::Clear(){
	node* tmp;
	node* p=top;
	while(p!=NULL){
		tmp=p;
		p=p->next;
		delete p;
	}
}
void Stack::Print(){
	node* p=top;
	while(p->next!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
}
int main(){
	Stack s;
	s.Push(11);
	s.Push(22);
	s.Print();
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐