C语言 栈和队列力扣刷题—用栈实现队列,循环队列

2024-06-10 1484阅读

前面我发布了栈和队列的相关博客

(C语言)栈与解题应用:(C语言)栈与解题运用-CSDN博客

(C语言)(C语言)队列实现与用队列实现栈:(C语言)队列实现与用队列实现栈-CSDN博客

 其中第二篇博客中有一题是用队列实现栈与下面的题目一类似,思路也类似,可以对照一起学习

题目一:用栈实现队列

链接:232. 用栈实现队列 - 力扣(LeetCode)

C语言 栈和队列力扣刷题—用栈实现队列,循环队列

在这里我们我们直接使用前面我们实现过的栈代码在这里:(C语言)栈与解题运用-CSDN博客

解题思路:

要用栈的后进先出实现队列的先进先出,我们可以创建两个栈进行数据之间的相互交换,实现这一效果。其中st1为主要栈,st2为次要栈(数据出队列时进行数据的临时放置),入队列时只需将数据入栈到我们的主栈中,出队列时将st1中top-1个数据转入st2中(倒序转入),此时st中剩下的一个数据是我们要出的数据,出来后,再将st2中的数据全部转入st1中(还是倒序转入,两次转数据都是倒序,最终st1中的数据还是正序的),这样就完成了数据的出队列。这就是最主要的思路。

下面我们来展示代码:

typedef int StDateType;
// 方便以后更改栈的数据类型
typedef struct Stack {
    StDateType* a;
    int top;
    int capacity;
} stack;
// 初始化栈
void STInit(stack* pst) {
    assert(pst);
    pst->a = NULL;
    pst->capacity = pst->top = 0;
}
// 栈的销毁
void STDestroy(stack* pst) {
    assert(pst);
    free(pst->a);
    pst->a = NULL;
    pst->capacity = pst->top = 0;
}
// 入栈
void STPush(stack* pst, StDateType x) {
    assert(pst);
    if (pst->top == pst->capacity) {
        // capacity为空时newcapacity为4,不为空时扩大为原来的二倍
        int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
        StDateType* temp =
            (StDateType*)realloc(pst->a, sizeof(StDateType) * newcapacity);
        // 当capacity为0,a==NULL时,注意realloc当传递的指针为空时,作用相当于malloc
        if (temp == NULL) {
            perror("realloc failed");
            return;
        }
        pst->a = temp;
        pst->capacity = newcapacity;
    }
    pst->a[pst->top] = x;
    pst->top++;
}
// 出栈
void STPop(stack* pst) {
    // pst不能孔 栈也不能为空
    assert(pst && pst->top);
    pst->top--;
}
// 判空
bool STEmpty(stack* pst) { return pst->top == 0; }
// 栈的长度
int STSize(stack* pst) { return pst->top; }
// 打印栈顶
StDateType STTop(stack* pst) {
    // pst不能孔 栈也不能为空
    assert(pst && pst->top);
    return pst->a[pst->top - 1];
}
typedef struct {
    stack st1;
    stack st2;
} MyQueue;
MyQueue* myQueueCreate() {
    MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
    STInit(&queue->st1);
    STInit(&queue->st2);
    return queue;
}
void myQueuePush(MyQueue* obj, int x) { STPush(&obj->st1, x); }
int myQueuePop(MyQueue* obj) {
    int size = obj->st1.top;
    while (size > 1) {
        STPush(&obj->st2, STTop(&obj->st1));
        STPop(&obj->st1);
        size--;
    }
    int ret = STTop(&obj->st1);
    STPop(&obj->st1);
    size = obj->st2.top;
    while (size > 0) {
        STPush(&obj->st1, STTop(&obj->st2));
        STPop(&obj->st2);
        size--;
    }
    return ret;
}
int myQueuePeek(MyQueue* obj) { return obj->st1.a[0]; }
bool myQueueEmpty(MyQueue* obj) { return STEmpty(&obj->st1); }
void myQueueFree(MyQueue* obj) {
    STDestroy(&obj->st1);
    STDestroy(&obj->st2);
    free(obj);
}

这个题还是比较简单的,下面我们在来看一道稍难点的题目。

题目二:设计循环队列

题目链接:622. 设计循环队列 - 力扣(LeetCode)

解题思路:首先这个循环队列是有固定长度的,我们可以采用数组的方式实现,当然也可以采用循环链表的方式实现,在这里我采用的是数组的方式。如何让数组循环起来是一个问题,可以采用取模的方式来达到循环的效果,循环链表的结构体中我们要定义head和rear分别指头和尾,还有k指队列中的容量,这里和实现栈的有相同的问题,rear不能单纯的指尾而是要指向尾的下一个位置,这样就解决这一问题,但是如何判空和判满呢?判空很简单head==rear时为空,但是满的时候也是head==rear怎么办,我们可以在循环队列结构体中再定义一个size变量来和k进行比较,这是一个好办法,还有另一个常用的方法就是创建数组时就多创建一个位置,这样就将满和空区分开了。在程序中我们要始终想着可能的特殊情况,运用好取模的操作。详细请看下面的代码。

C语言 栈和队列力扣刷题—用栈实现队列,循环队列

代码展示:

typedef struct {
    int* a;
    int k;//队列的容量
    int head;//头
    int rear;//尾的下一个
} MyCircularQueue;
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->head == obj->rear;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return (obj->rear + 1) % (obj->k + 1) == obj->head;
}
MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* queue = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    queue->a = (int*)malloc(sizeof(int) * (k + 1));
    queue->rear = queue->head = 0;
    queue->k=k;
    return queue;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj)) {
        return false;
    } else {
        obj->a[obj->rear++] = value;
        obj->rear %= obj->k + 1;
        return true;
    }
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) {
        return false;
    } else {
        obj->head++;
        obj->head = (obj->head + obj->k + 1) % (obj->k + 1);
        return true;
    }
}
int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) {
        return -1;
    } else {
        return obj->a[obj->head];
    }
}
int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) {
        return -1;
    } else {
        return obj->a[(obj->rear+obj->k)%(obj->k+1)];
        //这里可能有点难理解,代入写特殊情况就明白了为什么这样写了
        //及rear可能等于0,此时rear-1可就等于-1了
    }
}
void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    free(obj);
}

各位看官点个赞再走吧,您的支持是我博客的动力。

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]