用链栈将十进制转换成八进制

使用链栈将十进制转换成八进制`

//十进制转化成八进制
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100

#define OK 1;
#define ERROR 0;
#define True 1;
#define False 0;

typedef int Status;
typedef  int SElemType;

//栈的链式存储结构

typedef struct StackNode
{ 
	SElemType data;
	struct StackNode *next;
 }StackNode,*LinkStackPtr;
 
typedef struct LinkStack
{   
    LinkStackPtr top;
    int count;
 }LinkStack;
 
//进栈操作
Status Push(LinkStack *S,SElemType e)//SElemType 栈的顺序存储实现 
{ 
	LinkStackPtr s=(LinkStackPtr)malloc(sizeof(StackNode));
	s->data=e;
	s->next=S->top;
	S->top=s;
	S->count++;
	return OK;
 } 

//栈的移出
Status Pop(LinkStack *S,SElemType *e)
{ 
	LinkStackPtr p;
    *e=S->top->data;
    p=S->top;
    S->top=S->top->next;
    free(p);
    S->count--;
    return OK;
	
 } 

void InitStack(LinkStack *S)
{ 
	S->top=0;
}//将栈初始化为空栈

int StackEmpty(LinkStack S)
{ 
	if(S.top==0)
	return 1;
	else
	return 0;
 } //判断栈是否为空
 
void Conversion(int N)
{ 
	LinkStack S;
	int x;
	InitStack(&S);
	while(N>0)
	{ 
		x=N%8;
		Push(&S,x);
		N/=8;
	}
	 printf("转换成八进制为:");  
	    while(!StackEmpty(S))  
	    {   
	        Pop(&S,&x);  
	        printf("%d",x);  
	    }  
	    printf("\n");  
	}  


 
 int main(void)  
	{   
	    int N;  
	    printf("请输入需要数制转换的十进制数:");  
	    scanf("%d",&N);  
	    Conversion(N);  
	    return 0;  
	}  

代码中部分借鉴了其他博主的代码下面是链接,如果有不合适的联系删除: https://blog.csdn.net/weichanjuan3/article/list/3
代码有不正确之处,还请多多包涵

    原文作者:jackson-tao
    原文地址: https://blog.csdn.net/qq_45755309/article/details/105016023
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞