栈的链表实现

#define NULL 0
#include<iostream>
using namespace std;
template<class Type>
struct node{
	Type data;
	node<Type> *link;
};

template<class Type>
class Stack{
private:
	node<Type> *top;
public:
	Stack(){
		top=NULL;
	}
	~Stack(){
		node<Type> *temp;
		while(top){
			temp=top;
			top=top->link;
			delete temp;
		}
	}
	bool Push(const Type& item){
		node<Type> *temp = new node<Type>;
		if(temp){
			temp->data=item;
			temp->link=top;
			top=temp;
			return true;
		}
	}
	bool IsEmpty(){
		return top==NULL;
	}
	bool Pop(Type& item){
		if(IsEmpty()){
			cout<<"Stack is empty"<<endl;
			return false;
		}
		else{
			node<Type> *temp;
			item=top->data;
			temp=top;
			top=top->link;
			delete temp;
			return true;
		}
	}
};
int main(){
	Stack<int> s1;
	s1.Push(5);
	s1.Push(7);
	int n1;
	s1.Pop(n1);
	cout<<n1<<endl;
}

你可能感兴趣的:(栈的链表实现)