共享堆栈

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了共享堆栈脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>             // c 库
#include <stdlib.h>                //maclloc 库
#include <iostream>                // c++ 库

// 有本句 ,下面cout 前面可以没有  std::
using namespace std;
tyPEdef char  ElemType;  //元素数据类型 char 
#define MAXSIZE 10

typedef struct ShareStack {
    ElemType Data[MAXSIZE];
    int top1, top2;
}SS;

void InIT(SS&amp; S) {
    S.top1 = -1;
    S.top2 = MAXSIZE;
}

bool Push(SS& S, bool left, ElemType e) {
    if (S.top1 + 1 == S.top2) {
        //cout << " full!";
        return 0;
    }
    else if (left) {
        S.Data[++S.top1] = e;
    }
    else {
        S.Data[--s.top2] = e;
    }
    return 1;
}

bool Pop(SS& S, bool left, ElemType& e) {
    if (left) {
        if (S.top1 == -1)
            return 0;
        e = S.Data[S.top1--];
    }
    else {
        if (S.top2 == MAXSIZE)
            return 0;
        e = S.Data[S.top2++];
    }
}


void main() {
    SS S;
    Init(S);

    Push(S, 1, 'a');
    Push(S, 1, 'b');
    Push(S, 1, 'c');
    Push(S, 1, 'd');
    Push(S, 1, 'e');
    Push(S, 0, 'A');
    Push(S, 0, 'B');
    Push(S, 0, 'C');
    Push(S, 0, 'D');

    Push(S, 0, 'E');
    Push(S, 0, 'F');
    //测试查看数组
    for (int i = 0; i < MAXSIZE; i++) {
        cout << "n" << i << "  " << S.Data[i];
    }

    ElemType e;
    Pop(S, 1, e);
    cout << endl;
    cout << endl << " Pop  " << e;
    Pop(S, 1, e);
    cout << endl;
    cout << endl << " Pop  " << e;
    Pop(S, 0, e);
    cout << endl;
    cout << endl << " Pop  " << e;
    Pop(S, 0, e);
    cout << endl;
    cout << endl << " Pop  " << e;
    cout << endl;
}

 

脚本宝典总结

以上是脚本宝典为你收集整理的共享堆栈全部内容,希望文章能够帮你解决共享堆栈所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。