栈(两种存储方式)——进制数字相互之间转化

题目:
将十进制数N转换成为x进制数,可以使用辗转相除法。
(1)将N除以x,取其余数;
(2)判断商是否为0,如果为零,结束程序;否则,将商送N,转(1)继续执行

顺序栈:

#define StackSize 100
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef int DataType;
typedef struct{
	DataType stack[StackSize];
	int top;
}SeqStack; 
//栈的初始化
void InitStack(SeqStack *S){
	S->top=0;//把栈顶指针置为0 
} 
//判断栈是否为空 
int StackEmpty(SeqStack S){
	if(S.top==0){
		return 1;
	}else{
		return 0;
	}
} 
//取栈顶元素
int GetTop(SeqStack S,DataType *e){
	if(S.top==0){
		printf("栈已经空了\n"); 
		return 0;
	}else{
		*e=S.stack[S.top-1];
		return 1;
	}
} 
//进栈操作
int PushStack(SeqStack *S,DataType e){
	if(S->top>=StackSize){
		printf("栈已经满了\n"); 
		return 0;
	}else{
		S->stack[S->top]=e;
		S->top++;
		return 1; 
	} 
} 
//出栈操作
int PopStack(SeqStack *S,DataType *e){
	if(S->top==0){
		printf("栈已经空了\n"); 
		return 0;
	}else{
		S->top--;
		*e=S->stack[S->top];
		return 1;
	}
} 
//返回栈的长度
int StatckLength(SeqStack S){
	return S.top;
} 
//清空栈
void ClearStack(SeqStack *S){
	S->top=0;
} 
void Coversion(int N){
	SeqStack S;
	int x;
	InitStack(&S);
	while(N>0){
		x=N%8;
		PushStack(&S,x);
		N=N/8;
	}
	printf("请输入十进制转换成八进制数字:");
	while(!StackEmpty(S)){
		PopStack(&S,&x);
		printf("%d",x); 
	}
	printf("\n");
}
void Coversion1(int N){
	SeqStack S;
	int x;
	InitStack(&S);
	while(N>0){
		x=N%2;
		PushStack(&S,x);
		N=N/2;
	}
	printf("请输入十进制转换成二进制数字:");
	while(!StackEmpty(S)){
		PopStack(&S,&x);
		printf("%d",x); 
	}
	printf("\n");
}
int main(){
	int sum;
	printf("请输入十进制数字:");
	scanf("%d",&sum);
	Coversion(sum);
	Coversion1(sum); 
	return 1;
}

在这里插入图片描述链栈:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef int DataType;
#define ListSize 100
//链栈存储类型 
typedef struct Node{
	DataType data;
	struct Node *next;
}LStackNode,*LinkStack;
//初始化链栈 
void InitStack(LinkStack *top){
	if((*top=(LinkStack)malloc(sizeof(LStackNode)))==NULL){
		exit(-1); 
	}
	(*top)->next=NULL;
} 
//判断栈是否为空
int StackEmpty(LinkStack top){
	if(top->next==NULL){
		return 1;
	}else{
		return 0;
	}
} 
//进栈操作
int PushStack(LinkStack top,DataType e){
	LStackNode *p;
	if((p=(LinkStack)malloc(sizeof(LStackNode)))==NULL){
		printf("内存分配失败!\n");
		exit(-1); 
	}
	p->data=e;
	p->next=top->next;
	top->next=p;
	return 1;
} 
//出栈操作
int PopStack(LinkStack top,DataType *e){
	LStackNode *p;
	p=top->next;//出栈操作就是将单链表中的第一个结点删除
	if(!p){
		printf("栈已空\n");
		return 0; 
	} 
	top->next=p->next;
	*e=p->data;
	free(p);
	return 1;
} 
//取栈顶元素
int GetTop(LinkStack top,DataType *e){
	LStackNode *p;
	p=top->next;
	if(!p){
		printf("栈已空\n");
		return 0; 
	}
	*e=p->data;
	return 1;
} 
//求表长操作
int StackLength(LinkStack top){
	LStackNode *p;
	int count=0;
	p=top;
	while(p->next!=NULL){
		p=p->next;
		count++;
	}
	return count;
} 
//销毁栈
void DestroyStack(LinkStack top){
	LStackNode *p,*q;
	p=top;
	while(!p){
		q=p;
		p=p->next;
		free(q); 
	}
} 

void Coversion(int N){
	LinkStack top;
	int x;
	InitStack(&top);
	while(N>0){
		x=N%8;
		PushStack(top,x);
		N=N/8;
	}
	printf("请输入十进制转换成八进制数字:");
	while(!StackEmpty(top)){
		PopStack(top,&x);
		printf("%d",x); 
	}
	printf("\n");
}
void Coversion1(int N){
	LinkStack top;
	int x;
	InitStack(&top);
	while(N>0){
		x=N%2;
		PushStack(top,x);
		N=N/2;
	}
	printf("请输入十进制转换成二进制数字:");
	while(!StackEmpty(top)){
		PopStack(top,&x);
		printf("%d",x); 
	}
	printf("\n");
}
int main(){
	int sum;
	printf("请输入十进制数字:");
	scanf("%d",&sum);
	Coversion(sum);
	Coversion1(sum); 
	return 1;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值