#include<iostream>
#include<stdlib h="">
using namespace std;
template <class t="">
class LinkStack;
template <class t="">
class StackNode
{
public:
friend class LinkStack<t>;
private:
T data;
StackNode<t> *next;
};
template<class t="">
class LinkStack
{
public:
LinkStack() { top = NULL; }
~LinkStack();
bool StackEmpty()
{ return top ==NULL; }
void Push(T x);
void Pop(T &x);
T GetTop();
private:
StackNode<t>*top;
};
template<class t=""> //入栈
void LinkStack<t>::Push(T x)
{
StackNode<t> *p;
p = new StackNode<t>;
p->data = x;
p->next = top;
top = p;
}
template <class t=""> //返回栈顶元素
T LinkStack<t>::GetTop()
{
if (StackEmpty())
{
cout << "为空" << endl;
return -1;
}
else return top->data;
}
template <class t=""> //出栈
void LinkStack<t>::Pop(T &x)
{
StackNode<t> *p = top;
if (StackEmpty())
{
cout << "为空" << endl;
}
else
{
x = p->data;
top = p->next;
delete p;
}
}
template <class t="">
LinkStack<t>::~LinkStack() //析构函数
{
StackNode <t> *s;
while (top)
{
s = top->next;
delete top;
top = s;
}
}
void conversion(int N, int d) //进制转换
{
LinkStack<int>S;
int i;
while (N)
{
S.Push(N%d);
N = N / d;
}
while (!S.StackEmpty())
{
S.Pop(i);
cout << i;
}
cout << endl;
}
void main()
{
int N(64), d(2);
cout<<"十进制64转化为二进制:";
conversion(N, d);
system("PAUSE");
return;
}
</int></t></t></class></t></t></class></t></class></t></t></t></class></t></class></t></t></class></class></stdlib></iostream>
链式栈简单模板代码
最新推荐文章于 2023-07-16 11:37:45 发布