简体   繁体   English

使用2个队列实现堆栈

[英]Implementing Stack using 2 Queues

Why does this code work: 为什么这段代码有效:

#include <cstdio>
#include <cstdlib>

#define N n *
#define Q q *
#define S s *

typedef struct n
{
    int data;
    struct N nxt;
}n;

typedef struct q
{
    N f;
    N r;
}q;

typedef struct stack
{
    Q q1;
    Q q2;
}s;
Q Createq()
{
    Q qq = (Q)malloc(sizeof(q));
    qq->f = qq->r  = 0;
    return qq;
}

S CreateStk()
{
    S stk = (S)malloc(sizeof(s));
    stk->q1 = Createq();
    stk->q2 = Createq();
    return stk;
}

int Deq(Q qq)
{
    if(qq->f == 0 && qq->r == 0) return -1;
    N nn = qq->r;
    int data = nn->data;
    qq->r = qq->r->nxt;
    free(nn);
    if(!qq->r)
        qq->f = 0;
    return data;
}

void Enq(Q qq, int data)
{
    if(!qq->f)
    {
        N nn = (N)malloc(sizeof(n));
        nn->data = data;
        nn->nxt = 0;
        qq->f = qq->r = nn;
    }
    else
    {
        N nn = (N)malloc(sizeof(n));
        nn->data = data;
        nn->nxt = 0;
        qq->f->nxt = nn;
        qq->f = nn;
    }
}

void Push(S stk, int data)
{
    Enq(stk->q2,data);

    while(stk->q1->f)
    {
        Enq(stk->q2,Deq(stk->q1));
    }

    Q t = stk->q1;
    stk->q1 = stk->q2;
    stk->q2 = t;
}

int Pop(S stk)
{
    return Deq(stk->q1);
}
int main() 
{
    S stk = CreateStk();
    Push(stk,10);
    Push(stk,30);
    Push(stk,40);
    Push(stk,50);
    printf("\nPopped: %d.", Pop(stk));
    printf("\nPopped: %d.", Pop(stk));
    printf("\nPopped: %d.", Pop(stk));
    printf("\nPopped: %d.", Pop(stk));
    return 0;
    return 0;
}

Output: 输出:

 Popped: 50. Popped: 40. Popped: 30. Popped: 10. 

While this does not: 虽然这不是:

#include <cstdio>
#include <cstdlib>

#define N n *
#define Q q *

typedef struct n
{
    int data;
    struct N nxt;
}n;

typedef struct q
{
    N f;
    N r;
}q;

Q Createq()
{
    Q qq = (Q)malloc(sizeof(q));
    qq->f = qq->r  = 0;
    return qq;
}

int Deq(Q qq)
{
    if(qq->f == 0 && qq->r == 0) return -1;
    N nn = qq->r;
    int data = nn->data;
    qq->r = qq->r->nxt;
    free(nn);
    if(!qq->r)
        qq->f = 0;
    return data;
}

void Enq(Q qq, int data)
{
    if(!qq->f)
    {
        N nn = (N)malloc(sizeof(n));
        nn->data = data;
        nn->nxt = 0;
        qq->f = qq->r = nn;
    }
    else
    {
        N nn = (N)malloc(sizeof(n));
        nn->data = data;
        nn->nxt = 0;
        qq->f->nxt = nn;
        qq->f = nn;
    }
}

void Push(Q qq1, Q qq2, int data)
{
    Enq(qq2,data);

    while(qq1->f)
    {
        Enq(qq2,Deq(qq1));
    }

    Q t = qq1;
    qq1 = qq2;
    qq2 = t;
}

int Pop(Q qq1)
{
    return Deq(qq1);
}

int main() {
    // your code goes here
    Q qq1 = Createq();
    Q qq2 = Createq();
    Push(qq1,qq2,10);
    Push(qq1,qq2,30);
    Push(qq1,qq2,40);
    Push(qq1,qq2,50);
    printf("\nPopped: %d.", Pop(qq1));
    printf("\nPopped: %d.", Pop(qq1));
    printf("\nPopped: %d.", Pop(qq1));
    printf("\nPopped: %d.", Pop(qq1));
    return 0;
}

Output: 输出:

Popped: -1. 弹出:-1。 Popped: -1. 弹出:-1。 Popped: -1. 弹出:-1。 Popped: -1. 弹出:-1。

The expected output is the first one as its obvious by the question heading. 预期的输出是问题标题显而易见的第一个输出。 However i do not understand the gory details of why the code did not work when i did not encapsulate the 2 queues in a struct in my second example. 但是,当我在第二个例子中没有将2个队列封装在结构中时,我不明白为什么代码不起作用的血腥细节。

PS: I think the problem is with the Push method - but not sure what went wrong. PS:我认为问题在于Push方法 - 但不确定出了什么问题。

Aside from being horrendously hard to read, the problem is that your second Push function changes the values of its parameters in the following lines of code. 除了非常难以阅读之外,问题在于您的第二个Push函数会在以下代码行中更改其参数的值。

Q t = qq1;
qq1 = qq2;
qq2 = t;

qq1 and qq2 are parameters of the function, so the new values are not updated in the calling function ( main ). qq1qq2是函数的参数,因此新值不会在调用函数( main )中更新。

One way to fix this is to make the parameters pass by reference: 解决此问题的一种方法是使参数通过引用传递:

void Push(Q &qq1, Q &qq2, int data)

That way, changes to qq1 and qq2 will also change the values in the calling function. 这样,对qq1qq2更改也将更改调用函数中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM