-2

suppose I got a node like this:

struct node{
  int a;
  int b;
} 

Now I want to make a queue structure in cpp. If the data type was int then we could do that easily in this way:

queue<int> a;

And we could also push or pop elements like this: a. push_back(12) or a. pop(12)

But in the earlier case when out data type is user defined how can we make such a queue and push or pop elemeqnts from it?

5
  • 2
    I don't get what your problem is. You can well use std::queue<node> as you like. (Didn't dv BTW) Commented Sep 11, 2016 at 9:13
  • 1
    Exactly the same way, replace int with node. Have you even tried it? Commented Sep 11, 2016 at 9:15
  • How to push element there. Suppose I want to push 11, 12 for the first node. Commented Sep 11, 2016 at 9:16
  • You should create an object of node and then push it.
    – max_hassen
    Commented Sep 11, 2016 at 9:21
  • 1
    @AmirKhasru Something like a.push_back(node{11,12}) maybe? Commented Sep 11, 2016 at 9:24

4 Answers 4

3

There is absolutely no problem in holding non-PODs like struct or class inside container like queue.

struct s1{
    int a;
    string b;
};
class Foo{
    int a;
    string b;
};
int main() {
    queue<int> qi;
    queue<s1> qs;
    queue<Foo> qfoo;
    return 0;
}
6
  • Nice. It was awesome. But how to push such class or struct node there? Commented Sep 11, 2016 at 9:19
  • 1
    Basically you use containers to hold multiple instances of class or struct. So create instance and push them in. Then you can sort them, delete them, add more and do whatever you wish. Commented Sep 11, 2016 at 9:21
  • @SauravSahu Show them how to do actually. Feel free to take that from my comment. Commented Sep 11, 2016 at 9:25
  • suppose I got a queue like this: queue<node>a. Now I want to push there 11 and 12. Then how can I push them? Push_back(11, 12)? This way? Commented Sep 11, 2016 at 9:26
  • @AmirKhasru Read my comment. Commented Sep 11, 2016 at 9:28
2
// Example program
#include <iostream>
#include <queue>

using namespace std;

struct s1{
    int a;
    string b;
};

class Foo{
    public:
    int a;
    string b;
};

int main()
{
  queue<Foo> q;
  Foo obj;
  obj.a=2;
  obj.b="Object";
  q.push(obj);
  Foo p=q.back();

  cout<<p.a<<endl;
  cout<<p.b<<endl;

  return 0;
}
1
  • Nice explanation. :) Commented Sep 12, 2016 at 15:49
1

If you have

struct node{
    int a;
    int b;
};

std::queue<node> q;

you can use push as follows:

q.push({ 11, 12 });

this adds a node with a = 11 and b = 12.

This works as long as copy-list-initialization is possible, as it is in this case. Otherwise you would have to use q.push(node{ 11, 12 });

3
  • Supposed the constructor wasn't declared explicit. Commented Sep 11, 2016 at 9:37
  • @πάνταῥεῖ What constructor? Commented Sep 11, 2016 at 9:45
  • @DeiDei One imaginary given. I'm just trying to explore the whole space of possibilities. Commented Sep 11, 2016 at 9:48
-4

Use queue a; make objects of node type and use functions a.push_back() a.pop_front() to push and pop respectively.Also don't forget to #include queue.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.