#include<iostream>
using namespace std;
template <class T>
struct Node
{
T data;
Node *next;
};
template <class T>
class Linkstack
{
public:
Linkstack()
{
top=NULL;
}
~Linkstack()
{
Node<T> *p;
while(top!=NULL)
{
p=top;
top=top->next;
delete p;
}
}
void push(T x);
void Printlist();
T pop();
T gettop();
int Size();
int Empty()
{
if(top==NULL)return 1;
else return 0;
}
private:
Node<T>*top;
};
template<class T>
void Linkstack<T>::push(T x)
{
Node<T> *p;
p=new Node<T>;
p->data=x;
p->next=top;
top=p;
}
template<class T>
T Linkstack<T>::pop()
{
if(top==NULL)
{
throw"floorout";
}
Node<T>*p;
p=top;
T x=top->data;
top=top->next;
delete p;
return x;
}
template<class T>
T Linkstack<T>::gettop()
{
if(top==NULL)
{
throw"floorout";
}
T x=top->data;
return x;
}
template <class T>
int Linkstack<T>::Size()
{
int sizel=0;
Node<T>* p;
p=top;
while(p!=NULL)
{
sizel++;
p=p->next;
}
return sizel;
}
template <class T>
void Linkstack<T>::Printlist()
{
Node<T>*p;
p=top;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
Linkstack<int>s;
s.push(1);
s.push(2);
s.push(4);
s.Printlist();
cout<<endl;
cout<<s.Size()<<endl;
cout<<s.pop()<<endl;
s.Printlist();
}
using namespace std;
template <class T>
struct Node
{
T data;
Node *next;
};
template <class T>
class Linkstack
{
public:
Linkstack()
{
top=NULL;
}
~Linkstack()
{
Node<T> *p;
while(top!=NULL)
{
p=top;
top=top->next;
delete p;
}
}
void push(T x);
void Printlist();
T pop();
T gettop();
int Size();
int Empty()
{
if(top==NULL)return 1;
else return 0;
}
private:
Node<T>*top;
};
template<class T>
void Linkstack<T>::push(T x)
{
Node<T> *p;
p=new Node<T>;
p->data=x;
p->next=top;
top=p;
}
template<class T>
T Linkstack<T>::pop()
{
if(top==NULL)
{
throw"floorout";
}
Node<T>*p;
p=top;
T x=top->data;
top=top->next;
delete p;
return x;
}
template<class T>
T Linkstack<T>::gettop()
{
if(top==NULL)
{
throw"floorout";
}
T x=top->data;
return x;
}
template <class T>
int Linkstack<T>::Size()
{
int sizel=0;
Node<T>* p;
p=top;
while(p!=NULL)
{
sizel++;
p=p->next;
}
return sizel;
}
template <class T>
void Linkstack<T>::Printlist()
{
Node<T>*p;
p=top;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
Linkstack<int>s;
s.push(1);
s.push(2);
s.push(4);
s.Printlist();
cout<<endl;
cout<<s.Size()<<endl;
cout<<s.pop()<<endl;
s.Printlist();
}