顺序栈and链栈的数制转换

顺序栈 

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#define N 100005
#define ll long long
using namespace std;
#define size 100
#define STACKINCREAMENT 10
struct sqstack {
	int *base;
	int *top;
	int stacksize;
};
int initstack(sqstack &s) {
	s.base=new int[size];
	if(!s.base) {
		return 0;
	}
	s.top=s.base;
	s.stacksize=size;
	return 1;
}
int empty(sqstack s) {
	if(s.base==s.top)
		return 0;
	return 1;
}
int top(sqstack s) {
	return *(s.top-1);
}
int push(sqstack &s,int e) {
	if(s.top-s.base==s.stacksize)
		return 0;
	*s.top++=e;
	return 1;
}
int pop(sqstack &s) {
	int e;
	if(s.top==s.base) {
		return 0;
	}
	e=*--s.top;
	return e;
}
int main() {
	sqstack s;
	initstack(s);
	int n;
	cout<<"输入一个非负十进制数:"<<endl;
	while(cin>>n) {
		while(n) {
			int temp=n%8;
			n/=8;
			push(s,temp);
		}
		cout<<"与其等值的八进制数是:"<<endl;
		while(empty(s)) {
			int e;
			cout<<pop(s);
		}
		cout<<endl;
	}
	return 0;
}

链栈 

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#define N 100005
#define ll long long
using namespace std;
struct Lnode {
	int data;
	Lnode *next;
};
int initstack(Lnode *&s) {
	s=NULL;
	return 0;
}
int empty(Lnode *&L) {
	if(L!=NULL)
		return 1;
	return 0;
}
int push(Lnode *&L,int e) {
	Lnode *p;
	p=new Lnode;
	p->data=e;
	p->next=L;
	L=p;
	return 1;
}
int top(Lnode *L) {
	if(L!=NULL)
		return L->data;
}
int pop(Lnode *&L,int e) {
	if(L==NULL)
		return 0;
	e=L->data;
	Lnode *p;
	p=L;
	L=L->next;
	delete p;
	return e;
}
int main() {
	Lnode *head;
	initstack(head);
	int n;
	cout<<"输入一个非负十进制数:"<<endl;
	cin>>n;
	while(n) {
		int temp=n%8;
		n/=8;
		push(head,temp);
	}
	cout<<"与其等值的八进制数是:"<<endl;
	while(empty(head)) {
		int e;
		cout<<pop(head,e);
	}
	return 0;
}

。。还是对 指针 领悟不够。。再次学习一下 https://blog.csdn.net/raoshihong/article/details/42580657

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值