数据结构(七) 顺序栈实现数制转换
2016-12-30 21:34
197 查看
//用栈处理数制的转换
//采用的存储结构为顺序存储结构
#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;
}
相关文章推荐
- 数据结构——顺序栈与链栈的C++实现
- 【数据结构】顺序栈(顺序表动态实现)
- 数据结构中关键路径算法的实现与应用
- 四则运算实现(c 语言 数据结构课程设计题)
- 数据结构(严蔚敏)抽象数据类型Triplet的表示与实现(第一章)
- 基于关系数据库系统链式存储的树型结构数据,求某结点下的子树所有结点算法(t-sql语言实现)
- 数据结构——简单Hash Table实现
- 数据结构中关键路径算法的实现与应用
- 实现了一个SQL查询整个树状结构数据的方法!找更好的!
- 数据结构中关键路径算法的实现与应用
- 数据结构课程设计-----用C#实现双向链表
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- 树型结构数据在关系数据库中用链式存储相关查询实现
- 级联数据的树状存储结构HashMap实现
- castor + xml schema 实现webservice中复杂结构数据传输
- 数据结构课程设计-----用C#实现双向链表
- 数据结构之:线性表的顺序表示和实现