C语言面试题之化栈为队

化栈为队

实例要求

  • C语言实现实现一个MyQueue类,该类用两个栈来实现一个队列;
  • 示例:
c 复制代码
MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 false
  • 说明:
  • 1、只能使用标准的栈操作 ,即只有 push to top, peek/pop from top, size 和 is empty
    操作是合法的;
  • 2、所使用的语言也许不支持栈。
  • 3、可以使用 list 或者 deque(双端队列)来模拟一个栈,
  • 4、只要是标准的栈操作即可。
  • 5、假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

实例分析

  • 一、算法思想:
  • 若实现一个队列的功能,需要用到两个栈来实现此功能,创建两个栈S1和S2;
  • 二、入队列:
  • 所有的数据元素都入栈到S1,即所有的数据元素在S1完成入队列;
  • 三、出队列:
  • 判断S2是否为空;
  • 若S2不为空,则数据元素在S2出栈,即数据元素在S2完成出队列;
  • 若S2为空且S1不为空,则S1中所有数据元素依次在S1出栈并依次入栈到S2,接下来,所有的数据元素在S2出栈,即所有的数据元素在S2完成出队列;
  • 若S2为空且S1为空,即所构造的队列为空;

示例代码

c 复制代码
#define maxSize 1024

typedef struct {
    int stack1[maxSize];
    int top1; // 栈1的栈顶指针
    int stack2[maxSize];
    int top2; // 栈2的栈顶指针
} MyQueue;

/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
    MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
    queue->top1 = -1; // 栈1为空
    queue->top2 = -1; // 栈2为空
    return queue;
}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
    obj->stack1[++obj->top1] = x; // 将元素压入栈1
}

/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
    if (obj->top2 == -1) { // 如果栈2为空
        while (obj->top1 != -1) { // 将栈1中的元素逐个弹出并压入栈2,以颠倒顺序
            obj->stack2[++obj->top2] = obj->stack1[obj->top1--];
        }
    }
    if (obj->top2 == -1) { // 如果栈2仍为空,说明队列为空
        return -1;
    }
    return obj->stack2[obj->top2--]; // 弹出栈2的栈顶元素
}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
    if (obj->top2 == -1) { // 如果栈2为空
        while (obj->top1 != -1) { // 将栈1中的元素逐个弹出并压入栈2,以颠倒顺序
            obj->stack2[++obj->top2] = obj->stack1[obj->top1--];
        }
    }
    if (obj->top2 == -1) { // 如果栈2仍为空,说明队列为空
        return -1;
    }
    return obj->stack2[obj->top2]; // 返回栈2的栈顶元素,但不弹出
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
    return obj->top1 == -1 && obj->top2 == -1; // 如果栈1和栈2均为空,则队列为空
}

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

/**
 * Your MyQueue struct will be instantiated and called as such:
 * MyQueue* obj = myQueueCreate();
 * myQueuePush(obj, x);
 
 * int param_2 = myQueuePop(obj);
 
 * int param_3 = myQueuePeek(obj);
 
 * bool param_4 = myQueueEmpty(obj);
 
 * myQueueFree(obj);
*/

运行结果


相关推荐
UIUV2 分钟前
JavaScript中this指向机制与异步回调解决方案详解
前端·javascript·代码规范
momo1002 分钟前
IndexedDB 实战:封装一个通用工具类,搞定所有本地存储需求
前端·javascript
liuniansilence2 分钟前
🚀 高并发场景下的救星:BullMQ如何实现智能流量削峰填谷
前端·分布式·消息队列
再花2 分钟前
在Angular中实现基于nz-calendar的日历甘特图
前端·angular.js
星辰烈龙4 分钟前
黑马程序员Java基础9
java·开发语言
山沐与山5 分钟前
【Redis】Redis集群模式架构详解
java·redis·架构
GISer_Jing14 分钟前
今天看了京东零售JDS的保温直播,秋招,好像真的结束了,接下来就是论文+工作了!!!加油干论文,学&分享技术
前端·零售
ss27315 分钟前
Java并发编程:DelayQueue延迟订单系统
java·python·算法
wcy_101120 分钟前
七大软件设计原则
java·设计规范
Mapmost21 分钟前
【高斯泼溅】如何将“歪头”的3DGS模型精准“钉”在地图上,杜绝后续误差?
前端