#ifndef STACK_H
#define STACK_H
template<typenameE>classStack;template<typenameE>classSNode{Edata;SNode<E>*next=nullptr;friendclassStack<E>;SNode():next(nullptr){}};template<typenameE>classStack{public:Stack();~Stack();intsize();boolis_empty();boolpush(constE&);boolpop();booltop(E&);private:SNode<E>*head_;// Pointer to front of the list.
intnum_of_nodes_;};template<typenameE>Stack<E>::Stack(){head_=nullptr;num_of_nodes_=0;}template<typenameE>Stack<E>::~Stack(){while(!is_empty())pop();}template<typenameE>intStack<E>::size(){returnnum_of_nodes_;}template<typenameE>boolStack<E>::is_empty(){// Return true if there are nodes in the list, else return false.
returnnum_of_nodes_==0;}template<typenameE>boolStack<E>::push(constE&kElement){// Add a new element to the front of the list.
SNode<E>*new_node=newSNode<E>;if(new_node==nullptr)returnfalse;new_node->data=kElement;new_node->next=head_;head_=new_node;num_of_nodes_++;returntrue;}template<typenameE>boolStack<E>::pop(){// Remove the front element of the list.
if(is_empty())returnfalse;SNode<E>*front=head_;head_=head_->next;deletefront;front=nullptr;num_of_nodes_--;returntrue;}template<typenameE>boolStack<E>::top(E&element){// Return the front element of the list.
if(is_empty())returnfalse;element=head_->data;returntrue;}#endif