【编程随想录算法训练营第十天 | ● 理论基础 ● 232.用栈实现队列 ● 225. 用队列实现栈】

用栈实现队列

leetcode

代码随想录

栈的实现

  • 前提
    • 只能使用标准的栈操作 push to top, peek/pop from top, size, 和 is empty
    • 可以使用 list 或者 deque(双端队列)来模拟一个栈
    • 一块存储空间
    • 只能通过Top输入输出访问该存储空间
  • 解题思路
    • push:只要数据放进输入栈就好
    • pop:输出栈如果为空,把进栈数据全部导入进来,再从出栈弹出数据;如果输出栈不为空,则直接从出栈弹出数据
    • 判断队列为空:如果进栈和出栈都为空的话,说明模拟的队列为空了
    • 在代码实现的时候,会发现pop() 和 peek()两个函数功能类似,代码实现上也是类似的,可以思考一下如何把代码抽象一下。
#define MAX_SIZE 100

typedef struct {
    int stackIn[MAX_SIZE];
    int stackOut[MAX_SIZE];
    int stackInTop;
    int stackOutTop;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* obj = malloc(sizeof(MyQueue));
    obj -> stackInTop = -1;
    obj -> stackOutTop = -1;
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    obj -> stackIn[++(obj->stackInTop)] = x;
}

int myQueuePop(MyQueue* obj) {
    if (obj -> stackOutTop > -1){
        return obj -> stackOut[(obj->stackOutTop)--];
    }
    else {
        while (obj -> stackInTop > -1){
            obj -> stackOut[++(obj->stackOutTop)] = obj -> stackIn[(obj->stackInTop)--];
        }
        return obj -> stackOut[(obj->stackOutTop)--];
    }
}

int myQueuePeek(MyQueue* obj) {
    int result = myQueuePop(obj);
    obj -> stackOutTop ++; 

    return result;
}

bool myQueueEmpty(MyQueue* obj) {
    return obj -> stackInTop == -1 && obj -> stackOutTop == -1;
}

void myQueueFree(MyQueue* obj) {
    free(obj);
}

用队列实现栈

leetcode

代码随想录

队列的实现

  • 前提
    • 使用队列的标准操作 —— 也就是 push to backpeek/pop from frontsizeis empty
    • 可以使用 list 或者 deque(双端队列)来模拟一个栈队列
    • 一块存储空间
    • 只能通过front索引输出,back索引输入
    • 可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)
  • 解题思路
    • 一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
typedef int QueueData;

typedef struct QueueNode{
    QueueData data;
    struct QueueNode* next;
}QueueNode;


typedef struct {
    QueueNode* front;
    QueueNode* back;
} MyStack;


MyStack* myStackCreate() {
    MyStack* obj = malloc(sizeof(MyStack));
    QueueNode* head = malloc(sizeof(QueueNode));
    head -> next = NULL;
    obj -> front = head;
    obj -> back = head;

    return obj;
}

void myStackPush(MyStack* obj, int x) {
    QueueNode* tmp = malloc(sizeof(QueueNode));
    obj -> back -> next = tmp;
    obj -> back = tmp;
    obj -> back -> data = x;
    obj -> back -> next = NULL;
}

int myStackPop(MyStack* obj) {
    QueueNode* record = obj -> back;

    while (obj -> front -> next != record){
        obj -> back -> next = obj -> front -> next;
        obj -> back = obj -> front -> next;
        obj -> front -> next = obj -> front -> next -> next;
        obj -> back -> next = NULL;
    }

    record = obj -> front -> next;
    obj -> front -> next = record -> next;
    obj -> back = obj -> front -> next == NULL ? obj -> front : obj -> back;
    QueueData result = record -> data;
    free(record);

    return result;
}

int myStackTop(MyStack* obj) {
    QueueNode* record = obj -> back;

    while (obj -> front -> next != record){
        obj -> back -> next = obj -> front -> next;
        obj -> back = obj -> front -> next;
        obj -> front -> next = obj -> front -> next -> next;
        obj -> back -> next = NULL;
    }

    QueueData result = obj -> front -> next -> data;
    obj -> back -> next = obj -> front -> next;
    obj -> back = obj -> front -> next;
    obj -> front -> next = obj -> front -> next -> next;
    obj -> back -> next = NULL;


    return result;
}

bool myStackEmpty(MyStack* obj) {
    return obj -> front -> next == NULL;
}

void myStackFree(MyStack* obj) {
    while (obj -> front -> next != NULL){
        QueueNode* tmp = obj -> front -> next;
        obj -> front -> next = obj -> front -> next -> next;
        free(tmp);
    }

    free(obj -> front);
    free(obj);
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值