here's the code
class Node{
public:
Node(){ next=NULL; value=0; }
void setNext(Node *next){ this->next=next; }
void setValue(int v){ value=v; }
Node *getNext(){ return next; }
int getValue(){ return value; }
private:
int value;
Node *next;
};
template<class T>
class Stack{
public:
Stack(){
count=0;
newNode=NULL;
head=NULL;
}
T getTop(){ return head->getValue(); }
int getCount(){ return count; }
void push(T &v){
newNode=new Node();
newNode->setValue(v);
newNode->setNext(NULL);
if(head==NULL){
head=newNode;
}
else{
newNode->setNext(head);
head=newNode;
}
count++;
}
int pop(){
T temp;
if(!isEmpty()){
Node *t1=head;
temp=head->getValue();
head=head->getNext();
count--;
delete t1;
}
else
cout<<"Stack Empty";
return temp;
}
bool isEmpty(){
if(head==NULL)
return true;
else
return false;
}
void displayStack(){
Node *t=head;
if(!isEmpty() ){
while(t!=NULL){
cout<<" "<<t->getValue()<<" ";
t=t->getNext();
}
}
else
cout<<" Stack Empty \n";
}
private:
int count;
Node *newNode;
Node *head;
};


Sign In
Create Account


Back to top









