zoukankan      html  css  js  c++  java
  • 顺序栈的构建—数据结构算法

    功能截图:

    部分源码:

    #include<stdio.h> // EOF(=^Z或F6),NULL
    #include<stdlib.h> // srand( ) ,rand( ),exit(n)
    #include<iostream>
    using namespace std;
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2 
    #define MAXSIZE 100 //最大长度
    typedef int SElemType;
    typedef int StackType;
    typedef int Status; 
    typedef struct {
    SElemType *base;
    SElemType *top;
    int stacksize;
    }SqStack;
    
    Status InitStack(SqStack &S){
    S.base =new SElemType[MAXSIZE];
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = MAXSIZE;
    return OK;
    }
    
    Status Push(SqStack &S,SElemType e){
    if(S.top-S.base ==S.stacksize) return ERROR;
    *S.top++=e;
    return OK; 
    }
    
    Status visit(int e){
    printf("%6d",e);
    return OK;
    }
    
    Status StackTravel(SqStack &S,Status (*visit)(int)){
    //对栈用visit()遍历
    while(S.top>S.base){
    visit (*(--S.top));
    } 
    return OK;
    }
    Status Pop(SqStack &S,SElemType &e)
    {
    
    if(S.top == S.base) return ERROR;
    e = *--S.top;
    return OK;
    }
    Status StackLength(SqStack S) {
    //栈顶指针减去栈底指针等于长度,因为栈顶指针指向当前栈顶元素的下一个位置
    return S.top - S.base;
    }
    
    SElemType GetTop(SqStack S){
    if(S.top != S.base){
    return *(S.top -1);
    }
    }
    

      

    注意这里仅为部分代码。获取源码请关注“值南针”微信公众号:可用电脑微信关注或手机关注(要最新版微信pc端)。在电脑方便。直接下载源码。

    点击想要的算法

    点击下载直接就可以用的。

  • 相关阅读:
    NOI2019 I 君的商店
    CF1326F
    APIO2016 划艇
    LeetCode-Remove Nth Node From End of List
    LeetCode-Remove Element
    LeetCode-Remove Duplicates from Sorted List II
    LeetCode-Remove Duplicates from Sorted List
    LeetCode-Unique Paths II
    LeetCode-Unique Paths
    LeetCode-Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/honeynan/p/12215278.html
Copyright © 2011-2022 走看看