Jan 21, 2019 at 6:51pm Jan 21, 2019 at 6:51pm UTC
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 Jan 21, 2019 at 7:00pm Jan 21, 2019 at 7:00pm UTC
Jan 21, 2019 at 7:48pm Jan 21, 2019 at 7:48pm UTC
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.
Jan 21, 2019 at 7:58pm Jan 21, 2019 at 7:58pm UTC
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 Jan 21, 2019 at 7:58pm Jan 21, 2019 at 7:58pm UTC