堆C数组实现
2015-07-05 14:41
417 查看
堆栈是一个最后出来该数据结构.
栈的基本操作包含:入栈,出栈,初始化栈,清空栈,遍历栈.
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=-1; } //入栈 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+1); scanf("%d",&value); S->Data[S->top]=value; } } //出栈 void PopStackValue(Stack *S) { if(S->top>=0) { printf("the stack %dst value pop out: %d\n",S->top+1,S->Data[--S->top]); } else { printf("The Stack is empty\n"); } } //推断栈空 void IsEmpty(Stack *S) { if(S->top==-1) { printf("The Stack is empty.\n"); } else { printf("The stack is not empty.\n"); } } //清空栈 void ClearStack(Stack *S) { S->top=-1; } //遍历栈 void ScanStack(Stack *S) { int i; int len=S->top-1; int StackArray[len]; for(i=len;i>0;i--) { StackArray[i]=S->Data[i--]; } printf("The all stack member(from top to bottom) is:\n"); while(len>=0) { 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); }
执行结果例如以下:
转载请注明:刘
相关文章推荐
- qsort详解
- sqlite3 数据库使用
- 查看最近的增删改查操作
- 快速samb服务器搭建
- css3 text-shadow
- 五大移动GPU厂商
- dstat
- Spark源码阅读笔记(RDD)(一)
- emacs 搭建racket开发环境
- 汇编实验一 查看CPU和内存,用机器指令和汇编指令编程
- 解析JS运动
- ACM题目之颠倒的价牌
- HDU 1026 Ignatius and the Princess I(bfs +记录路径)
- java JTextArea的简单实例
- 常用http状态码
- UIColor和UIImage转换
- 替身杀手——写进37生日
- iOS UICollectionView基础
- HDU 2028 Lowest Common Multiple Plus
- cocos2D-X从的源代码的分析cocos2D-X学习OpenGL(1)----cocos2D-X渲染架构