学习笔记------数据结构(C语言版)栈的顺序存储
2016-01-19 17:29
555 查看
//SqStack.cpp
//main.cpp
//SqStack.h
//predefined.h
#include "predefined.h" #include "SqStack.h" Status InitStack(SqStack *S) //构造一个空栈 { (*S).base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!(*S).base) exit(OVERFLOW); (*S).top=(*S).base; (*S).stacksize=STACK_INIT_SIZE; return OK; } Status DestroyStack(SqStack *S) //销毁栈S,S不再存在 { free((*S).base); (*S).base=(*S).top=NULL;//避免野指针 (*S).stacksize=0; return OK; } Status ClearStack(SqStack *S) //把S置为空栈 { (*S).top=(*S).base; return OK; } Status StackEmpty(SqStack S) //若栈为空,返回TRUE { if(S.base==S.top) return TRUE; else return FALSE; } int StackLength(SqStack S) //返回栈S长度 { return S.top-S.base; } Status GetTop(SqStack S,SElemType *e) //若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR。 { if(StackEmpty(S)) return ERROR; *e=*((S).top-1); return OK; } Status Push(SqStack *S,SElemType e) //插入元素e为新的栈顶元素 { if(((*S).top-(*S).base)>=(*S).stacksize) { (*S).base=(SElemType *)realloc((*S).base,((*S).stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!(*S).base) exit(OVERFLOW); (*S).top=(*S).base+(*S).stacksize; (*S).stacksize+=STACKINCREMENT; } (*(*S).top)=e; (*S).top++; return OK; } Status Pop(SqStack *S,SElemType *e) //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR. { if((*S).base==(*S).top) return ERROR; *e=*(--(*S).top); return OK; } Status StackTraverse(SqStack S,void (*visit)(SElemType *p)) //从栈底到栈顶依次对每个元素调用函数visit()。一旦visit调用失败,则操作失败 { SElemType *p; p=S.base; while(p!=S.top) { visit(p); p++; } return OK; }
//main.cpp
#include "predefined.h" #include "SqStack.h" void PrintElem(SElemType *p) { printf("%d ",*p); } int main() { SqStack S; Status s; int i,n; SElemType m; printf("Function 1\n★函数Status InitStack(SqStack *S)测试...\n"); s=InitStack(&S); printf("▲初始化顺序栈S: %d (0:失败 1:成功)\n\n",s); printf("Function 2\n★函数StackEmpty(SqStack S)测试...\n"); s=StackEmpty(S); printf("▲S是否为空栈: %d (0:否 1:是)\n\n",s); printf("Function 3\n★函数Push(SqStack *S,SElemType e)测试...\n"); printf("▲请输入准备向S入栈的个数:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("▲请输入La第%d个数:",i); scanf("%d",&m); s=Push(&S,m); printf("▲入栈成功?:%d (1:成功 0:失败)\n",s); } printf("\n"); printf("Function 4\n★函数StackTraverse(SqStack S,void (*visit)(SElemType *p))测试...\n"); printf("▲栈S中的元素为:S={"); StackTraverse(S,PrintElem); printf("}\n\n"); printf("Function 5\n★函数Pop(SqStack *S,SElemType *e)测试...\n"); Pop(&S,&m); printf("▲S栈顶元素\"%d\"出栈...\n",m); printf("▲栈S中的元素为:S={"); StackTraverse(S,PrintElem); printf("}\n\n"); printf("Function 6\n★函数StackLength(SqStack S)测试...\n"); n=StackLength(S); printf("▲栈S的长度为:%d\n\n",n); printf("Function 7\n★函数GetTop(SqStack S,SElemType *e)测试...\n"); GetTop(S,&m); printf("▲S的栈顶元素为:%d\n\n",m); printf("Function 8\n★函数ClearStack(SqStack *S)测试...\n"); printf("▲栈S置空前 :"); StackEmpty(S)?printf("空 栈!\n"):printf("非 空!\n"); ClearStack(&S); printf("▲栈S置空前 :"); StackEmpty(S)?printf("空 栈!\n\n"):printf("非 空!\n\n"); printf("Function 9\n★函数DestroyStack(SqStack *S)测试...\n"); printf("▲栈S销毁前 :"); S.base?printf("存 在!\n"):printf("不存在!\n"); DestroyStack(&S); printf("▲栈S销毁前 :"); S.base?printf("存 在!\n"):printf("不存在!\n"); }
//SqStack.h
//-----栈的顺序存储表示------ #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack; Status InitStack(SqStack *S); Status DestroyStack(SqStack *S); Status ClearStack(SqStack *S); Status StackEmpty(SqStack S); int StackLength(SqStack S); Status GetTop(SqStack S,SElemType *e); Status Push(SqStack *S,SElemType e); Status Pop(SqStack *S,SElemType *e); Status StackTraverse(SqStack S,void (*visit)(SElemType *p)); void PrintElem(SElemType *p);
//predefined.h
#include "stdio.h" #include "stdlib.h" #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status;
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- C#数据结构之顺序表(SeqList)实例详解
- Lua和C语言的交互详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C++基于栈实现铁轨问题
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法