• 栈的应用


    // 栈.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;
    }
  • 相关阅读:
    Socket规划(1)
    hdu 2391 Filthy Rich
    UDP议定书图像高速传输无损失程序
    C# 通过豆瓣网络编程API获取图书信息
    OS调度算法常用摘要
    mysql回想一下基础知识
    2015第37周三
    2015第37周二
    2015第37周一
    2015第37周一struts2 jstl 标签
  • 原文地址:https://www.cnblogs.com/menghuizuotian/p/3780669.html
Copyright © 2020-2023  润新知