【C语言】用队列实现栈

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

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

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

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

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

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

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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C语言中可以使用数组或链表来实现队列。 1. 队列 使用数组实现队列,需要定义一个指向队列头的指针front和一个指向队列尾的指针rear,队列的长度为maxsize。具体实现如下: ``` #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int front, rear; } Queue; void initQueue(Queue *q) { q->front = q->rear = 0; } int isQueueFull(Queue *q) { return (q->rear + 1) % MAXSIZE == q->front; } int isQueueEmpty(Queue *q) { return q->front == q->rear; } void enqueue(Queue *q, int x) { if (isQueueFull(q)) { printf("Queue is full\n"); return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return -1; } int x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return x; } ``` 其中,initQueue函数用于初始化队列;isQueueFull函数和isQueueEmpty函数分别用于判断队列是否已满和是否为空;enqueue函数用于入队;dequeue函数用于出队。注意,在队列满或空时,需要对应处理。 使用链表实现队列,需要定义一个链表节点Node,以及一个指向队列头节点和队列尾节点的指针head和tail。具体实现如下: ``` typedef struct Node { int data; struct Node *next; } Node; typedef struct { Node *head, *tail; } Queue; void initQueue(Queue *q) { q->head = q->tail = NULL; } int isQueueEmpty(Queue *q) { return q->head == NULL; } void enqueue(Queue *q, int x) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = x; newNode->next = NULL; if (isQueueEmpty(q)) { q->head = q->tail = newNode; } else { q->tail->next = newNode; q->tail = newNode; } } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return -1; } int x = q->head->data; Node *temp = q->head; q->head = q->head->next; if (q->head == NULL) { q->tail = NULL; } free(temp); return x; } ``` 其中,initQueue函数用于初始化队列;isQueueEmpty函数用于判断队列是否为空;enqueue函数用于入队;dequeue函数用于出队。注意,在队列为空时,需要对应处理。 2. 使用数组实现,需要定义一个指向顶的指针top,的长度为maxsize。具体实现如下: ``` #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int top; } Stack; void initStack(Stack *s) { s->top = -1; } int isStackFull(Stack *s) { return s->top == MAXSIZE - 1; } int isStackEmpty(Stack *s) { return s->top == -1; } void push(Stack *s, int x) { if (isStackFull(s)) { printf("Stack is full\n"); return; } s->top++; s->data[s->top] = x; } int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack is empty\n"); return -1; } int x = s->data[s->top]; s->top--; return x; } ``` 其中,initStack函数用于初始化;isStackFull函数和isStackEmpty函数分别用于判断是否已满和是否为空;push函数用于入;pop函数用于出。注意,在满或空时,需要对应处理。 使用链表实现,需要定义一个链表节点Node,以及一个指向顶节点的指针top。具体实现如下: ``` typedef struct Node { int data; struct Node *next; } Node; typedef struct { Node *top; } Stack; void initStack(Stack *s) { s->top = NULL; } int isStackEmpty(Stack *s) { return s->top == NULL; } void push(Stack *s, int x) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = x; newNode->next = s->top; s->top = newNode; } int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack is empty\n"); return -1; } int x = s->top->data; Node *temp = s->top; s->top = s->top->next; free(temp); return x; } ``` 其中,initStack函数用于初始化;isStackEmpty函数用于判断是否为空;push函数用于入;pop函数用于出。注意,在为空时,需要对应处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值