0

I'm trying to create a recursive Linked List. At the moment I simply provide the class with two methods, one for tail insertion and a print. I don't understand why it doesn't print anything. I think the main problem is on recInsert(node,key) method who interprets the head node always as NULL. What I'm doing wrong?

I want to print the sequence 8->7->12->22

Here's my code:

 template<class H>class NodeList{
        private:
            NodeList<H> *prev,*next;
            H* key;

        public:
            NodeList(NodeList<H> *next,H *key){
            this->next = next;
            this->key = new H(*key);
        }

            NodeList<H> *getPrev(){return prev;}
            NodeList<H> *getNext(){return next;}

            void setPrev(NodeList<H> *prev){this->prev = prev;}
            void setNext(NodeList<H> *next){this->next = next;}

            void setKey(H *key){this->key = new H(*key);}
            H *getKey(){return key;}    
    };

    template<class H>class List{
        private:
            NodeList<H> *head;

        public:
            List(){
                head = NULL;
            }

        NodeList<H>* insTail(NodeList<H> *nod,H *key){
            if(nod == NULL){
                 nod = new NodeList<H>(nod,key);
            }
            else{
                nod->setNext(insTail(nod->getNext(),key));
            }
                return nod;
        }

        List<H> *ins(H key){
            insTail(head,&key);
            return this;
        }

        void recPrint(NodeList<H> *head){
            if(head == NULL){
                return;
            }
            else{
                cout<<*head->getKey();
                recPrint(head->getNext());
            }
        }

        void print(){
            recPrint(head);
            cout<<endl;
    }
        };

 int main(){
        List<int> *l = new List<int>();
        l->ins(8)->ins(7)->ins(12)->ins(22);
        l->print();

}

I've resolved adding a control on head node on insTail() method

NodeList<H>* insTail(NodeList<H> *nod,H *key){
        if(head == NULL)
            head = new NodeList<H>(NULL,key);
        if(nod == NULL){
             return new NodeList<H>(NULL,key);
        }
        else{
            nod->setNext(insTail(nod->getNext(),key));
            return nod;
        }
    }
10
  • 7
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: How to debug small programs Commented Sep 13, 2017 at 18:18
  • 1
    Don't use recursive function calls. They don't scale well. Use a loop and a std::stack instead. Commented Sep 13, 2017 at 18:21
  • 1
    Unless this is a learning exercise, you should just use std::list (or std::vector) and be done already, rather than re-inventing the wheel. Commented Sep 13, 2017 at 18:21
  • 2
    provide a main function too, write an example along with what you expect as its output and what it actually is printing Commented Sep 13, 2017 at 18:25
  • Thanks for all the answers,I've edited the post with the info .Normally I create the list iteratively.It's a learning exercise actually, so I'm not allowed to use std::list or vector. Commented Sep 13, 2017 at 18:34

1 Answer 1

1

You're almost there: head needs to be assigned the result of insTail:

head = insTail(head,&key);
Sign up to request clarification or add additional context in comments.

4 Comments

But in this case I provide an head insertion,not tail. Why my nod = new NodeList<H>(nod,key) won't work?
You are assigning nod to new. This does not affect head. It will remain nullptr.
Right! Many Thanks,I've misinterpreted your comment before.I've resolved adding a control on head node.
Love you back :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.