0
点赞
收藏
分享

微信扫一扫

【剑指offer】09.用两个栈实现队列

Ichjns 2022-04-13 阅读 61
算法

C语言版本

#define MAX_QUEUE_SIZE 10000


typedef struct {
    int *s1;
    int *s2;
    int top_s1;  // 栈顶为什么是int类型?
    int top_s2;

} CQueue;


CQueue* cQueueCreate()  // 主要做一些内存分配的工作
{
    // 注释部分是我写的
    // CQueue *cq;
    // cq = (CQueue*)malloc(sizeof(int) * MAX_QUEUE_SIZE);
    // cq->s1 = (int*)malloc(sizeof(int))
    // cq->s2 = (int*)malloc(sizeof(int))

    // if(cq->s1 != NULL){
    //     free(cq->s1);
    //     return NULL;
    // }

    // if(cq->s2 != NULL){
    //     free(cq->s2);
    //     return NULL;
    // }

    // return cq;

    CQueue *cq;  // 定义结构体指针
    cq = (CQueue*)malloc(sizeof(CQueue));  // 为结构体分配内存
    if(cq == NULL)  // 我的理解是:如果分配失败,返回NULL。暂时不懂啥意思
    {
        return NULL;
    }

    cq->s1 = (int*)malloc(MAX_QUEUE_SIZE * sizeof(int));  // 为栈s1分配内存
    if(cq->s1 == NULL)  // 如果分配失败,就释放所有分配的内存
    {
        free(cq);
        return NULL;
    }

    cq->top_s1 = -1;

    cq->s2 = (int*)malloc(MAX_QUEUE_SIZE * sizeof(int));  // 为栈s2分配内存
    if(cq->s2 == NULL)
    {
        free(cq);
        return NULL;
    }

    cq->top_s2 = -1;

    return cq;
}

void cQueueAppendTail(CQueue* obj, int value)  // 入队
{
    // if(obj->top_s1 != -1){
    //     obj->s1[++obj->top_s1] = value;
    // }

    if(obj->top_s1 != MAX_QUEUE_SIZE)  // 将新入队的元素压入栈s1
    {
        obj->s1[++obj->top_s1] = value;
    }
}

int cQueueDeleteHead(CQueue* obj)  // 出队
{
    // int top_s1_element = 0;  // 用两个变量表示两个栈的栈顶的元素
    // int top_s2_element = 0;

    // // s1 is not empty, s2 is empty
    // while(obj->top_s1 != -1){
    //     top_s1_element = obj->s1[obj->top_s1--];
    //     obj->s2[++obj->top_s2] = top_s1_element;
    // }

    // if(obj->top_s2 != -1){
    //     top_s2_element = obj->s2[obj->top_s2--];
    //     return top_s2_element;
    // }

    // return -1;

    int top_s1_element = 0;
    int top_s2_element = 0;

    // 栈s2为空,s1不为空,将s1中所有的元素压入s2,再将s2的栈顶元素出队
    if(obj->top_s2 == -1 && obj->top_s1 != -1)
    {
        while(obj->top_s1 != -1)
        {
            // s1中的元素弹出
            top_s1_element = obj->s1[obj->top_s1--];
            // s1中弹出的元素压入s2中
            obj->s2[++obj->top_s2] = top_s1_element;
        }
    }

    // 栈s2不为空,弹出s2中的栈顶元素,注意:这里不管s1是否为空
    if(obj->top_s2 != -1)
    {
        top_s2_element = obj->s2[obj->top_s2--];
        return top_s2_element;
    }

    // 那现在只剩下s1和s2均为空的情况,根据题意,这种情况表示队列中没有元素,直接返回-1
    return -1;
}

void cQueueFree(CQueue* obj)  // 内存回收
{
    // if(obj != NULL){
    //     if(obj->s1 != NULL){free(obj->s1);}
    //     if(obj->s2 != NULL){free(obj->s2);}
    // }
    // free(obj);

    if(obj != NULL)
    {
        if(obj->s1 != NULL)
        {
            free(obj->s1);
        }
        if(obj->s2 != NULL)
        {
            free(obj->s2);
        }
        free(obj);
    }
}

/**
 * Your CQueue struct will be instantiated and called as such:
 * CQueue* obj = cQueueCreate();
 * cQueueAppendTail(obj, value);
 
 * int param_2 = cQueueDeleteHead(obj);
 
 * cQueueFree(obj);
*/
举报

相关推荐

0 条评论