Binary Tree 2 Linked List

Discussion in 'C Programming' started by DanielJohnson, Feb 16, 2008.

  1. 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.
    DanielJohnson, Feb 16, 2008
    #1
    1. Advertising

  2. On Feb 16, 1:26 pm, Richard Heathfield <> wrote:
    > 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.
    >

    I have posted code there too. But I would say it won't hurt to
    express your opinion.
    DanielJohnson, Feb 16, 2008
    #2
    1. Advertising

  3. 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.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #3
  4. On Feb 16, 1:31 pm, Richard Heathfield <> wrote:
    > DanielJohnson said:
    >
    >
    >
    > > On Feb 16, 1:26 pm, Richard Heathfield <> wrote:
    > >> 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.

    >
    > > I have posted code there too. But I would say it won't hurt to
    > > express your opinion.

    >
    > 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
    DanielJohnson, Feb 16, 2008
    #4
  5. DanielJohnson said:

    > On Feb 16, 1:26 pm, Richard Heathfield <> wrote:
    >> 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.
    >>

    > I have posted code there too. But I would say it won't hurt to
    > express your opinion.


    using std::stack;

    Just a guess, mind.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #5
  6. DanielJohnson said:

    <snip>

    > 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.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #6

  7. > 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.
    >


    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.
    DanielJohnson, Feb 16, 2008
    #7
  8. DanielJohnson

    CBFalconer Guest

    DanielJohnson wrote:
    >
    > 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 17, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Snyke

    Leaf linked binary tree

    Snyke, Nov 7, 2005, in forum: Java
    Replies:
    0
    Views:
    487
    Snyke
    Nov 7, 2005
  2. fool
    Replies:
    14
    Views:
    501
    Barry Schwarz
    Jul 3, 2006
  3. joshd
    Replies:
    12
    Views:
    664
    John Carson
    Oct 2, 2006
  4. DanielJohnson

    Binary Tree 2 Linked List

    DanielJohnson, Feb 16, 2008, in forum: C++
    Replies:
    6
    Views:
    663
    Jeff Schwab
    Feb 17, 2008
  5. mac
    Replies:
    1
    Views:
    1,484
    Jens Thoms Toerring
    May 27, 2008
Loading...

Share This Page