用栈怎么实现将十进制数n转换为m进制数,其中2<=m<=16;请问下面哪里错了啊啊啊啊?
用栈怎么实现将十进制数n转换为m进制数,其中2<=m<=16;请问下面哪里错了啊啊啊啊?
#include<iostream>
#include <assert.h>
using namespace std;
template<class ElemType>
struct LinkNode
{
ElemType data;
struct LinkNode *next;
};
template<class ElemType>
class LinkStack
{
private:
LinkNode<ElemType> *next;
public:
void InitStack();//构建(初始化)栈
void ~LinkStack();//析构栈
bool Empty();//判空
void Push(ElemType e);//入栈
void Pop();//出栈
ElemType GetTop();//取栈顶元素
void Clear();//清空
int Length();//求栈长
void Disp();//显示栈中元素
void Convert(int n,int m)//进制转换
};
//初始化
template<class ElemType>
void LinkStack<ElemType>::initStack()
{
head=new LinkNode<ElemType>;
head->next=0
}
//析构
template<class ElemType>
void LinkStack<ElemType>::~LinkStack()
{
LinkNode *p=head;
*q=head->next;
while(q!=NULL)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
//判空
template<class ElemType>
bool LinkStack<ElemTpye>::Empty()
{
return(head->next==NULL);
}
//入栈
template<class ElemType>
void LinkStack<ElemType>::Push(ElemType e)
{
LinkNode<ElemType> *newNode=new LinkNode<ElemType>;
newNode->e;
newNode->next=head->next;
head->next=newNode;
}
//出栈
template<class ElemType>
void LinkStack<ElemType>::Pop()
{
assert(head->next);
LinkNode<ElemType> *deleteNode=head->next;
head->next=deleteNode->next;
delete deleteNode;
}
//取栈顶元素
template<class ElemType>
ElemType LinkStack<ElemType>::GetTop()
{
assert(head->next);
return head->next->data;
}
//清空
template<class ElemType>
void LinkStack<ElemType>::Clear()
{
LinkNode<ElemType> *p=head->next;
*q=p->next;
while(q!=NULL)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
//求栈长
template<class ElemType>
int LinkStack<ElemType>::Length()
{
int count=0;
LinkNode *p=head->next;
while(p!=0)
{
count++;
p=p->next;
}
return count;
}
//显示栈
template<class ElemType>
void LinkStack<ElemType>::Disp()
{
LinkNode *p=head->next;
while(p!=0)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
//十进制数n转换成m进制数(2<=m<=16)
template<class ElemType>
void LinkStack<ElemType>::Convert(int n,int m)
{
assert(m<2||m>16);
LinkStack s;
InitStack(s);
while(n!=0)
{
Push(ElemType n%m);
n=n/m;
}
cout<<"转换后的数是:";
while(Empty(s))
{
int x=GetTop(s),y;
if(x<10)
cout<<x;
else
y=x-10+'A';
cout<<y;
}
cout<<endl;
}