zoukankan      html  css  js  c++  java
  • 栈的应用

    // 栈.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    typedef char ElemType;
    #define MAX_INIT_SIZE 100
    struct SeqStack{
        ElemType data[MAX_INIT_SIZE];
        int top;
    };
    //初始化栈
    SeqStack *InitStack()
    {
       SeqStack *stack=new SeqStack;
       stack->top=-1;    //置空栈的运算就是将stack->top置为-1;
       return stack;
    }
    //判断是否为空
    bool StackEmpty(SeqStack *stack)
    {
        if(stack->top>=0)
            return false;
        else
            return true;
    }
    //判断是否已满
    bool StackFull(SeqStack *stack)
    {
       if(stack->top<MAX_INIT_SIZE-1&&stack->top>=0)
           return false;
       else
           return true;
    }
    //push
    void Push(SeqStack *stack,ElemType e)
    {
       //始终记得要对栈的空 满进行判断
        if (stack->top>=-1&&stack->top<MAX_INIT_SIZE)
        {
            stack->top++;
            stack->data[stack->top]=e;
        }
        else
            cout<<"error"<<endl;
    }
    //Pop
    void Pop(SeqStack *stack,ElemType &e)
    {
        if (stack->top>=0)  //top =0 是第一个储存位置
        {
            e=stack->data[stack->top];
            stack->top--;
        }
        else
            cout<<"error"<<endl;
    }
    
    
    int main(int argc, char* argv[])
    {
        cout<<"初始化栈!"<<endl;
        SeqStack *S;
        S=InitStack();
        cout<<"请输入入栈元素,并按<ENTER>结束:"<<endl;
        ElemType ch;
        ch=getchar();
        while(ch!='
    ')
        {
            Push(S,ch);
            ch=getchar();
        }
        while(S->top>=0)
        {
            Pop(S,ch);
            cout<<ch<<"  ";
        }
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    makefile简单例子
    js归并排序
    js插入排序
    js堆排序
    js选择排序
    js冒泡算法以及优化
    使用go语言判断不同数据类型
    go使用接口案例排序
    go接口使用案例晓demo
    go面向对象-继承
  • 原文地址:https://www.cnblogs.com/menghuizuotian/p/3780669.html