Reputation: 1963
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. Popped: -1. Popped: -1. Popped: -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.
PS: I think the problem is with the Push
method - but not sure what went wrong.
Upvotes: 1
Views: 241
Reputation: 8514
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.
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
).
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.
Upvotes: 3