🖊作者 : D. Star.
📘专栏 : 数据结构
😆今日小tips : 有些家里可能会有两个同样名字的 WiFi,而其中一个名字的最后有个 5G,那么,这个 5G 和常说的 5G 信号是一回事吗?并不是,无线路由器上的 5G 指的是 WiFi 的工作频段,手机上的 5G 指的是第五代蜂窝网络技术标准。与普通的 2.4G 频段相比,无线路由器上的 5G 频段的频宽更宽,如果宽带够快,那么就可以有更好的网络体验。但是 5G 频段的覆盖范围要比 2.4G 频段小一些,穿墙能力也要弱一些。
文章目录
🔎题目链接:
🔎题目:
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
[“MyStack”, “push”, “push”, “top”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
----解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
🔎分块解释:
✔队列函数:
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
//初始化
void InitQueue(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
pq->size = 0;
}
//销毁
void DestroyQueue(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* del = cur;
cur = cur->next;
free(del);
del = NULL;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
//判断是否为空
bool EmptyQueue(Queue* pq)
{
assert(pq);
return pq->head == NULL && pq->tail == NULL;
}
//入队
void PushQueue(Queue* pq, QDataType x)
{
assert(pq);
QNode* newNode = (QNode*)malloc(sizeof(QNode));
if (newNode == NULL)
{
perror("malloc fail");
exit(-1);
}
else
{
newNode->data = x;
newNode->next = NULL;
}
if (pq->tail == NULL)
{
pq->head = pq->tail = newNode;
}
else
{
pq->tail->next = newNode;
pq->tail = newNode;
}
pq->size++;
}
//出队
void PopQueue(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
if (pq->head != pq->tail)
{
QNode* del = pq->head;
pq->head = pq->head->next;
free(del);
}
else
{
free(pq->head);
pq->head = pq->tail = NULL;
}
pq->size--;
}
//取头
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->head->data;
}
//取尾
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->tail->data;
}
//队列的长度
int SizeQueue(Queue* pq)
{
assert(pq);
return pq->size;
}
✔用队列实现栈:
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
if (obj == NULL)
{
perror("malloc fail");
exit(-1);
}
InitQueue(&obj->q1);
InitQueue(&obj->q2);
return obj;
}
//将元素 x 压入栈顶
void myStackPush(MyStack* obj, int x) {
assert(obj);
//法一:
Queue* empty = &obj->q1;
Queue* noempty = &obj->q2;
if (!EmptyQueue(&obj->q1)) {
empty = &obj->q2;
noempty = &obj->q1;
}
PushQueue(noempty, x);
//法二:
// if(!EmptyQueue(&obj->q1))
// {
// PushQueue(&obj->q1,x);
// }
// else
// {
// PushQueue(&obj->q2,x);
// }
}
//移除并返回栈顶元素。
int myStackPop(MyStack* obj) {
assert(obj);
Queue* empty = &obj->q1;
Queue* noempty = &obj->q2;
if (!EmptyQueue(&obj->q1)) {
empty = &obj->q2;
noempty = &obj->q1;
}
//将不为空的n-1个移到为空中
while (noempty->size > 1) {
PushQueue(empty, QueueFront(noempty));
PopQueue(noempty);
}
int ret = QueueFront(noempty);
PopQueue(noempty);
return ret;
}
//返回栈顶元素。
int myStackTop(MyStack* obj) {
assert(obj);
//法一:
// //判断哪一个队列为空
// Queue* empty = &obj->q1;
// Queue* noempty = &obj->q2;
// if(!EmptyQueue(&obj->q1))
// {
// empty = &obj->q2;
// noempty = &obj->q1;
// }
// //将不为空的n-1个移到为空中
// while(noempty->size >1){
// PushQueue(empty,QueueFront(noempty));
// PopQueue(noempty);
// }
// int ret = QueueFront(noempty);
// PushQueue(empty,ret);
// PopQueue(noempty);
// return ret;
//法二:
if (!EmptyQueue(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
assert(obj);
return EmptyQueue(&obj->q1) && EmptyQueue(&obj->q2);
}
void myStackFree(MyStack* obj) {
assert(obj);
DestroyQueue(&obj->q1);
DestroyQueue(&obj->q2);
free(obj);
}
🔎代码详情:
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
typedef struct {
Queue q1;
Queue q2;
} MyStack;
//初始化
void InitQueue(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
pq->size = 0;
}
//销毁
void DestroyQueue(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* del = cur;
cur = cur->next;
free(del);
del = NULL;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
//判断是否为空
bool EmptyQueue(Queue* pq)
{
assert(pq);
return pq->head == NULL && pq->tail == NULL;
}
//入队
void PushQueue(Queue* pq, QDataType x)
{
assert(pq);
QNode* newNode = (QNode*)malloc(sizeof(QNode));
if (newNode == NULL)
{
perror("malloc fail");
exit(-1);
}
else
{
newNode->data = x;
newNode->next = NULL;
}
if (pq->tail == NULL)
{
pq->head = pq->tail = newNode;
}
else
{
pq->tail->next = newNode;
pq->tail = newNode;
}
pq->size++;
}
//出队
void PopQueue(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
if (pq->head != pq->tail)
{
QNode* del = pq->head;
pq->head = pq->head->next;
free(del);
}
else
{
free(pq->head);
pq->head = pq->tail = NULL;
}
pq->size--;
}
//取头
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->head->data;
}
//取尾
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->tail->data;
}
//队列的长度
int SizeQueue(Queue* pq)
{
assert(pq);
return pq->size;
}
MyStack* myStackCreate() {
MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
if (obj == NULL)
{
perror("malloc fail");
exit(-1);
}
InitQueue(&obj->q1);
InitQueue(&obj->q2);
return obj;
}
//将元素 x 压入栈顶
void myStackPush(MyStack* obj, int x) {
assert(obj);
//法一:
Queue* empty = &obj->q1;
Queue* noempty = &obj->q2;
if (!EmptyQueue(&obj->q1)) {
empty = &obj->q2;
noempty = &obj->q1;
}
PushQueue(noempty, x);
//法二:
// if(!EmptyQueue(&obj->q1))
// {
// PushQueue(&obj->q1,x);
// }
// else
// {
// PushQueue(&obj->q2,x);
// }
}
//移除并返回栈顶元素。
int myStackPop(MyStack* obj) {
assert(obj);
Queue* empty = &obj->q1;
Queue* noempty = &obj->q2;
if (!EmptyQueue(&obj->q1)) {
empty = &obj->q2;
noempty = &obj->q1;
}
//将不为空的n-1个移到为空中
while (noempty->size > 1) {
PushQueue(empty, QueueFront(noempty));
PopQueue(noempty);
}
int ret = QueueFront(noempty);
PopQueue(noempty);
return ret;
}
//返回栈顶元素。
int myStackTop(MyStack* obj) {
assert(obj);
//法一:
// //判断哪一个队列为空
// Queue* empty = &obj->q1;
// Queue* noempty = &obj->q2;
// if(!EmptyQueue(&obj->q1))
// {
// empty = &obj->q2;
// noempty = &obj->q1;
// }
// //将不为空的n-1个移到为空中
// while(noempty->size >1){
// PushQueue(empty,QueueFront(noempty));
// PopQueue(noempty);
// }
// int ret = QueueFront(noempty);
// PushQueue(empty,ret);
// PopQueue(noempty);
// return ret;
//法二:
if (!EmptyQueue(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
assert(obj);
return EmptyQueue(&obj->q1) && EmptyQueue(&obj->q2);
}
void myStackFree(MyStack* obj) {
assert(obj);
DestroyQueue(&obj->q1);
DestroyQueue(&obj->q2);
free(obj);
}
感谢家人的阅读,若有不准确的地方 欢迎在评论区指正!