#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
template<class ElemType>
struct Node
{
ElemType data;
Node<ElemType> *next;
Node();
Node(ElemType item,Node<ElemType> *link);
};
template<class ElemType>
Node<ElemType>::Node()
{
next=NULL;
}
template<class ElemType>
Node<ElemType>::Node(ElemType item,Node<ElemType> *link)
{
data=item;
next=link;
}
template<class ElemType>
class LinkStack
{
protected:
Node<ElemType>*top;
int cnt;
public:
LinkStack();
~LinkStack();
int Length() const;
bool Empty() const;
void Clear();
void Traverse() const;
bool Push(const ElemType &e);
bool Top(ElemType &e) const;
bool Pop(ElemType &e);
LinkStack(const LinkStack<ElemType> ©);
LinkStack<ElemType> &operator=(const LinkStack<ElemType> ©);
};
template<class ElemType>
LinkStack<ElemType>::LinkStack()
{
top=NULL;
cnt=0;
}
template<class ElemType>
LinkStack<ElemType>::~LinkStack()
{
Clear();
}
template<class ElemType>
int LinkStack<ElemType>::Length() const
{
return cnt;
}
template<class ElemType>
bool LinkStack<ElemType>::Empty() const
{
return top==NULL;
}
template<class ElemType>
void LinkStack<ElemType>::Clear()
{
ElemType tmpElem;
while(!Empty())
{
Pop(tmpElem);
}
}
template<class ElemType>
void LinkStack<ElemType>::Traverse() const
{
Node<ElemType> *tmpPtr;
LinkStack<ElemType>tmpS;
for(tmpPtr=top;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
{
tmpS.Push(tmpPtr->data);
}
for(tmpPtr=tmpS.top;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
{
cout<<tmpPtr->data<<' ';
}
cout<<endl;
}
template<class ElemType>
bool LinkStack<ElemType>::Push(const ElemType &e)
{
Node<ElemType>*newTop=new Node<ElemType>(e,top);
if(newTop==NULL)
{
return false;
}
else
{
top=newTop;
cnt++;
return true;
}
}
template<class ElemType>
bool LinkStack<ElemType>::Top(ElemType &e) const
{
if(Empty())
{
return false;
}
else
{
e=top->data;
return true;
}
}
template<class ElemType>
bool LinkStack<ElemType>::Pop(ElemType &e)
{
if(Empty())
{
return false;
}
else
{
Node<ElemType> *old_top=top;
e=old_top->data;
top=old_top->next;
cnt--;
delete old_top;
return true;
}
}
template<class ElemType>
LinkStack<ElemType>::LinkStack(const LinkStack<ElemType> ©)
{
if(copy.Empty())
{
top=NULL;
cnt=0;
}
else
{
top=new Node<ElemType>;
top->data=copy.top->data;
cnt=copy.cnt;
Node<ElemType> *buttonPtr=top;
for(Node<ElemType> *tmpPtr=copy.top->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
{
buttonPtr->next=new Node<ElemType>;
buttonPtr->next->data=tmpPtr->data;
buttonPtr=buttonPtr->next;
}
}
}
template<class ElemType>
LinkStack<ElemType>&LinkStack<ElemType>::operator=(const LinkStack<ElemType> ©)
{
if(©!=this)
{
if(copy.Empty())
{
top=NULL;
cnt=0;
}
else
{
Clear();
top=new Node<ElemType>;
top->data=copy.top->data;
cnt=copy.cnt;
Node<ElemType>* buttomPtr=top;
for(Node<ElemType>*tmpPtr=copy.top->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
{
buttomPtr->next=new Node<ElemType>;
buttomPtr->next->data=tmpPtr->data;
buttomPtr=buttomPtr->next;
}
}
}
return *this;
}
int main()
{
LinkStack<int>s;
int i;
for(i=1;i<=5;i++)
{
s.Push(i);
}
s.Traverse();
LinkStack<int>s1(s);
s1.Traverse();
return 0;
}
链栈的建立与操作
最新推荐文章于 2023-03-26 14:25:26 发布