栈的基础操作——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; }
相关文章推荐
- C语言学习6 :指针的定义,指针类型要合法,指针要初始化,指针做函数参数,数组和指针的通用性,指针+1所代表的空间,void * 指针,交换函数中的指针,数组和字符型指针区别,字符型指针的应用,使用指针完成字符操作函数
- 黑马程序员--Java基础学习笔记【数组操作、基本数据类型包装类】
- for语句,const定义,数组操作,类型转换,static等注意点
- C语言基础知识之(六):数组-----数组类型、定义数组、读取数组、排序数组、字符串数组函数
- JAVA基础 day23 IO流的其他类 (对象的序列化 管道流 RandomAccessFile类(随机读取文件) DateStream(操作基本数据类型的流) 操作数组的流 )字符编码问题
- [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组
- linux——Shell 脚本基础篇(变量类型,变量操作,定义,运算与逻辑关系)
- 语法基础: 使用不同的数据类型标记数组
- SQL2K数据库开发五之表操作管理用户定义的数据类型
- JAVA数组和JAVA集合类的对象操作-Java基础-Java-编程开发
- C#基础数据类型与字节数组(内存中的数据格式)相互转换(BitConverter 类)
- C#基础系列(1)-- 第一部分 基础数据类型与操作 -- 位运算(1)
- C#基础系列(2)-- 第一部分 基础数据类型与操作 -- 位运算(2)
- 数据结构类型定义及基本操作汇总(一)--线性表,单链表,栈和队列
- DELPHI基础:字符串、数组操作函数
- C#中定义数组--字符串及数组操作
- C#基础系列(4)-- 第一部分 基础数据类型与操作 -- 枚举类型与位标志(4)
- 将基础数据类型与字节数组相互转换
- C#基础系列(3)-- 第一部分 基础数据类型与操作 -- 位运算(3)
- (教学思路 C#数组一)认识数组,了解数组类型、学习定义及初始化