Article Directory
One: ideas
Two: realization
(1) Structure definition
(2) Initialization and destruction
Note: When creating a new Mystack
structure outside the test file , there cannot be only one sentence Mystack ms
. Because the queue is used to implement the stack, then the bottom layer of the stack is the queue , so the queue structure must be created at the same time and then assigned.
(3) Into the "stack"
Note: The essence of stacking and popping is to operate the queue, so only the interface of the queue operation is used here. If you want to understand the queue's dequeue and enqueue operations, please see:
(4) Out of the "stack"
Three: code
QueueToStack.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct QNode
{
DataType _val;
struct QNode* _next;
}QNode;
typedef struct Queue
{
QNode* _front;
QNode* _rear;
}Queue;
typedef struct MyStack
{
Queue* _q1;//储备元素的栈
Queue* _q2;//进元素的栈
}MyStack;
void MyStackInit(MyStack* ps);
void MyStackPush(MyStack* ps, DataType x);//进栈
DataType MystackPop(MyStack* ps);//出栈
QueueToStack.c
#include "QueueToStack.h"
void MyStackInit(MyStack* ps)
{
assert(ps);
ps->_q1->_front = NULL;
ps->_q1->_rear = NULL;
ps->_q2->_front = NULL;
ps->_q2->_rear = NULL;
printf("初始化成功\n");
}
void MystackDestory(MyStack* ps)
{
assert(ps);
//释放储存栈
QNode* cur = ps->_q1->_front;
QNode* pre;
while (cur)
{
pre = cur;
cur = cur->_next;
free(pre);
}
free(ps);
}
//入队
void QueuePush(Queue* pq, DataType x)
{
assert(pq);
QNode* NewNode = (QNode*)malloc(sizeof(QNode));
NewNode->_val = x;
NewNode->_next = NULL;
if (pq->_rear == NULL)
{
pq->_rear = NewNode;
pq->_front = NewNode;
}
else
{
pq->_rear->_next = NewNode;
pq->_rear = NewNode;
}
}//
//出队
DataType QueuePop(Queue* pq)
{
assert(pq);
if (pq->_front == pq->_rear)
{
DataType rec = pq->_front->_val;
free(pq->_rear);
pq->_rear = NULL;
pq->_front = NULL;
return rec;
}
else
{
QNode* cur = pq->_front;
pq->_front = pq->_front->_next;
return cur->_val;
free(cur);
}
}
void MyStackPush(MyStack* ps,DataType x)
{
//如果队列1不空就先把队列1中的元素出队列然后进入队列2
if (ps->_q1->_front != NULL)
{
DataType save = QueuePop(ps->_q1);//出队列1
QueuePush(ps->_q2,save);//进队列2
}
QueuePush(ps->_q2, x);//而后在正常进“栈”
}
DataType MystackPop(MyStack* ps)
{
if (ps->_q2->_front == NULL && ps->_q1->_front !=NULL)
//如果队列2为空,队列1不空,将队1所有元素出队1,依次进队2
{
while (ps->_q1->_front)
{
DataType save = QueuePop(ps->_q1);
QueuePush(ps->_q2, save);
}
}
//出“栈”时,第一个元素就是队列2的尾元素
while(ps->_q2->_front!=ps->_q2->_rear)//出队2直到队列2剩余最后一个元素
{
DataType save = QueuePop(ps->_q2);
QueuePush(ps->_q1, save);
}
DataType rec = QueuePop(ps->_q2);
return rec;
}
test.c
#include "QueueToStack.h"
void test()
{
MyStack ms;
Queue q1;
Queue q2;
ms._q1 = &q1;
ms._q2 = &q2;
MyStackInit(&ms);
MyStackPush(&ms, 1);
MyStackPush(&ms, 2);
MyStackPush(&ms, 3);
MyStackPush(&ms, 4);
MyStackPush(&ms, 5);
printf("%d ", MystackPop(&ms));
printf("%d ", MystackPop(&ms));
printf("%d ", MystackPop(&ms));
printf("%d ", MystackPop(&ms));
printf("%d ", MystackPop(&ms));
}
int main()
{
test();
}