Queries Help Please

I have a code that is not quite working, it's supposed to show the query but it just copys the last number

//Queue
#include<iostream>
using namespace std;
int N;
struct Queue
{
int inf;
Queue *next;
};
Queue *head, *tail;
int IsEmptyQ(Queue* h)
{
return h==NULL;
}
void CreateEmptyQ(Queue* &h, Queue* &t)
{
h = t = NULL;
}
void Put (int x, Queue* &h, Queue* &t)
{
Queue *q;
q = new Queue;
q -> inf = x;
q -> next = NULL;
if( IsEmptyQ (h))
{
h = q;
}
else
{
t -> next = q;
}
t = q;
}
void Get (int x, Queue* &h, Queue* &t)
{
Queue *q;
q = h;
h = h -> next;
x = q -> inf;
if (IsEmptyQ(t))
{
t = NULL;
}
delete q;
}
int main()
{
cin>>N;
CreateEmptyQ(head,tail);
int x;
for(int i=0;i<N;i++)
{
cin>>x;
Put(x,head,tail);
}
for(int i=0;i<N;i++)
{
Get(x,head,tail);
cout<<x<<' ';
}
cout<<endl;
return 0;
}
Last edited on
Although not object oriented, this is some very nice, very clean code.

Get() sets x to the value at the head of the queue, but since x is passed by value, it has no effect on the parameter that you passed in. Change
void Get (int x, Queue* &h, Queue* &t)
to
void Get (int &x, Queue* &h, Queue* &t)

or maybe change Get to return the value gotten, i.e.
int Get(Queue * &h, Queue *&t);
and change the call accordingly.
Both of the suggestions doesn't work.


When I do the first one it shows me the queue in the order i put it.

And the secons one shows me the last number N-times

But thank you for trying!
Last edited on
When I do the first one it shows me the queue in the order i put it.

Well, that's what a queue does: first in, first out. What order were you expecting?
Topic archived. No new replies allowed.