Data structure stack and queue interview questions (3) Two queues implement a stack

We know that queues are first-in, first-out, and stacks are last-in, first-out, so to use two queues to implement a stack, our thinking should be this:
always keep one queue empty each time, and operate on the other queue.
During the push operation, we use the push operation directly behind the queue with elements, which is equivalent to the push operation of the stack.
When performing the pop-out operation, we will dequeue the elements in the queue with elements in turn, and enter the queue into another queue. When there is one element left, the element that is dequeued at this time is the queue tail element, just Equivalent to stack pop. It happens to be the last in first out of the stack.

The following is the specific code implementation

#include <stdio.h>
#include "seqqueue.h"
#include"seqqueue.c"
typedef struct stackqueue{
    seqqueue queue1;
    seqqueue queue2;
}stackqueue;

initialization function

void stackqueue_init(stackqueue *s)
{
    if(s == NULL)
    {
        return;
    }
    seqqueue_init(&s->queue1);
    seqqueue_init(&s->queue2);
    return;
}

print function

void stackqueue_print(stackqueue *s)
{
    if(s == NULL)
    {
        return;
    }
    size_t i = 0;
    if(s->queue1.size == 0)
    {
        for(i = s->queue2.head;i < s->queue2.tail;i++)
        {
            if(i > MAXSIZE)
            {
                i = 0;
            }
            printf("[%c] ",s->queue2.data[i]);
            printf("\n");
            return;
        }
    }
    if(s->queue2.size == 0)
    {
        for(i = s->queue1.head;i < s->queue1.tail;i++)
        {
            if(i > MAXSIZE)
            {
                i = 0;
            }
            printf("[%c] ",s->queue1.data[i]);
            printf("\n");
        }
    }
}

push function

void stackqueue_push(stackqueue *s,seqqueuetype value)
{
    if(s == NULL)
    {
        return;
    }
    if(s->queue1.size == 0)
    {
        seqqueue_push(&s->queue2,value);
    }
    if(s->queue2.size == 0)
    {
        seqqueue_push(&s->queue1,value);
    }
}

pop function

void stackqueue_pop(stackqueue *s)
{
    if(s == NULL)
    {
        return;
    }
    seqqueuetype top;
    if(s->queue1.size != 0)
    {
        while(1)
        {
            if(s->queue1.size == 1)
            {
                seqqueue_pop(&s->queue1);
                return;
            }
            seqqueue_gettop(&s->queue1,&top);
            seqqueue_push(&s->queue2,top);
            seqqueue_pop(&s->queue1);
        }
    }
    else
    {
        while(1)
        {
            if(s->queue2.size == 1)
            {
                seqqueue_pop(&s->queue2);
                return;
            }
            seqqueue_gettop(&s->queue2,&top);
            seqqueue_push(&s->queue1,top);
            seqqueue_pop(&s->queue2);
        }
    }
}

get the top element of the stack

int stackqueue_gettop(stackqueue *s,seqqueuetype *value)
{
    if(s == NULL || value == NULL)
    {
        return 0;
    }
    seqqueuetype top
    if(s->queue1.size != 0)
    {
        while(1)
        {
            if(s->queue1.size == 1)
            {
                seqqueue_gettop(&s->queue1,value);
                return 1;
            }
            seqqueue_pop(&s->queue1);
        }
    }
    else
    {
        while(1)
        {
            if(s->queue2.size == 1)
            {
                seqqueue_gettop(&s->queue2,value);
                return 1;
            }
            seqqueue_pop(&s->queue2);
        }
    }
}

The following is the test function and the result verification
write picture description here
write picture description here


Please point out any mistakes, thank you

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325481437&siteId=291194637