• 顺序栈


    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);

  • 相关阅读:
    使用IOCP完成端口队列做任务队列
    对Zlib单元进行再封装
    XmlReader/XmlWriter 类
    TextReader/TextWriter 的类
    LINQ to XML
    Xml序列化
    动态Linq(结合反射)
    设计模式总结
    深入了解.Net上下文
    应用程序域
  • 原文地址:https://www.cnblogs.com/saimeco/p/5141335.html
Copyright © 2020-2023  润新知