两个栈共享一个数组空间(P106Q4)

设两个栈共享一个数组空间,试写出两个栈公用的

读栈顶元算法elemtp top_dustack(dustacktp ds;,int i),

进栈操作算法void push_dustack (dustacktp ds, int i, elemt x),

及出栈算法elemtp pop_dustack(dustacktp ds, int i)

 其中i的取值是1或2,用以指示栈号。

代码:

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
#define FMT "%d"

typedef struct stack {
    ElementType* element; // 储存元素的顺序表
    int maxSize; // 最大储存数量
    int top[2]; // 储存两个栈的栈顶元素位置
}dustacktp;

// 对栈进行初始化
dustacktp* init(dustacktp* ds, int maxSize);
// 读取栈顶元素
ElementType top_dustack(dustacktp* ds, int i);
// 入栈
int push_dustack(dustacktp* ds, int i, ElementType x);
// 出栈
ElementType pop_dustacktp(dustacktp* ds, int i);
// 释放空间
void destory(dustacktp* ds);

// 测试函数
void test(void);


int main(int argc, char const *argv[]) {

    test();

    system("pause");
    return 0;
}


void test(void) {
    dustacktp ds;
    int maxSize = 50;
    init(&ds, maxSize);

    // 对测试的栈进行入栈操作
    // 将10, 20, 30 ...交错入栈
    ElementType t = 10;
    for (int i = 0, flag = 1; i < 100; i++, t += 10, flag = 3 - flag) {
        push_dustack(&ds, flag, t);
    }

    // 对1号栈进行出栈
    while (top_dustack(&ds, 1) != -1)  {
        printf(FMT, pop_dustacktp(&ds, 1));
        if (top_dustack(&ds, 1) != -1) {
            printf(" - ");
        }
    }
    printf("\n");
    
    // 对2号栈进行出栈
    while (top_dustack(&ds, 2) != -1)  {
        printf(FMT, pop_dustacktp(&ds, 2));
        if (top_dustack(&ds, 2) != -1) {
            printf(" - ");
        }
    
    }
    printf("\n");
    
    destory(&ds);
}

dustacktp* init(dustacktp* ds, int maxSize) {
    if ((ds->element = (ElementType*)malloc(sizeof(ElementType) * maxSize)) == NULL) {
        return NULL;
    }
    ds->maxSize = maxSize;
    ds->top[0] = 0;
    ds->top[1] = maxSize - 1;
    return ds;
}

ElementType top_dustack(dustacktp* ds, int i) {
    i--;
    if (i == 0) {
        if (ds->top[0] == 0) {
            return -1;
        }
        return ds->element[ds->top[0] - 1];
    }
    else {
            if (ds->top[1] == ds->maxSize - 1) {
                return -1;
            }
            return ds->element[ds->top[1] + 1];
    }
}

int push_dustack(dustacktp* ds, int i, ElementType x) {
    i--;
    if (ds->top[0] + 1 == ds->top[1]) {
        return -1;
    }
    
    if (i == 0) {
        ds->element[ds->top[0]++] = x;
    }else {
        ds->element[ds->top[1]--] = x;
    }
    return 0;
}

ElementType pop_dustacktp(dustacktp* ds, int i){
    i--;
    if (i == 0) {
        if (ds->top[0] == 0) {
            return -1;
        }
        return ds->element[--ds->top[0]];
    }else {
        if (ds->top[1] == ds->maxSize - 1) {
                return -1;
            }
        return ds->element[++ds->top[1]];
    }
}

void destory(dustacktp* ds) {
    free(ds->element);
}

运行结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值