目录
一、前言
栈和队列在算法题里面经常用到,可以用自己实现的栈和队列,也可以用库函数里面实现的栈和队列。
二、经典题型
1. 用栈实现队列(手动实现栈)
① 我们需要一个出栈栈代表出队列,然后一个入栈栈代表入队列,入栈需要倒入出栈倒过来,从而实现队列。
② 当出栈为空,就把入栈全部倒入进去。
③ 当出栈不为空,什么都不动。
AC代码:
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
void StackInit(Stack* ps) {
assert(ps);
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}
void StackDestroy(Stack* ps) {
assert(ps);
free(ps->_a);
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}
void StackPush(Stack* ps, STDataType data) {
assert(ps);
if (ps->_top == ps->_capacity) {
int newcapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->_a, sizeof(STDataType) * newcapacity);
if (tmp == NULL) {
perror("relloc fail");
exit(-1);
}
ps->_a = tmp;
ps->_capacity = newcapacity;
}
ps->_a[ps->_top] = data;
ps->_top++;
}
void StackPop(Stack* ps) {
assert(ps);
assert(ps->_top > 0);
--ps->_top;
}
bool StackEmpty(Stack* ps) {
assert(ps);
return ps->_top == 0;
}
STDataType StackTop(Stack* ps) {
assert(ps);
assert(ps->_top > 0);
return ps->_a[ps->_top - 1];
}
int StackSize(Stack* ps) {
assert(ps);
return ps->_top;
}
typedef struct {
Stack pushst;
Stack popst;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&obj->pushst);
StackInit(&obj->popst);
return obj;
}
void myQueuePush(MyQueue* obj, int x) {
StackPush(&obj->pushst, x);
}
int myQueuePeek(MyQueue* obj) {
if (StackEmpty(&obj->popst)) {
while (!StackEmpty(&obj->pushst)) {
StackPush(&obj->popst, StackTop(&obj->pushst));
StackPop(&obj->pushst);
}
}
return StackTop(&obj->popst);
}
int myQueuePop(MyQueue* obj) {
int front = myQueuePeek(obj);
StackPop(&obj->popst);
return front;
}
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst);
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&obj->pushst);
StackDestroy(&obj->popst);
free(obj);
}
/**
* 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);
*/
2. 队列实现栈(手动实现队列)
① 需要准备两个队列,一个不为空,一个为空。
② 不为空的队列,pop时候,先把前n - 1个入为空的队列,再把最后一个元素pop,就完成了出队列。
③ 取top的时候,直接取队列的尾元素。
AC代码:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// 链式结构:表示队列
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* _next;
QDataType _data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
int size;
}Queue;
// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
void QueueInit(Queue* q) {
assert(q);
q->_front = q->_rear = NULL;
q->size = 0;
}
void QueueDestroy(Queue* q) {
assert(q);
QNode* cur = q->_front;
while (cur) {
QNode* next = cur->_next;
free(cur);
cur = next;
}
q->_front = q->_rear = NULL;
q->size = 0;
}
void QueuePush(Queue* q, QDataType data) {
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL) {
perror("malloc fail");
exit(-1);
}
newnode->_data = data;
newnode->_next = NULL;
if (q->_front == NULL) {
q->_front = q->_rear = newnode;
}
else {
q->_rear->_next = newnode;
q->_rear = newnode;
}
q->size++;
}
void QueuePop(Queue* q) {
assert(q);
assert(!QueueEmpty(q));
if (q->_front->_next == NULL) {
free(q->_front);
q->_front = q->_rear = NULL;
}
else {
QNode* next = q->_front->_next;
free(q->_front);
q->_front = next;
}
q->size--;
}
QDataType QueueFront(Queue* q) {
assert(q);
assert(!QueueEmpty(q));
return q->_front->_data;
}
QDataType QueueBack(Queue* q) {
assert(q);
assert(!QueueEmpty(q));
return q->_rear->_data;
}
int QueueEmpty(Queue* q) {
assert(q);
return q->_front == NULL;
}
int QueueSize(Queue* q) {
assert(q);
return q->size;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&pst->q1);
QueueInit(&pst->q2);
return pst;
}
void myStackPush(MyStack* obj, int x) {
if (!QueueEmpty(&obj->q1)) {
QueuePush(&obj->q1, x);
}else {
QueuePush(&obj->q2, x);
}
}
int myStackPop(MyStack* obj) {
Queue* empty = &obj->q1;
Queue* nonempty = &obj->q2;
if (!QueueEmpty(&obj->q1)) {
nonempty = &obj->q1;
empty = &obj->q2;
}
while (QueueSize(nonempty) > 1) {
QueuePush(empty, QueueFront(nonempty));
QueuePop(nonempty);
}
int top = QueueFront(nonempty);
QueuePop(nonempty);
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);
}
/**
* 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);
*/