题目描述
如果不知道什么是队列,什么是栈,可以先看看这篇文章
数据结构-队列
数据结构-栈
思路讲解
这道题目的意思就是,用两个队列实现栈的功能,我们要知道队列是先进入先出来,而栈是先进入后出来,只要把这个给解决了,这道题就好办了我们这里采取的方法是两个队列互相导数据,哪个队列是空的就往哪边导入数据,这里导数据的次数是有规定的,我们要满足栈的先入后出,我们往一个队列里面插入1,2,3,4,在向另一个队列里面导入1,2,3,在把原来队列里面剩的4给取出来,就完成了栈的先入后出,后入先出的原则这里就是这道题的核心,下面画图来看看
首先创建两个队列,然后开始导入数据
这样我们就完成队列实现栈
下面是代码
这里的C语言写的为什么这么长,那是因为C语言库中没得队列,我们需要手动创建一个
typedef int datatype;
struct queue_node
{
datatype data;
struct queue_node* next;
};
typedef struct queue_node queue_node;
struct queue_point
{
struct queue_node* phead;
struct queue_node* ptail;
int size;
};
typedef struct queue_point queue_point;
void init_node(queue_node* node)
{
node->data = 0;
node->next = NULL;
}
void queueinit(queue_point* point)//初始化
{
point->phead = NULL;
point->ptail = NULL;
point->size = 0;
}
void insert_push(queue_point* point, datatype data)//尾插入
{
assert(point);
queue_node* newnode = NULL;
newnode = (queue_node*)malloc(sizeof(queue_node));
if (newnode == NULL)
{
perror("newnode_fail");
exit(1);
}
newnode->data = data;
newnode->next = NULL;
if (point->ptail == NULL)
{
point->ptail = point->phead = newnode;
point->size++;
}
else
{
point->ptail->next = newnode;
point->ptail = newnode;
point->size++;
}
}
void del_push_data(queue_point* point)//删除队头
{
assert(point && point->size != 0);
queue_node* temp = NULL;
if (point->phead->next == NULL)
{
free(point->phead);
point->phead = NULL;
point->ptail = NULL;
point->size = 0;
}
else
{
temp = point->phead->next;
free(point->phead);
point->phead = temp;
point->size--;
}
}
void print_queue(queue_point* point)//打印队列
{
assert(point && point->size > 0);
queue_node* pcur = point->phead;
while (pcur)
{
printf("%d ->", pcur->data);
pcur = pcur->next;
}
printf("NULL");
}
void free_queue(queue_point* point)//删除队列
{
queue_node* prev = NULL;
queue_node* pcur = point->phead;
while (pcur)
{
prev = pcur;
pcur = pcur->next;
free(prev);
}
point->phead = point->ptail = NULL;
point->size = 0;
}
datatype get_front_data(queue_point* point)//取对头数据
{
assert(point && point->size > 0);
assert(point->phead);
return point->phead->data;
}
datatype get_back_data(queue_point* point)//取出队尾数据
{
assert(point && point->size > 0);
assert(point->phead);
return point->ptail->data;
}
int get_size(queue_point* point)//获得数量
{
assert(point->phead && point->size > 0);
return point->size;
}
bool queue_empty(queue_point* point)//判空
{
assert(point);
if (point->size == 0)
return true;
else
return false;
}
typedef struct {
queue_point p1;
queue_point p2;
} MyStack;
MyStack* myStackCreate() {
MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
queueinit(&(pst->p1));
queueinit(&(pst->p2));
return pst;
}
void myStackPush(MyStack* obj, int x) {
if ( queue_empty(&(obj->p2)))
{
insert_push(&(obj->p2), x);
}
else
{
insert_push(&(obj->p1), x);
}
}
int myStackPop(MyStack* obj) {
queue_point* empty = &(obj->p1);
queue_point* noempty = &(obj->p2);
if (queue_empty(&(obj->p1)))
{
noempty = &(obj->p2);
empty = &(obj->p1);
}
else
{
noempty = &(obj->p1);
empty = &(obj->p2);
}
while (get_size(noempty) > 1)
{
datatype insert = get_front_data(noempty);
insert_push(empty, insert);
del_push_data(noempty);
}
datatype topdata = get_front_data(noempty);
del_push_data(noempty);
return topdata;
}
int myStackTop(MyStack* obj) {
if (queue_empty(&(obj->p1)))
{
return get_back_data(&(obj->p2));
}
else
{
return get_back_data(&(obj->p1));
}
}
bool myStackEmpty(MyStack* obj) {
return queue_empty(&(obj->p1)) && queue_empty(&(obj->p2));
}
void myStackFree(MyStack* obj) {
free_queue(&(obj->p1));
free_queue(&(obj->p2));
free(obj);
}
完