225. 用队列实现栈 - 力扣(LeetCode)
Queue.h
#pragma once
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QNode
{
QDataType data;
struct QNode* next;
} QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
} Queue;
Queue* QueueCreate();
void QueueDestroy(Queue* q);
void QueuePush(Queue* q, QDataType x);
QDataType QueuePop(Queue* q);
QDataType QueueFront(Queue* q);
QDataType QueueBack(Queue* q);
int QueueSize(Queue* q);
bool QueueEmpty(Queue* q);
Queue.c
#include"Queue.h"
Queue* QueueCreate()
{
Queue* q = (Queue*)malloc(sizeof(Queue));
if (NULL == q)
{
perror("malloc failed");
exit(-1);
}
q->head = q->tail = NULL;
q->size = 0;
return q;
}
void QueueDestroy(Queue* q)
{
assert(q);
while (!QueueEmpty(q))
QueuePop(q);
free(q);
}
void QueuePush(Queue* q, QDataType x)
{
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (NULL == newnode)
{
perror("malloc failed");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (QueueEmpty(q))
q->head = q->tail = newnode;
else
{
q->tail->next = newnode;
q->tail = newnode;
}
++(q->size);
}
QDataType QueuePop(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
QDataType ret = q->head->data;
QNode* newhead = q->head->next;
free(q->head);
q->head = newhead;
if (NULL == newhead)
q->tail = NULL;
--(q->size);
return ret;
}
QDataType QueueFront(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->head->data;
}
QDataType QueueBack(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->tail->data;
}
int QueueSize(Queue* q)
{
assert(q);
return q->size;
}
bool QueueEmpty(Queue* q)
{
assert(q);
return !(q->size);
}
MyStack.h
#pragma once
#include"Queue.h"
typedef struct MyStack
{
Queue* qa;
Queue* qb;
} MyStack;
MyStack* myStackCreate();
void myStackPush(MyStack* st, int x);
QDataType myStackPop(MyStack* st);
QDataType myStackTop(MyStack* st);
bool myStackEmpty(MyStack* st);
void myStackFree(MyStack* st);
MyStack.c
#include "MyStack.h"
MyStack* myStackCreate()
{
MyStack* st = (MyStack*)malloc(sizeof(MyStack));
if (NULL == st)
{
perror("malloc failed");
exit(-1);
}
st->qa = QueueCreate();
st->qb = QueueCreate();
return st;
}
void myStackPush(MyStack* st, QDataType x)
{
assert(st);
if (QueueEmpty(st->qb))
QueuePush(st->qa, x);
else
QueuePush(st->qa, x);
}
QDataType myStackPop(MyStack* st)
{
assert(st);
assert(!myStackEmpty(st));
Queue* empty = st->qa;
Queue* noempty = st->qb;
if (!QueueEmpty(st->qa))
{
empty = st->qb;
noempty = st->qa;
}
QDataType ret = noempty->tail->data;
while (QueueSize(noempty) > 1)
{
QDataType ret = QueuePop(noempty);
QueuePush(empty, ret);
}
QueuePop(noempty);
return ret;
}
QDataType myStackTop(MyStack* st)
{
assert(st);
assert(!myStackEmpty(st));
if (QueueEmpty(st->qb))
return st->qa->tail->data;
else
return st->qb->tail->data;
}
bool myStackEmpty(MyStack* st)
{
return (QueueEmpty(st->qa) && QueueEmpty(st->qb));
}
void myStackFree(MyStack* st)
{
QueueDestroy(st->qa);
QueueDestroy(st->qb);
free(st);
}
test.c
#include<stdio.h>
#include "MyStack.h"
static void Qprint(Queue* q)
{
assert(q);
QNode* cur = q->head;
printf("head<-");
while (cur)
{
printf("%d<-", cur->data);
cur = cur->next;
}
printf("tail\n");
}
void test1()
{
MyStack* st = myStackCreate();
myStackPush(st, 1);
myStackPush(st, 2);
printf("top = %d\n", myStackTop(st));
myStackPush(st, 3);
printf("qa:"); Qprint(st->qa); printf("qb:"); Qprint(st->qb);
printf("top = %d\n", myStackTop(st));
myStackPush(st, 4);
printf("qa:"); Qprint(st->qa); printf("qb:"); Qprint(st->qb);
printf("top = %d\n", myStackTop(st));
myStackPop(st);
printf("qa:"); Qprint(st->qa); printf("qb:"); Qprint(st->qb);
myStackPop(st);
printf("qa:"); Qprint(st->qa); printf("qb:"); Qprint(st->qb);
printf("top = %d\n", myStackTop(st));
myStackFree(st);
}
int main()
{
test1();
return 0;
}