您的位置:首页 > 编程语言 > C语言/C++

栈的基础操作——2,8,16进制的入栈——数组类型定义

2014-05-22 15:17 337 查看
#define stack_init_size 100
#define stackincrement  10
typedef int ElemType;
typedef int status;
const status error=0;
const status ok=1;
const status overflow=-2;

const int MAXSIZE = 100;
typedef struct {
ElemType elem[MAXSIZE];
int top;
} SqStack;

status initstack(SqStack &s)
{
//TODO1-------
//栈的初始化,构造一个空栈;
s.top =0;

return ok;
}

status stackempty(SqStack s)
{
//TODO2------
//判断栈是否为空,空返回ok,否则返回error;
if (s.top == 0)  return ok;
else return error;  //根据判断结果修改。
}

status gettop(SqStack s,ElemType &e)
{
//TODO3-------
//取栈顶元素

if (s.top == 0)  return error;
e = s.elem[s.top-1]; //减去一个元素的存储空间

return ok;
}

status push(SqStack &s,ElemType e)
{
//TODO4-------
//判断栈是否满
//入栈,讨论不同的情况(2,8,16进制)

if ( s.top == MAXSIZE )  return error;  // 栈满
s.elem[s.top] = e;
switch(e)
{case 10:	s.elem[s.top]='A';break;
case 11:	s.elem[s.top]='B';break;
case 12:	s.elem[s.top]='C';break;
case 13:	s.elem[s.top]='D';break;
case 14:	s.elem[s.top]='E';break;
case 15:	s.elem[s.top]='F';break;
default:	s.elem[s.top]=e;break;
}

s.top++;        // 或者s.elem[top++] = x;代替这两行

return ok;
}

status pop(SqStack &s,ElemType &e)
{
//TODO5------
//出栈操作
if ( s.top==0 )  return error;

s.top--;
e = s.elem[s.top]; //可用x=s.elem[--top];代替这两行

return ok;
}

status destroystack(SqStack &s)
{    free(s.elem);
return ok;

}

status clearstack(SqStack &s)
{   s.top=0;
return ok;

}

int stacklength(SqStack s)
{    return s.top;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐