栈实现队列
–> leetcode 链接
先看这道题
入数据很简单
但是如果要出数据就要先把数据从第一个栈 pop
出来,之后 push
到 另外一个栈中
然后就可以把第二个栈中的元素pop
出去,就可以啦。
源码
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
bool StackEmpty(ST* ps);
void StackInit(ST* ps)
{
assert(ps);
ps->capacity = 0;
ps->a = NULL;
ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->capacity == ps->top)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->capacity = newCapacity;
ps->a = tmp;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
void StackDesroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool StackEmpty(ST* ps)
{
assert(ps);
//if (ps->top > 0)
// return false;
//else
// return true;
return ps->top == 0;
}
typedef struct
{
ST stPush;
ST stPop;
} MyQueue;
bool myQueueEmpty(MyQueue* obj);
MyQueue* myQueueCreate() {
MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&q->stPush);
StackInit(&q->stPop);
return q;
}
void myQueuePush(MyQueue* obj, int x) {
StackPush(&obj->stPush, x);
}
int myQueuePop(MyQueue* obj) {
if(StackEmpty(&obj->stPop))
{
while(!StackEmpty(&obj->stPush))//把 stPush 的元素都放到 stPop中
{
StackPush(&obj->stPop, StackTop(&obj->stPush));
StackPop(&obj->stPush);
}
}
int ret = StackTop(&obj->stPop);
StackPop(&obj->stPop);
return ret;
}
int myQueuePeek(MyQueue* obj) {
if(StackEmpty(&obj->stPop))
{
while(!StackEmpty(&obj->stPush))//把 stPush 的元素都放到 stPop中
{
StackPush(&obj->stPop, StackTop(&obj->stPush));
StackPop(&obj->stPush);
}
}
return StackTop(&obj->stPop);
}
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->stPop) && StackEmpty(&obj->stPush);
}
void myQueueFree(MyQueue* obj) {
StackDesroy(&obj->stPush);
StackDesroy(&obj->stPop);
free(obj);
}
队列实现栈
–> leetcode 链接
这道题也是入数据很简单
但是出数据就和上一道题有些不同了,我们可以先把第一个队列中的数据 pop
到第二个
直到只剩下一个元素,然后把这个元素 pop
掉, 就达到 后入先出 的目的了
源码
typedef int QueueDatatype;
typedef struct QListNode
{
struct QListNode* next;
QueueDatatype data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
bool QueueEmpty(Queue* pq);
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while(cur != NULL)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->tail = pq->head = NULL;
}
void QueuePush(Queue* pq, QueueDatatype x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
if (pq->head == NULL)
{
pq->head = pq->tail = NULL;
}
}
QueueDatatype QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QueueDatatype QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
int QueueSize(Queue* pq)
{
assert(pq);
int count = 0;
QNode* cur = pq->head;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* st = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&st->q1);
QueueInit(&st->q2);
return st;
}
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1, x);
}
else
{
QueuePush(&obj->q2, x);
}
}
int myStackPop(MyStack* obj) {
Queue* emptyQ = &obj->q1;
Queue* nonemptyQ = &obj->q2;
if(!QueueEmpty(&obj->q1))//转化 空 和 非空 队列
{
emptyQ = &obj->q2;
nonemptyQ = &obj->q1;
}
while(QueueSize(nonemptyQ) > 1)
{
QueuePush(emptyQ,QueueFront(nonemptyQ));
QueuePop(nonemptyQ);
}
int top = QueueFront(nonemptyQ);
QueuePop(nonemptyQ);
return top;
}
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
真不戳