【C语言】用队列实现栈

用两个队列(先进先出)实现一个栈(后进先出)

题目链接:https://leetcode.cn/problems/implement-stack-using-queues/description/

1.C语言首先要造一个队列出来

2.两个队列实现栈,始终保持一个队列为空,一个队列非空的状态

3.根据栈先进先出的特性,入栈要把数据放入非空队列中

4.取栈顶元素则取非空队列的队尾即可

5.出栈,并返回栈顶元素的值,则需要把非空队列中n个数据的前n-1个数据导入空队列中,剩下的唯一一个数据就是要出栈的数据。

cpp 复制代码
typedef int QDataType;
typedef struct QueueNode
{
    QDataType val;
    struct QueueNode* next;
}QNode;
typedef struct Queue
{
    QNode* phead;
    QNode* ptail;
    int size;
}Queue;

//初始化
void QueueInit(Queue* pq)
{
    assert(pq);
    pq->phead = pq->ptail = NULL;
    pq->size = 0;
}
//销毁
void QueueDestroy(Queue* pq)
{
    assert(pq);
    QNode* cur = pq->phead;
    while (cur)
    {
        QNode* next = cur->next;
        free(cur);
        cur = next;
    }
    pq->phead = pq->ptail = NULL;
    pq->size = 0;
}
//入队列
void QueuePush(Queue* pq, QDataType x)
{
    assert(pq);

    //在队尾入,尾插
    //创建新节点
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    if (newnode == NULL)
    {
        perror("malloc fail");
        return;
    }
    newnode->val = x;
    newnode->next = NULL;

    //空链表
    if (pq->phead==NULL)
    {
        pq->phead = pq->ptail = newnode;
    }
    else 
    {
        pq->ptail->next = newnode;
        pq->ptail = newnode;
    }
    pq->size++;
}
//出队列
void QueuePop(Queue* pq)
{
    assert(pq);
    //零个节点
    assert(pq->phead);
    //一个节点
    if (pq->phead->next == NULL)
    {
        free(pq->phead);
        pq->phead = pq->ptail = NULL;
    }
    else
    {
        QNode* next = pq->phead->next;
        free(pq->phead);
        pq->phead = next;
    }
    pq->size--;
}
//判空
bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->size == 0;
}
//取队头
QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(pq->phead);
    return pq->phead->val;
}
//取队尾
QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(pq->ptail);
    return pq->ptail->val;
}
//队列长度
size_t QueueLength(Queue* pq)
{
    assert(pq);
    return pq->size;
}

typedef struct {
    Queue obj1;
    Queue obj2;
} MyStack;

MyStack* myStackCreate() {
    MyStack* p=(MyStack*)malloc(sizeof(MyStack));
    QueueInit(&p->obj1);
    QueueInit(&p->obj2);
    return p;
}

void myStackPush(MyStack* obj, int x) {
    //入栈在非空队列
    if(QueueEmpty(&obj->obj1))
    {
        QueuePush(&obj->obj2,x);
    }
    else
    {
        QueuePush(&obj->obj1,x);
    }
}

int myStackPop(MyStack* obj) {
    Queue* EmptyQ=&obj->obj1;
    Queue* NoEmptyQ=&obj->obj2;
    if(!QueueEmpty(&obj->obj1))
    {
        EmptyQ=&obj->obj2;
        NoEmptyQ=&obj->obj1;
    }
    while(QueueLength(NoEmptyQ)>1)
    {
        int front=QueueFront(NoEmptyQ);
        QueuePush(EmptyQ,front);
        QueuePop(NoEmptyQ);
    }
    int front=QueueFront(NoEmptyQ);
    QueuePop(NoEmptyQ);
    return front;
}

int myStackTop(MyStack* obj) {
    Queue* EmptyQ=&obj->obj1;
    Queue* NoEmptyQ=&obj->obj2;
    if(!QueueEmpty(EmptyQ))
    {
        EmptyQ=&obj->obj2;
        NoEmptyQ=&obj->obj1;
    }
    return QueueBack(NoEmptyQ);
}

bool myStackEmpty(MyStack* obj) {
    return QueueEmpty(&obj->obj1)&&QueueEmpty(&obj->obj2);
}

void myStackFree(MyStack* obj) {
    QueueDestroy(&obj->obj1);
    QueueDestroy(&obj->obj2);
    free(obj);

}

/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);
 
 * int param_2 = myStackPop(obj);
 
 * int param_3 = myStackTop(obj);
 
 * bool param_4 = myStackEmpty(obj);
 
 * myStackFree(obj);
*/
相关推荐
小七在进步3 分钟前
string的实现(1)
c++·笔记·visual studio
布莱克6051 小时前
Swift 循环与控制转移详解:从 for-in 到标签跳转
开发语言·ssh·swift·循环
kyle~2 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
瑞克-桑切斯2 小时前
Kubernetes 1.28 保姆级新手学习教程
运维·学习·docker·云原生·容器·kubernetes
敲上瘾2 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
老花眼猫2 小时前
C语言绘图方法:(10)椭圆缩放旋转
c语言·经验分享·青少年编程·课程设计
联犀2 小时前
ai时代复杂项目的golang项目如何更好的与ai共舞:monorepo+gozero
开发语言·人工智能·golang
牢姐与蒯3 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好4 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
lcj25115 小时前
[C++] C++11
开发语言·c++·笔记