return top;}
elemtype gettop(linkstack *top)//取栈顶元素
{ if(top->next!=NULL)
return (top->next->data);
else return NULL; }
elemtype empty(linkstack *top)//判栈空
{ if(top->next==NULL) return 1;
else return 0;}
void decTOhex(int n)
//十进制数 n 转换成十六进制
{ elemtype x;
linkstack *top=NULL;
top=inistack(top);//栈的初始化
while(n)
{ int j=n%16;
if(j<10) x=j+48;
//将数字 0~9 转换成字符 0~9
else x=j+55;
//将数字 10~15 转换成字符 A~F
top=push(top,x); /*进栈*/
n=n/16; }
while(!empty(top))//判栈非空
{ x=gettop(top); //取栈顶元素
cout<<x; top=pop(top);//退栈}
cout<<endl;}
void main()
{ int n;
cout<<"请输入十进制正整数:";
cin>>n;
cout<<"十进制数"<<n<<"的十六进制为";
decTOhex(n);}
(06)#include<iostream.h>
#define elem int
class astack
{ private: int size; int top;
elem *listarray;
public:
astack(){
size=1000; top=0;
listarray=new elem[size]; }
~astack(){ delete [] listarray;}
void clear() { top=0;}
bool push(elem &item)
{ if(top==size) return false;
else {
listarray[top++]=item; return true;}}
bool pop(elem &it)
{ if(top==0) return false;
else { it=listarray[--top];return true;}}
bool topvalue(elem &it)
{ if(top==0) return false;
else {it=listarray[top-1];
return true;}}
bool empty(){
if(top==0) return true;
else return false;}
int length(){ return top; }
void dectobin(int n)
{ clear(); int j=n;
while(j)
{ int k=j%2;push(k); j/=2;}
while(!empty())
{ topvalue(j); cout<<j;
pop(j); } cout<<endl;}};
void main()
{ int n;
cout<<"输入十进制整数:";
cin>>n; astack s;
cout<<"十进制为:"<<n<<endl;
cout<<"二进制为:"; s.dectobin(n);}
(07)#include<iostream.h>
#define elem int
class link
{ public: elem element; link *next;};
class lstack
{ private: link *top; int size;
public:
lstack(){top=NULL;size=0;}
void clear(){top=NULL;}
bool push( elem &item)
{ link *s=new link;
s->element=item;
s->next=top; top=s;
size++;return true;}
bool pop(elem &it)
{ if(size==0) return false;