参考代码1:
/顺序栈结构利用数组实现—参数由指针传递/
#include<stdlib.h>
#include<stdio.h>
#include<stddef.h>
#define MAXSIZE 50
typedef char ElemType;
/*定义栈结构*/
typedef struct
{ ElemType data[MAXSIZE];
int top;
}SeqStack,*PSeqStack;
/*初始化栈,构造一个空栈,如果成功,则返回栈的地址*/
PSeqStack Init_SeqStack()
{ PSeqStack s;
s=(PSeqStack)malloc(sizeof(SeqStack));
if(s)
s->top=-1;
return s;
}
/* 判断栈是否为空,如果为空,则返回1,否则返回0*/
int Empty_SeqStack(PSeqStack s)
{if(s->top==-1)
return 1;
else
return 0;
}
/*入栈操作,栈不满时,入栈一个元素,成功返回1,失败返回0*/
int Push_SeqStack(PSeqStack s,ElemType x)
{
if(s->top==MAXSIZE-1 )
return 0;
else
{s->top=s->top+1;
s->data[s->top]=x;
return 1;
}
}
/*出栈操作,栈不空时,出栈一个元素,用参数*x保存,成功返回1,失败返回0*/
int Pop_SeqStack(PSeqStack s,ElemType *x)
{
if(Empty_SeqStack(s))
return 0;
else
{ *x=s->data[s->top];
s->top=s->top-1;
return 1;
}
}
/*取栈顶元素操作,栈不空时,获取栈顶元素,成功返回1,失败返回0*/
int GetTop_SeqStack(PSeqStack s,ElemType *x)
{
if(Empty_SeqStack(s))
return 0;
else
*x=s->data[s->top];
return 1;
}
/*销毁栈*/
void Destroy_SeqStack(PSeqStack *s)
{
if(*s)
free(*s);
*s=NULL;
return;
}
/*十进制转换成r进制(2,8,16)*/
void Conversion(int num,int r)
{ PSeqStack s;
ElemType x;
if(!r)
{
printf("基数不能为0");
return;
}
s=Init_SeqStack();
if(!s)
{
printf("初始化栈空间失败");
return;
}
while(num)
{if(num%r>9)
Push_SeqStack(s,num%r+'A'-10); /*余数大于9,则进栈ABCDEF*/
else
Push_SeqStack(s,num%r+'0'); /*余数小于10,则进栈数字字符*/
num=num/r;
}
while(!Empty_SeqStack(s))
{Pop_SeqStack(s,&x);
printf("%c",x);
}
}
int main()
{ int r, num;
printf("请输入要转换的数据");
scanf("%d",&num);
printf("请输入要转换成几进制:");
scanf("%d",&r);
Conversion(num,r);
getch(); //非程序一部分,Dev-C++环境要求
}
参考代码2:
/*利用顺序栈----清华版定义---实现十进制转换为R进制*/
#include<malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define Null 0
typedef int status;
typedef int elemtype;
typedef struct
{elemtype *base;
elemtype *top;
int stacksize;
} sqstack;
status initstack(sqstack *s)
{s->base=(elemtype *)malloc(STACK_INIT_SIZE*sizeof(elemtype));
if(s->base==Null)
{printf("error");
exit(0);
}
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return(OK);
}
status stackempty(sqstack *s)
{if(s->top==s->base)
return OK;
else
return ERROR;
}
void push(sqstack *s,elemtype e)
{if(s->top-s->base>=s->stacksize )
{ s->base=(elemtype *)realloc(s->base,(s->stacksize+ STACKINCREMENT)*sizeof(elemtype));
if(!s->base) exit(0);
s->top=s->base+s->stacksize;
s->stacksize+= STACKINCREMENT;
}
*(s->top)=e;
s->top++;
}
void pop(sqstack *s,elemtype *e)
{if(s->top==s->base)
printf("underflow!\n");
else
*e=*(--s->top);
}
status gettop(sqstack *s,elemtype *e)
{
*e=*(s->top-1);
}
void conversion()
{sqstack s;
int N,R,data,popelem;
initstack(&s);
printf(“input N and R:\n”);
scanf("%d,%d",&N,&R);
while(N)
{push(&s,N%R);
N=N/R;
}
while(!stackempty(&s))
{gettop(&s,&data);
printf("%d\t",data);
pop(&s,&popelem);
}
}
main()
{conversion();
}
/*利用l带头结点的链栈实现十进制数转换为八进制数*/
#define Null 0
struct stacknode
{int data;
struct stacknode *next;
};
push(struct stacknode *s,int x) /*使一个值为x的数进栈*/
{struct stacknode *p;
p=(struct stacknode *) malloc(sizeof(struct stacknode));
p->data=x;
p->next=s->next;
s->next=p;
}
int pop(struct stacknode *s) /*出栈操作*/
{struct stacknode *p;
p=s->next;
if(s->next==Null)
return 0;
s->next=p->next;
free(p);
return 1;
}
int gettop(struct stacknode *s) /*取栈顶元素*/
{ if(s->next==Null)
{ printf("no data exit");
return -1;
}
return s->next->data;
}
main()
{struct stacknode *s;
int N,data;
s=(struct stacknode *) malloc(sizeof(struct stacknode)); /*头指针指向头结点*/
s->next=Null;
scanf("%d",&N);
while(N)
{push(s,N%8); /*余数进栈*/
N=N/8;
}
while(s->next!=Null)
{data=gettop(s);
printf("%d\t",data);
pop(s);
}
}