0

Code Should i use &g on calling or *s in stack declaration?:

#include<iostream>
#include<stack>
using namespace std;

struct node{
    int data;
    struct node *link;
};

main(){
    stack<node> s;
    struct node *g;
    g = new node;
    s.push(g);
}

2 Answers2

1

Stack push() either copy the object, either move it. If you do not need share access to node objects, put them (not pointers) to stack via move semantic:

std::stack<node> st;
st.push(node());

http://en.cppreference.com/w/cpp/container/stack/push

Spock77
  • 3,256
  • 2
  • 30
  • 39
-1

I think you have an error at s.push(g) because type of gis node*, so you cannot put it into stack<node>. I think you should declare stack < node*> because it's easy to use with new operation.

user0042
  • 7,917
  • 3
  • 24
  • 39