zoukankan      html  css  js  c++  java
  • 顺序栈的初始化,建立,插入,查找,删除。

    ---

    顺序栈:普通数组保存方式,栈顶(max-1)为满,栈底(-1)为空;

    ////////////////////////////////////////////
    //顺序栈的初始化,建立,插入,查找,删除。//
    //Author:Wang Yong                        //    
    //Date: 2010.8.19                         //
    ////////////////////////////////////////////
     
     
    #include <stdio.h>
    #include <stdlib.h>
     
    #define  MAX 100                //定义最大栈容量
     
    typedef int ElemType;
     
    ///////////////////////////////////////////
     
    //定义栈类型 
    typedef struct
    {
        ElemType data[MAX];
        int top;
    }SeqStack;
     
    ///////////////////////////////////////////
     
    //栈的初始化
     
    SeqStack SeqStackInit()
    {
        SeqStack s;
        s.top = -1;
        return s;
    }
     
    ///////////////////////////////////////////
     
    //判断栈空的算法
     
    int SeqStackIsEmpty(SeqStack s)
    {
        if(s.top == -1)
            return 0;
        else
            return 1;
    }
     
    ///////////////////////////////////////////
     
    //进栈的算法
     
    void SeqStackPush(SeqStack &s,ElemType x)
    {
        if(s.top == MAX-1)              //进栈的时候必须判断是否栈满 
            printf("stack full
    "); 
        s.top++;
        s.data[s.top] = x;
    }
     
    //////////////////////////////////////////
     
    //出栈的算法
     
    ElemType SeqStackPop(SeqStack &s)
    {
        if(s.top == -1)             //出栈的时候必须判断是否栈空 
            printf("stack empty
    ");
        ElemType x;
        x = s.data[s.top];
        s.top--;
        return x;
    }
     
    //////////////////////////////////////
    int main()
    {
        SeqStack  stack;
        stack = SeqStackInit();
        printf("请输入进栈的元素:"); 
        ElemType x;
        while(scanf("%d",&x) != -1)
        {
            SeqStackPush(stack,x);  
        }
        printf("出栈的结果:"); 
        while(stack.top != -1)
        {
            printf("%d ",SeqStackPop(stack));
        }
        printf("
    ");
        return 0;
    } 

    ---

  • 相关阅读:
    算法---大整数相加
    Openxml入门---Openxm读取Excel数据
    一个有用的网络监控软件
    C#基础---C#如何对Json字符串处理
    C#基础---IComparable用法,实现List<T>.sort()排序
    OpenXml入门---word里面插入图片
    OpenXml入门
    OpenXml入门----给Word文档添加表格
    拼图
    小锤子
  • 原文地址:https://www.cnblogs.com/Ph-one/p/6889683.html
Copyright © 2011-2022 走看看