Java教程

共享堆栈

本文主要是介绍共享堆栈,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#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& 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;
}

 

这篇关于共享堆栈的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!