2024/10/9日 日志

今天进行了离散数学中关于并包的知识,通过对之前关系内容的复习和今天知识的学习,对三种并包形式,即自反r,对称s,传递t的实现有了规律总结,即RIr即(如包含x,x),RIk即(如包含y,x),以及R*R+即(R+R2+R3+.....+Rn)。
此外,在数据结构的学习中,继续学习了关于栈以及队列的内容,并对双栈公用的代码进行了实现。

点击查看代码
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

// 定义栈结构
typedef struct {
    int *stackArray;
    int top1;
    int top2;
} DoubleStack;

// 初始化双栈
void initDoubleStack(DoubleStack *ds, int size) {
    ds->stackArray = (int *)malloc(size * sizeof(int));
    ds->top1 = -1;
    ds->top2 = size;
}

// 销毁双栈
void destroyDoubleStack(DoubleStack *ds) {
    free(ds->stackArray);
    ds->stackArray = NULL;
    ds->top1 = -1;
    ds->top2 = 0;
}

// 判断栈1是否为空
bool isStack1Empty(DoubleStack *ds) {
    return ds->top1 == -1;
}

// 判断栈2是否为空
bool isStack2Empty(DoubleStack *ds) {
    return ds->top2 == MAX_SIZE;
}

// 压栈1
bool pushStack1(DoubleStack *ds, int value) {
    if (ds->top1 + 1 == ds->top2) {
        // 栈满
        return false;
    }
    ds->top1++;
    ds->stackArray[ds->top1] = value;
    return true;
}

// 压栈2
bool pushStack2(DoubleStack *ds, int value) {
    if (ds->top1 + 1 == ds->top2) {
        // 栈满
        return false;
    }
    ds->top2--;
    ds->stackArray[ds->top2] = value;
    return true;
}

// 弹栈1
bool popStack1(DoubleStack *ds, int *value) {
    if (isStack1Empty(ds)) {
        return false;
    }
    *value = ds->stackArray[ds->top1];
    ds->top1--;
    return true;
}

// 弹栈2
bool popStack2(DoubleStack *ds, int *value) {
    if (isStack2Empty(ds)) {
        return false;
    }
    *value = ds->stackArray[ds->top2];
    ds->top2++;
    return true;
}

// 查看栈1的栈顶元素
bool peekStack1(DoubleStack *ds, int *value) {
    if (isStack1Empty(ds)) {
        return false;
    }
    *value = ds->stackArray[ds->top1];
    return true;
}

// 查看栈2的栈顶元素
bool peekStack2(DoubleStack *ds, int *value) {
    if (isStack2Empty(ds)) {
        return false;
    }
    *value = ds->stackArray[ds->top2];
    return true;
}
posted @   Moonbeamsc  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
返回顶端
点击右上角即可分享
微信分享提示