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

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

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

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

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

题目一:用栈实现队列

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

在这里我们我们直接使用前面我们实现过的栈代码在这里:(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进行比较,这是一个好办法,还有另一个常用的方法就是创建数组时就多创建一个位置,这样就将满和空区分开了。在程序中我们要始终想着可能的特殊情况,运用好取模的操作。详细请看下面的代码。

代码展示:

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);
}

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

  • 29
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值