Binary Tree 2 Linked List

D

DanielJohnson

I am not asking any solution but please tell me why g++ compiler gives
me the following error. I am using g++ as against gcc only because I
used #include<stack> from C++ STL to store the values in stack as
opposed to creating one of mine. All of my code is in C.

I am converting a Binary Tree to a Doubly Linked List by using an
inorder traversal and changing pointers.

btree struct looks like this

typedef struct btree{
struct btree* left;
int data;
struct btree* right;

} btree;

And this is my function, where I get the error from g++ compiler

btree* ConvertBinaryTree2LinkedList(btree* root){
btree *cursor = root;
btree *head = NULL; // return as first element of linked list
int isLeftMost = 1;
stack < btree* > S;
int done = 0;

// Iterative Inorder Traversal
while(!done){
if (cursor != NULL) {
S.push(cursor);
cursor=cursor->left;
if (isLeftMost) head = cursor;
}
else{
if(!S.empty()){
isLeftMost = 0;
cursor = S.top();
S.pop();
cursor->right = S.top();
S.top()->left = cursor;
cursor = cursor->right;
}
else{
done = 1;
}
}
}
return head;

}

BinaryTree2LinkedList.cpp: In function 'btree*
ConvertBinaryTree2LinkedList(btree*)':
BinaryTree2LinkedList.cpp:72: error: 'stack' was not declared in this
scope
BinaryTree2LinkedList.cpp:72: error: expected primary-expression
before '*' token
BinaryTree2LinkedList.cpp:72: error: expected primary-expression
before '>' token
BinaryTree2LinkedList.cpp:72: error: 'S' was not declared in this
scope

Please help to fix the error. Every help is appreciated.
 
D

DanielJohnson

DanielJohnson said:


...except the bit that's broken, which does appear to be in C++. Whilst I
am fairly sure I know what is causing your error, I am reluctant to
mention it because, if I'm wrong, I can't guarantee that there will be C++
people around to correct me. I suggest instead that you ask this question
in comp.lang.c++ where they will quickly set you back on track, I'm sure.
I have posted code there too. But I would say it won't hurt to
express your opinion.
 
R

Richard Heathfield

DanielJohnson said:
I am not asking any solution but please tell me why g++ compiler gives
me the following error. I am using g++ as against gcc only because I
used #include<stack> from C++ STL to store the values in stack as
opposed to creating one of mine. All of my code is in C.

....except the bit that's broken, which does appear to be in C++. Whilst I
am fairly sure I know what is causing your error, I am reluctant to
mention it because, if I'm wrong, I can't guarantee that there will be C++
people around to correct me. I suggest instead that you ask this question
in comp.lang.c++ where they will quickly set you back on track, I'm sure.
 
D

DanielJohnson

DanielJohnson said:





using std::stack;

Just a guess, mind.

I tried std::stack and now got a different error

/tmp/ccrVuyd4.o: In function `main':
BinaryTree2LinkedList.cpp:(.text+0x3a3): undefined reference to
`CovertBinaryTree2LinkedList(btree*)'
collect2: ld returned 1 exit status
 
R

Richard Heathfield

DanielJohnson said:

I tried std::stack and now got a different error

And it's an error in a C++ program, not a C program. If you're still having
problems, you really really need to ask in a C++ group, not a C group,
because your code is being compiled according to C++ rules, not C rules.
They really are different languages.

Having said that, this one looks like a misspelling.
/tmp/ccrVuyd4.o: In function `main':
BinaryTree2LinkedList.cpp:(.text+0x3a3): undefined reference to
`CovertBinaryTree2LinkedList(btree*)'

Find the call, and correct the spelling.
 
D

DanielJohnson

Having said that, this one looks like a misspelling.


Find the call, and correct the spelling.

My bad...Atleast it fixed this error. There are other errors in my
iterative implementations but I can atleast work on them.

Thanks Richard as always.
 
C

CBFalconer

DanielJohnson said:
I am not asking any solution but please tell me why g++ compiler
gives me the following error. I am using g++ as against gcc only
because I used #include<stack> from C++ STL to store the values
in stack as opposed to creating one of mine. All of my code is
in C.

Then why are you compiling it with a C++ compiler? The languages
are different.
 

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

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top