zoukankan      html  css  js  c++  java
  • 顺序栈

    1.类型定义:

    typedef int ElemType;
    typedef struct{
      ElemType *elem;
      int top;
      int size;
      int increment;
      }SqStack;
    View Code

    调用:SqStack S;

    栈窗口:

    2.初始化:

    //初始化顺序栈
    Status InitStack_Sq(SqStack &S,int size,int inc){
      S.elem=(ElemType*)malloc(size*sizeof(ElemType));
      if(S.elem==NULL)return OVERFLOW;
      S.top=0;//置S为空栈
      S.size=size;
      S.increment=inc;
      return OK;
      }
    View Code

    调用:InitStack_Sq(S,10,5);

    栈窗口:

    对应分配的存储空间:

    3.入栈

    //顺序栈元素入栈
    Status Push_Sq(SqStack &S,ElemType e){
      ElemType *newbase;
      if(S.top>=S.size){
        newbase=(ElemType*)realloc(S.elem,(S.size+S.increment)*sizeof(ElemType));
        if(newbase==NULL)return OVERFLOW;
        S.elem=newbase;
        S.size+=S.increment;
      }
      S.elem[S.top++]=e;
      return OK;
      }
    View Code

    调用:

    1 int a[12]={2,3,6,9,8,7,4,0,5,1,41,45};
    2   for(int i=0;i<12;i++){
    3     Push_Sq(S,a[i]);
    4     }
    View Code

    栈窗口:

    此时i=0未把元素压入栈

    当把S.size个元素压入栈后:                   存储单元变化:

                          

    当压入个数大于S.size后

                  

    4.销毁:

    1 //销毁顺序栈
    2 Status DestroyStack_Sq(SqStack &S){  
    3   free(S.elem);
    4   S.elem==NULL;
    5   printf("顺序栈已销毁
    ");
    6   }
    View Code

     调用:DestroyStack_Sq(S);

  • 相关阅读:
    yarn安装ant-报错
    Linux扩展分区记录
    转载--tomcat调优
    转发:tomcat的acess_log打印post请求参数,分析日志
    经纬度差和米单位的换算
    loadrunner 11 安装与使用
    前端知识图谱
    linux-nc命令介绍
    双网卡设置(转:https://www.cnblogs.com/visionfeng/p/5825078.html)
    网络设备介绍
  • 原文地址:https://www.cnblogs.com/saimeco/p/5141335.html
Copyright © 2011-2022 走看看