I'm trying to create a nested linked list like this (list<list<list<Object> > >
) with a custom singly linked list for an assignment.
the problem is that I tested it and the program returns a "memory could not be read error" and terminates. This is the code I used to test it
int main (){
List<List<List<int> > > thirdLevelNumbers;
for(int i = 0; i < 10; i++){
List<List<int> > secondLevelNumbers;
for(int j = 0; j < 10; j++){
List<int> firstLevelNumbers;
for(int n = 0; n < 10; n++){
firstLevelNumbers.push_front(n);
}
secondLevelNumbers.push_front(firstLevelNumbers);
}
thirdLevelNumbers.push_front(secondLevelNumbers);
}
cout << "processed!" << endl;
system("PAUSE");
return 0;
}
Then I tried with the stl list and it worked!
So, I was wondering if anyone here could help me understand what part of the code causes the error and how to solve it.
I use eclipse with gcc (GCC) 4.5.2 and this is my linked list's code so far:
template <class T> class List;
template <class T>
class node
{
T info;
node<T>* next;
friend class List<T>;
};
template <class T>
class List
{
private:
node<T>* First;
public:
List()
{
First=NULL;
};
~List()
{
node<T> *p;
while (!isEmpty())
{
p=First->next;
delete First;
First=p;
};
};
bool isEmpty()
{
return First == NULL;
};
bool isFull()
{
node<T> *p;
p=new node<T>;
if (p==NULL)
return true;
else
{
delete p;
return false;
}
};
bool push_front(const T &Valor)
{
node<T>* newNode;
if (!isFull())
{
newNode=new node<T>;
newNode->info=Valor;
newNode->next=First;
First=newNode;
return true;
}
else
return false;
};
};
>`).