数据结构(七) 顺序栈实现数制转换
来源:互联网 发布:淘宝黑搜二期 编辑:程序博客网 时间:2024/06/05 07:34
//用栈处理数制的转换
//采用的存储结构为顺序存储结构
#include <iostream>
using namespace std;
#define MAXSIZE 100
//栈的结构体
struct Node
{
int *base;
int *top;
int stackSize;
};
//初始化栈的操作
void initStack(struct Node &S)
{
S.base = new int [MAXSIZE];
if(S.base == NULL)
{
cout<<"地址分配失败\n";
exit(1);
}
S.top = S.base;
S.stackSize = MAXSIZE;
}
//入栈操作
void push(struct Node &S,int e)
{
if(S.top-S.base == S.stackSize)
{
cout<<"此栈已经满了\n";
exit(1);
}
*S.top++ = e;
}
//出栈操作
void pop(struct Node &S,int &e)
{
if(S.top==S.base)
{
cout<<"栈为空\n";
exit(1);
}
e = *--S.top;
}
void function(struct Node S,int n)
{
int s;
while(n)
{
push(S,n%2);
n = n/2;
}
while(S.top!=S.base)
{
pop(S,s);
cout<<s<<" ";
}
}
int main()
{
struct Node S;
initStack(S);
int n;
cout<<"输入你要转换的十进制数\n";
cin>>n;
function(S,n);
return 0;
}
//采用的存储结构为顺序存储结构
#include <iostream>
using namespace std;
#define MAXSIZE 100
//栈的结构体
struct Node
{
int *base;
int *top;
int stackSize;
};
//初始化栈的操作
void initStack(struct Node &S)
{
S.base = new int [MAXSIZE];
if(S.base == NULL)
{
cout<<"地址分配失败\n";
exit(1);
}
S.top = S.base;
S.stackSize = MAXSIZE;
}
//入栈操作
void push(struct Node &S,int e)
{
if(S.top-S.base == S.stackSize)
{
cout<<"此栈已经满了\n";
exit(1);
}
*S.top++ = e;
}
//出栈操作
void pop(struct Node &S,int &e)
{
if(S.top==S.base)
{
cout<<"栈为空\n";
exit(1);
}
e = *--S.top;
}
void function(struct Node S,int n)
{
int s;
while(n)
{
push(S,n%2);
n = n/2;
}
while(S.top!=S.base)
{
pop(S,s);
cout<<s<<" ";
}
}
int main()
{
struct Node S;
initStack(S);
int n;
cout<<"输入你要转换的十进制数\n";
cin>>n;
function(S,n);
return 0;
}
0 0
- 数据结构(七) 顺序栈实现数制转换
- 顺序栈实现数制转换
- 数据结构(严蔚敏)顺序栈_数制转换
- 数制转换--顺序栈的实现
- 【数据结构】用栈实现数制的转换
- 《数据结构》算法3.8栈实现数制转换
- 数制转换问题(顺序栈)
- 数制转换各种实现 数据结构
- 数据结构实验五——栈实现数制转换
- 数据结构 P48 算法实现 栈的应用举例-数制转换
- C#栈的实现(数制转换)
- 用栈实现数制转换(java)
- 《数据结构(严蔚敏版)》学习笔记(三)——栈实现数制转换
- 《数据结构》使用数组实现数制的转换
- 7、数据结构笔记之七栈的应用之数制转换
- 数制转换(栈)
- 【数据结构】栈的应用--数制转换(c++)
- 2015年大二上-数据结构-栈(4)- 数制转换
- redis学习系列--7.redis-常用命令
- 1-python图像处理之PIL,pylab
- 回写缓存及磁盘调整
- 总结与计划(16-17)
- python 简单使用(1)
- 数据结构(七) 顺序栈实现数制转换
- JSTL的c:foreach标签
- 进程
- HTML中的那些常用标记
- SqueezeNet: Alexnet-level accuracy whith 50x Fewer Parameters And < 0.5MB Model Size
- 《Linux启动过程分析》内核挂载根文件系统
- 3 分布式数据库设计
- 数据结构之插入排序
- iOS拍照识别车牌技