C语言 栈的基本操作 栈的实现
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 10
#define OK 1
#define ERROR -1
#define STACKINCREMENT 10/*存储空间分配增量*/
typedef int ElemType;
typedef int Status;
栈的结构体
typedef struct SqStack
{int top;int bottom;ElemType array[STACK_SIZE];
}SqStack;
栈的初始化
Status Init_Stack(SqStack *S)
{S->top = S->bottom =0;return OK;
}
入栈
Status Push(SqStack *S, ElemType e)//e为待压入的元素
{int a = 0;if(S->top - S->bottom == STACK_SIZE - 1){printf("This stack is full!\n\n");a = ERROR;}else{S->array[S->top] = e;S->top++;a = OK;}return a;
}
出栈
Status Pop(SqStack *S)int e;if(S->bottom == S->top)printf("This stack is empty!\n\n");e = S->array[(S->top) - 1];S->top--;return e;
}
获取栈顶元素
Status GetTop(SqStack *S)
{int e;if(S->bottom == S->top){printf("The stack is empty!\n\n");}else{S->top--;e = S->array[S->top];S->top++;S->array[S->top] = e;}return e;
}
遍历栈
Status StackLength(SqStack *S)
{int i;int len = 0;if(S->top == S->bottom)printf("This stack is empty!\n\n");else{for(i = 0; i < S->top; i++)len++;}return len;
}
清空栈
Status ClearStack(SqStack *S)
{S->top = S->bottom;return OK;
}
输出栈
void OutPut_Stack(SqStack *S)
{int i;int len = 0;int j = 0;if(S->top == 0)printf("This stack is empty!\n\n");for(i = 0; i < S->top; i++)len++;j = len;for(i = len - 1; i >= 0; i--){printf("第%d个元素为:%d\n\n",j ,S->array[i]);j--;}
}
在主函数中调用以上函数
int main()
{int i;int a;int j = 0;SqStack S;int res1 = 0;//初始化栈的返回值int res2 = 0;//压入栈的返回值int res3 = 0;//获取栈顶元素的返回值int res4 = -1;//弹出栈顶元素的返回值int res6 = 0;//清除栈的返回值printf("1.初始化一个栈\n2.压入一个元素\n3.弹出栈顶元素\n4.获取栈的长度\n5.输出站\n6.获取栈顶元素\n7.清空栈\n");while(1){printf("请输入操作编号:");scanf("%d", &i);printf("\n");if(i == 1){//初始化一个栈res1 = Init_Stack(&S);if(res1 == 1)printf("初始化成功\n\n");elseprintf("初始化失败\n\n");res2 = 0;j = 1;}else if(i == 2){//压入元素if(j == 0){printf("Error!\n");printf("Please initialize a stack first!\n");}else{printf("请插入数据:");scanf("%d", &a);printf("\n");res2 = Push(&S, a);if(res2 == 1)printf("元素压入成功\n\n");elseprintf("元素压入失败\n\n");res2 = 0;}}else if(i == 3){if(j == 0){printf("Error!\n");printf("Please initialize a stack first!\n");}else{printf("\n-----------弹出栈顶元素-----------\n\n");res4 = Pop(&S);if(res4 != -1){printf("栈顶元素弹出成功\n");printf("栈顶元素为:%d\n", res4);}}}else if(i == 4){if(j == 0){printf("Error!\n");printf("Please initialize a stack first!\n");}else{printf("\n-----------获取栈的长度-----------\n\n");StackLength(&S);}}else if(i == 5){if(j == 0){printf("Error!\n");printf("Please initialize a stack first!\n");}else{printf("\n-----------输出栈-----------\n\n");OutPut_Stack(&S);}}else if(i == 6){if(j == 0){printf("Error!\n");printf("Please initialize a stack first!\n");}else{printf("\n-----------获取栈顶元素-----------\n\n");res3 = GetTop(&S);printf("栈顶元素为:%d\n", res3);}}else if(i == 7){if(j == 0){printf("Error!\n");printf("Please initialize a stack first!\n");}else{printf("\n-----------清空栈-----------\n\n");//清空栈res6 = ClearStack(&S);if(res6 == OK)printf("清除成功\n");elseprintf("清除失败\n");}}else{printf("操作编码错误\n请重新输入\n");}}return 0;
}
以上就是栈的操作的基本实现,希望能帮到大家
C语言 栈的基本操作 栈的实现相关推荐
- 数据结构源码笔记(C语言):栈的基本操作
#include <stdio.h> #define MaxSize 100typedef char ElemType;typedef struct {char stack[MaxSize ...
- c语言堆栈基本代码入栈出栈_链栈基本操作(入栈和出栈)C语言详解
链,即用 链栈的实现思路同 图 1 链栈示意图 将链表头部作为栈顶的一端,可以避免在实现数据 "入栈" 和 "出栈" 操作时做大量遍历链表的耗时操作. 链表的头 ...
- 数据结构:顺序栈基本操作(入栈和出栈)C语言详解(转载)by解学武
本文为解学武教程的免费章节 什么是顺序栈 顺序栈,即用顺序表实现栈存储结构.通过前面的学习我们知道,使用栈存储结构操作数据元素必须遵守 "先进后出" 的原则,本节就 "如 ...
- 顺序栈基本操作(入栈和出栈)C语言详解
#include <stdio.h> #include <stdlib.h> /*顺序栈基本操作(入栈和出栈)C语言详解栈的具体实现(1)顺序栈(2)链栈栈的应用(1)回退 ( ...
- c语言顺序栈完整程序,顺序栈的基本操作(C语言)
由于现在只学了C语言所以就写这个C语言版的栈的基本操作 这里说一下 :网上和书上都有这种写法 int InitStack(SqStack &p) &p是取地址 但是这种用法好像C并不 ...
- c语言堆栈基本代码入栈出栈_顺序栈基本操作(入栈和出栈)C语言详解
顺序,即用{1,2,3,4},存储状态如 图 1 顺序表存储 {1,2,3,4} 同样,使用栈存储结构存储 {1,2,3,4},其存储状态如图 2 所示: 图 2 栈结构存储 {1,2,3,4} 通过 ...
- 链栈的基本操作-C语言
链栈的基本操作 用链表实现一个栈 具体实现 定义一个链栈 //定义一个链栈 typedef struct LNode {int data;struct LNode* next; }LNode, *Li ...
- C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
//顺序栈的基本操作 #include<stdio.h> #define MaxSize 50 typedef int ElemType; //定义栈结构体 typedef struct ...
- C语言-实现栈的基本操作(顺序栈)
下面用两种方式来构建顺序栈,分别是将top定义为指针类型和将top定义成指针下标两种形式,实现栈的基本操作. 目录 方法一: 1.1结构定义 1.2 完整代码 1.3测试用代码(用来逐步测试以上栈功能 ...
最新文章
- 残缺的完美 送给飘
- linux ie8找不到,ie浏览器不见了怎么办 三种方法搞定【图文教程】
- Dubbo 跨语言调用神兽:dubbo-go-pixiu
- abap 导入队列末尾_在C#中将对象添加到队列的末尾-排队操作
- 获取线程名称 java 1615387415
- c++ 读文件_python中文件的使用
- java 中的集合_JAVA中的集合
- 一个完整的Java Web项目需要掌握哪些技术
- c语言linux系统宏,Linux下C语言中的预定义宏
- ceph的 cache tier实现分析
- matlab误码率理论,QDPSK理论误码率与实际误码率matlab仿真程序
- 对话知名视觉艺术设计师走尺:只要用心 人人是插画师
- 湖南工程学院CSDN高校俱乐部简介
- Activity销毁onStop或onDestroy延时10s左右才回调
- 被 GPT 带火的 Prompt 是什么?
- CCS中的IER和IFR寄存器:Symbol ‘IER‘ could not be resolved
- 门铃质检报告办理快速发证
- Simulink 环境基础知识(十八)--确定信号维度
- web渗透测试----21、网站备份文件泄露
- 码头船只货柜管理系统(Java+SSH+MySQL)