typedef struct {
Queue* q1;
Queue* q2;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate() {
MyStack* obj=(MyStack*)malloc(sizeof(MyStack));
obj->q1=NULL;
obj->q2=NULL;
return obj;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
if(QueueEmpty(&obj->q1)!= NULL)
{
QueuePush(&obj->q1, x);
}
else
{
QueuePush(&obj->q2, x);
}
}
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
Queue** empty=&obj->q2,**nonempty=&obj->q1;
if(obj->q2!=NULL)
{
empty=&obj->q1;
nonempty=&obj->q2;
}
while(QueueSize(*nonempty)>1)
{
QueuePush(empty,Queuetake(nonempty));
QueuePop(nonempty);
}
int ret=Queuetake(nonempty);
QueuePop(nonempty);
return ret;
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
Queue* empty=obj->q2,*nonempty=obj->q1;
if(obj->q2!=NULL)
{
empty=obj->q1;
nonempty=obj->q2;
}
return QueuetakeBack(&nonempty);
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(obj->q1)&&QueueEmpty(obj->q2);
}
void myStackFree(MyStack* obj) {
QueueDestory(&obj->q1);
QueueDestory(&obj->q2);
free(obj);
obj=NULL;
}
typedef struct {
Stack a;
Stack b;
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
Stackinit(&obj->a);
Stackinit(&obj->b);
return obj;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
StackPush(&obj->a,x);
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
if(StackEmpty(&obj->b)==1)
{
while(StackEmpty(&obj->a)!=1)
{
StackPush(&obj->b,Stacktake(&obj->a));
StackPop(&obj->a);
}
}
int ret=Stacktake(&obj->b);
StackPop(&obj->b);
return ret;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
if(StackEmpty(&obj->b)==1)
{
while(StackEmpty(&obj->a)!=1)
{
StackPush(&obj->b,Stacktake(&obj->a));
StackPop(&obj->a);
}
}
return Stacktake(&obj->b);
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->a)&&StackEmpty(&obj->b);
}
void myQueueFree(MyQueue* obj) {
StackDestory(&obj->a);
StackDestory(&obj->b);
free(obj);
obj=NULL;
写完这两题之后你会发现用两个队列实现栈明显要比用两个栈实现队列麻烦多了,用两个栈实现队列的话,你只需要把push栈的元素填入pop栈,然后pop栈出去即可,而用两个队列实现栈就没有这么简单了,他需要你每次都出了只剩一个元素,然后在出掉那一个元素,这正是由栈和队列的性质所造成的。
如果用链表的话,你需要一开始创建一个k个节点的链表,而且当你最后写完程序释放掉这k个节点时,也比较麻烦。所以还是应该用数组,他创建和销毁都比较简单,而且这么空间大小已经给定了,不需要扩容什么的。
当你使用数组写循环队列时,要注意如果rear超过了数组范围要将它回来开头,front也是同样的道理。如果你是用链表写的话,那么就是将尾节点的next指向头节点。
typedef struct {
int * arr;
int front;
int rear;
int k;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
obj->arr=(int *)malloc(sizeof(int)*(k+1));
obj->front=0;
obj->rear=0;
obj->k=k;
return obj;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
{
return false;
}
obj->arr[obj->rear++]=value;
if(obj->rear==obj->k+1)
{
obj->rear=0;
}
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
++obj->front;
if(obj->front==obj->k+1)
{
obj->front=0;
}
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->arr[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
if(obj->rear==0)
{
return obj->arr[obj->k];
}
else
{
return obj->arr[obj->rear-1];
}
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->rear==obj->front;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->rear+1)%(obj->k+1)==obj->front;
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->arr);
free(obj);
}