链式栈的基本操作与实现
来源:互联网 发布:vb调用matlab工具箱 编辑:程序博客网 时间:2024/05/17 04:01
1:头文件:SqStack.h#include<stdio.h>#include<stdlib.h>#define ElemType int#define STACK_SIZE 100#define ADD_SIZE 10typedef struct SqStack{ElemType *base;ElemType *top;int stacksize;}SqStack;bool Isempty(SqStack *s); //栈是否为空bool IsFull(SqStack *s); //判栈是否已满void InitStack(SqStack *s); //初始化栈void PushStack(SqStack *s,ElemType x); //入栈void PopStack(SqStack *s,ElemType *x); //出栈void ShowStack(SqStack *s); //显示栈void GetTop(SqStack *s,ElemType *x);//获取栈顶数据void ClearStack(SqStack *s); //清理栈void DestoryStack(SqStack *s); //摧毁栈void Show(); //显示菜单
2:功能实现文件SqStack.cpp#include"SqStack.h"void Show(){printf("***************************************\n");printf("* [1]:入栈 [2]:出栈 *\n");printf("* [3]:显示栈 [4]:获取栈顶数据*\n");printf("* [5]:摧毁栈 [6]:清理栈 *\n");printf("* [0]:结束程序 *\n");printf("***************************************\n");printf("请选择:");}void InitStack(SqStack *s){s->base=(ElemType*)malloc(STACK_SIZE*sizeof(ElemType));if(s->base==NULL){printf("开辟空间失败\n");return ;}s->top=s->base;s->stacksize=STACK_SIZE;}bool IsFull(SqStack *s){return s->top-s->base>=s->stacksize;}bool Isempty(SqStack *s){return s->base==s->top;}void PushStack(SqStack *s,ElemType x){if(IsFull(s)){s->base=(ElemType*)realloc(s->base,(s->stacksize+ADD_SIZE)*sizeof(ElemType));if(s->base==NULL){printf("增加空间失败\n");return;}s->top=s->base+s->stacksize;s->stacksize+=ADD_SIZE;}*s->top=x;s->top++;}void PopStack(SqStack *s,ElemType *x){if(Isempty(s)){printf("栈为空,不能出栈\n");return;}s->top--;*x=*s->top;}void GetTop(SqStack *s,ElemType *x){if(Isempty(s)){printf("栈为空,不能获取栈数据\n");return;}*x=*(s->top-1);}void ClearStack(SqStack *s){s->base=s->top=NULL;s->stacksize=0;}void DestoryStack(SqStack *s){free(s->base);ClearStack(s);}void ShowStack(SqStack *s){ElemType *top=s->top;while(top!=s->base){top--;printf("%d ",*top);}printf("\n");}
3:主文件main.cpp#include"SqStack.h"void main(){SqStack s;ElemType x;int pos=1;InitStack(&s);while(pos){Show();scanf("%d",&pos);switch(pos){case 1:printf("输入数据,以-1结束:");while(scanf("%d",&x),x!=-1){PushStack(&s,x);}break;case 2:PopStack(&s,&x); printf("出栈数据:%d\n",x);break;case 3:ShowStack(&s);break;case 4:GetTop(&s,&x);printf("获取栈顶数据:%d\n",x);break;case 5:DestoryStack(&s);printf("栈已被摧毁\n");break;case 6:ClearStack(&s);printf("栈已被清理\n");break;default:break;case 0:return;}}}
0 0
- 链式栈的基本操作与实现
- 链式队列的基本操作与实现
- 链式栈的基本操作实现c++
- 链式栈的基本操作
- 链式栈的基本操作
- 链式栈的基本操作
- 链式栈的基本操作
- 链式队列的基本操作实现c++
- 链式栈基本操作
- 栈的链式存储的实现与操作
- 链式栈的基本操作code_legend
- 【队列】链式结构实现队列的基本操作
- 链式结构实现线性表的基本操作
- 链式队列的基本操作
- 链式队的基本操作
- 线性表的链式存储与基本操作
- 栈的链式存储 c实现的几种基本操作
- 重学数据结构003——栈的基本操作及实现(链式存储)
- tslib移植-selected device is not a touchscreen I understand 错误解决
- Try...Catch...基础用法
- ABS(Android Build System)中在编译时生成源代码文件
- bean:write 中filter的作用
- Python进阶
- 链式栈的基本操作与实现
- html的字体名字(英文)
- sys系统的kset和kobject代码示例
- js运动-任意值添加运动
- jni使用注意
- POJ 3678 Katu Puzzle 2-SAT
- leetcode #5 Longest Palindromic Substring
- C学习笔记--指针(更新中)
- STL学习记录(六):List