栈的C数组实现
栈是一种先进后出的数据结构.栈的基本操作包括:入栈,出栈,初始化栈,清空栈,遍历栈.
C代码如下:
#include <stdio.h> #define MaxSize 20
typedef int ElemType; typedef struct stack
{
ElemType Data[MaxSize];
int top;
}Stack; //初始化栈
void InitStack(Stack *S)
{
S->top=-;
} //入栈
void PushStackValue(Stack *S)
{
printf("Input the Value of stack member:\n(0-exit)\n");
int value;
printf("Please input the 1st value of stack:\n");
scanf("%d",&value);
S->Data[++S->top]=value;
while(value)
{
S->top++;
printf("Please input the %dst value of stack:\n",S->top+);
scanf("%d",&value);
S->Data[S->top]=value;
}
} //出栈
void PopStackValue(Stack *S)
{
if(S->top>=)
{
printf("the stack %dst value pop out: %d\n",S->top+,S->Data[--S->top]);
}
else
{
printf("The Stack is empty\n");
}
} //判断栈空
void IsEmpty(Stack *S)
{
if(S->top==-)
{
printf("The Stack is empty.\n");
}
else
{
printf("The stack is not empty.\n");
}
} //清空栈
void ClearStack(Stack *S)
{
S->top=-;
} //遍历栈
void ScanStack(Stack *S)
{
int i;
int len=S->top-;
int StackArray[len];
for(i=len;i>;i--)
{
StackArray[i]=S->Data[i--];
}
printf("The all stack member(from top to bottom) is:\n");
while(len>=)
{
printf("%d ",S->Data[len--]);
}
printf("\n");
} void main()
{
Stack S; InitStack(&S);
PushStackValue(&S);
ScanStack(&S);
IsEmpty(&S);
PopStackValue(&S);
PopStackValue(&S);
PopStackValue(&S);
PopStackValue(&S);
}
运行结果如下:
栈的C数组实现的更多相关文章
- 栈ADT的数组实现
/* 栈的数组实现声明 */ struct StackRecord; typedef struct StackRecord *Stack; #define MinSstackSize 5 #defin ...
- C#编程(七十六)----------使用指针实现基于栈的高性能数组
使用指针实现基于栈的高性能数组 以一个案例为主来分析实现方法: using System; using System.Collections.Generic; using System.Linq; u ...
- C语言提高 (3) 第三天 二级指针的三种模型 栈上指针数组、栈上二维数组、堆上开辟空间
1 作业讲解 指针间接操作的三个必要条件 两个变量 其中一个是指针 建立关联:用一个指针指向另一个地址 * 简述sizeof和strlen的区别 strlen求字符串长度,字符数组到’\0’就结束 s ...
- 内存的堆分配和栈分配 & 字符数组,字符指针,Sizeof总结
堆和栈的区别 一个由C/C++编译的程序占用的内存分为以下几个部分1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈.2.堆区(heap ...
- java中的栈(利用数组实现栈)
java中的栈(利用数组实现栈) 常见的数据结构:https://blog.csdn.net/weixin_43304253/article/details/119764275 栈的介绍 1.栈的英文 ...
- Java栈的实例-数组和链表两种方法(转)
一.栈 栈的定义 栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表. (1)通常称插入.删除的这一端为栈顶 (Top),另一端称为栈底 (Bottom). (2)当表中没有元素时称为空栈. ...
- 牛客小白月赛13-H(单调栈+树状数组)
题目链接:https://ac.nowcoder.com/acm/contest/549/H 题意:给一个柱状图,包括每个矩阵的宽度和高度,求能组成的最大矩阵的面积. 思路:显然最大矩阵的高一定为n个 ...
- C++返回栈上的数组(局部变量)问题探索
char* teststr() { char s[] = "hello"; return s; } void main() { char* str = teststr(); ]; ...
- CF1556E-Equilibrium【栈,树状数组】
正题 题目连接:https://codeforces.com/contest/1556/problem/E 题目大意 两个长度为\(n\)的序列\(a,b\),\(q\)次询问一个区间\([l,r]\ ...
随机推荐
- uestc poj2559 秋实大哥去打工
//感觉有必要把这题放博客上待复习 刚刚写解题报告的时候发现自己又不会做这题了 //我不会告诉你这题绝对是命题人抄poj2559 这题使用一个单调递增的栈,栈内存储的元素有两个值,一个高度,一个长度. ...
- UESTC_邱老师选妹子 2015 UESTC Training for Dynamic Programming<Problem H>
H - 邱老师选妹子 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- ZOJ2913Bus Pass(BFS+set)
Bus Pass Time Limit: 5 Seconds Memory Limit: 32768 KB You travel a lot by bus and the costs of ...
- Mod_python: The Long Story
mod_python: the long story - Grisha Trubetskoy Mod_python: The Long Story Oct 25th, 2013 | Comments ...
- AS3聊天单行输入框图文混排完美实现
几年前刚毕业.第一个游戏模块做的就是聊天.到如今.几个游戏写过几次聊天模块. 之前在4399做的<幻龙骑士>(又名<神骑士>),还有上周六刚上线的<疯狂的子弹>, ...
- Ubuntu亮度无法调节或调节无法保存的问题
装了搜狗输入法之后,系统设置里面的很多软件都没有了.以前屏幕太亮在电源里面可以调节,现在不行了.没办法,只能找其他的办法了. 在网上查了很多资料,经自己的实验,找到了一个成功的方法. 首先进入 /sy ...
- zepto的touch.js左右滑动存在一些问题,用百度的touch.js代替
这几天用zepto想写一个移动端的活动,在实现左右滑动触发动画时,发现zepto的touch.js在ios的微信上有问题. 问题描述:左右滑动时如果手指没有一直跟频幕贴着(在手机上滑动时,如果手指不是 ...
- CSS的base文件常用代码
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body,but ...
- 用户"IIS APPPOOL\xxxxxxxx"登录失败解决方案
Server Error in '/' Application.-------------------------------------------------------------------- ...
- differ比较两个字符串的差异
"abcde","abdefk" ---->-c,+f,+k "aba","aababb" -----&g ...