Link to home
Create AccountLog in
Avatar of faal
faal

asked on

C++ program error using Microsoft visual studio 2005

I have a code consisting of a Node class(Node), a Linked list class (LinkedList), a N-ary Node class  (NNode), and a N-ary tree (NTree). The Node consist of a pointer points to next node and a NNode pointer that points to an N-ary Node, the linked list contain nodes, the N-ary Node contain of an integer data and a linked list, the N-ary tree contain N-ary Nodes. Here I have three files, MyStack.h : contain Node and LinkedList classes, NTree.h : contain NNode and NTree classes, and main.cpp : I am only calling the functions. Every time I compile the project I succeed (no errors), but when I run the project I get this warning:
Unhandled exception at 0x004119d2 in NTree.exe: 0xC0000005: Access violation reading location 0x00000000.
and I can't continue, I get it for the line (if (theHead == NULL && theCurrent == NULL)) in (void insert(NNode* thedata1)) function in (LinkedList) class, I don't know why I am getting this.

Here is the code:
main.cpp:

#include "NTree.h"
#include <iostream>

using namespace std;

int main()
{
      NTree mytree;

      mytree.addNode();
      //mytree.addNode();
      //mytree.addNode();
      mytree.Print();
      system("pause");
}


NTree.h :

#ifndef _NTREE_
#define _NTREE_

#include "MyStack.h"
#include <iostream>
#include <string>

using namespace std;

class NNode
{
public:
      friend class Node;
      int data;
      LinkedList* LL;
      NNode()
      {
            data = 0;
            LL = NULL;
      }
      NNode(int theData)
      {
            LL = NULL;
            data = theData;
      }
};

class NTree
{

private:
      NNode* root;
      int size;



public:


      NTree()
      {
            root = NULL;
            size = 0;
      }

      NNode* Search(NNode* current, int value)
      {
            if(current == NULL)
                  return NULL;
            else
            {
                  if(value == current->data)
                        return current;
                  else if(current->LL != NULL)
                     {
                        Node* current1 = current->LL->getTheHead();
                        NNode* current2 = current->LL->getChild(current1);
                        Search(current2, value);
                        current1 = current->LL->getNext(current1);
                  while(current1 != NULL)
                  {
                        current2 = current->LL->getChild(current1);
                        Search(current2, value);
                        current1 = current->LL->getNext(current1);
                  }
                    }
                  else
                  {
                        return NULL;
                  }
            }
      }

      void addNode()
      {
            root = new NNode(5);
            NNode* newcurrent = new NNode(3);;
            root->LL->insert(newcurrent);
            NNode* newcurrent1 = new NNode(4);;
            root->LL->insert(newcurrent1);
      }


      void Print(NNode* current)
      {
            if(current == NULL)
                  cout << "END" << endl;
            else
            {
                  cout << "Parent " << current->data;
                  if(current->LL != NULL)
                  {
                        //cout << " Children " << current->LL->display();

                        Node* current1 = current->LL->getTheHead();
                        NNode* current2 = current->LL->getChild(current1);
                        cout << " Children " << current2->data;
                        current1 = current->LL->getNext(current1);
                        while(current1 != NULL)
                        {
                              current2 = current->LL->getChild(current1);
                              cout << "," << current2->data;
                              current1 = current->LL->getNext(current1);
                        }
                        cout << endl;


                        current1 = current->LL->getTheHead();
                      current2 = current->LL->getChild(current1);
                        Print(current2);
                        current1 = current->LL->getNext(current1);
                        while(current1 != NULL)
                        {
                              current2 = current->LL->getChild(current1);
                              Print(current2);
                              current1 = current->LL->getNext(current1);
                        }
                  }
            }
      }


      void Print()
      {
            Print(root);
      }
};
#endif

MyStack.h :

#ifndef _MYSTACK_
#define _MYSTACK_

#include "NTree.h"
#include <iostream>
#include <string>

using namespace std;

/////////////////////////////////////
////// Node class
////////////////////////////////////

class Node
{
      friend class NNode;
 public:
        Node(NNode* theChild);// constructor
       
        void setData(NNode* theChild)  
            {
              child = theChild;          
            }
        NNode* getChild()             //get the character in the node
            {
            return child;        
           }
       
        void setNext(Node* next)    //setting the next node
            {
             theNext = next;
             }              
        Node* getNext()             //get the next node
            {
          return theNext;          
          }
 private:
        Node* theNext;
            NNode* child;
};


Node::Node(NNode* theChild)
{
                 
   theNext = NULL;
   child = theChild;
}

/////////////////////////////////////
// LinkedList class
////////////////////////////////////
class LinkedList
{
      friend class NTree;
    public:
           LinkedList();                      //constructor
           
           NNode* getCurrent()                  //get the current character
             {
               return theCurrent->getChild();  
               }
           
           void insert(NNode* thedata1)             //add a new character to the list
           {
                        
               
                Node* tmp = new Node(thedata1);
               
                if (theHead == NULL && theCurrent == NULL)
                {
                  theHead = theCurrent = tmp;
                 
                  theTail = theHead;
                  //theCurrent = theHead;
                  } //end of if
                else
                {
                 theCurrent = theHead;
                 while (theCurrent->getNext() != NULL)
                 {
                     theCurrent=theCurrent->getNext();
                    }//end of while
                  theCurrent->setNext(tmp);
                  theCurrent = tmp;
                  theTail = theCurrent;
                  }//end of  else
                };//end of function
           
           
            void deleteList()                
             {
                 Node* tmp = theHead;
                 if (theHead == theCurrent)
                  {
                             
                   theHead = theCurrent = theTail = theHead->getNext();
                   return;
                   }
                  else {
                       while (tmp->getNext() != NULL)
                       {
                         if (tmp->getNext() == theCurrent)
                         {
                                           
                            tmp->setNext(theCurrent->getNext());
                            theCurrent = tmp;
                            return;                
                           }//end of if
                         tmp = tmp->getNext();  
                       }//end of while
                  }//end of else
              }//end of delete
         
         NNode* removeHead()                  //remove the the character in the head of the list
             {
              Node* tmp = theHead;
               theHead = theHead->getNext();
               
              return tmp->getChild();    
           }

         NNode* getHead()                     //get the character in the head of the list
         {
           return theHead->getChild();            
         }
             Node* getTheHead()
             {
                   return theHead;
             }

         bool isEmpty()                     //check if the list is empty
         {
           if (theHead == NULL)
             return true;
             return false;  
         }//end of function isEmpty()  

             NNode* getChild(Node* current)
             {
                   if(theHead == NULL)
                         return NULL;
                   else
                         return current->getChild();
             }

             Node* getNext(Node* current)
             {
                   if (current->getNext() == NULL)
                         return NULL;
                   else
                         return current->getNext();
             }
       

             void display()
             {
                   Node* current = theHead;
                   NNode* currChild;

                   while(current != NULL)
                   {
                         currChild = current->getChild();
                         current = current->getNext();
                   }
                   cout << endl;
             }

             NNode* getTail( Node* current)
             {

                   while(current->getNext() != NULL)
                   {
                         current = current->getNext();
                   }
                   return current->getChild();
             }

         
                       
    //private:
    Node* theHead;
    Node* theCurrent;
    Node* theTail;
         
};

LinkedList::LinkedList()
{
       theHead = NULL;
       theCurrent = NULL;
       theTail = NULL;                        
}
#endif
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Addenum, after changing the constructors to

      NNode()
      {
            data = 0;
            LL = new LinkedList;
      }
      NNode(int theData)
      {
            LL = new LinkedList;
            data = theData;
      }

you also need to clean up, i.e.
//NTree.h :
 
#ifndef _NTREE_
#define _NTREE_
 
#include "MyStack.h"
#include <iostream>
#include <string>
 
using namespace std;
 
class NNode
{
public:
      friend class Node;
      int data;
      LinkedList* LL;
      NNode()
      {
            data = 0;
            LL = new LinkedList;
      }
      NNode(int theData)
      {
            LL = new LinkedList;
            data = theData;
      }
      NNode()
      {
            delete LL; // cleanup
      }
};

Open in new window

>>>> class NNode
>>>> {
>>>> public:
>>>>       friend class Node;
>>>>       int data;
>>>>       LinkedList* LL;

You should overthink your design. If a NNode *has* a pointer to a LinkedList as member things go wrong. It is as if a tail wags its dog and not the other way.

You should have like

class Node
{
        int data;
        Node* next;
        friend class LinkList;
};

class LinkList
{
      Node* root;
      Node* tail;

public:
      ...
};