std::stack compilation error

R

Rahul

Hi,

I have the following program,

void dfs(struct node * root)
{
if(root == NULL) return;
std::stack <struct node *> s;
s.push(root);

while(!s.empty())
{
struct node * temp = s.pop();
printf("%d ",temp->data);
s.push(temp->rptr);
s.push(temp->lptr);
}
}

and i get a error saying,

ro.c: In function `void dfs(node*)':
ro.c:419: `stack' undeclared in namespace `std'

Am i missing something?

Thanks in advance ! ! !
 
C

Christian Hackl

Rahul said:
void dfs(struct node * root)
{
if(root == NULL) return;
std::stack <struct node *> s;

You don't need the struct keyword here.
s.push(root);

while(!s.empty())
{
struct node * temp = s.pop();

That's not how std::stack works. top() and pop() are separate functions:
http://www.cppreference.com/cppstack/index.html
printf("%d ",temp->data);
s.push(temp->rptr);
s.push(temp->lptr);
}
}

and i get a error saying,

ro.c: In function `void dfs(node*)':
ro.c:419: `stack' undeclared in namespace `std'

Am i missing something?

#include <stack> ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top