C++:
#include<iostream>
using namespace std;
template<class H> class Nodo{
private:
H key;
Nodo<H>* next;
public:
Nodo(H x) : key(x), next(NULL){
}
void setKey(H x){
key = x;
}
void setNext(Nodo<H>* next){
this->next = next;
}
Nodo<H>* getNext(){
return next;
}
H getKey(){
return key;
}
H* getPtrKey(){
return &key;
}
};
template<class H> class List{
private:
int n;
Nodo<H>* head;
public:
List(){
head = NULL;
n = 0;
}
List<H>* insert(H x){
Nodo<H>* prev = NULL;
Nodo<H>* i = head;
while(i != NULL)
{
prev = i;
i = i->getNext();
}
Nodo<H>* nd = new Nodo<H>(x);
n++;
if (prev == NULL)
head = nd;
else
prev->setNext(nd);
nd->setNext(i);
return this;
}
List<H>* del(H x){
Nodo<H>* prev = NULL;
Nodo<H>* i = head;
while(i != NULL && i->getKey() != x )
{
prev = i;
i = i->getNext();
}
if( i == NULL ) return this;
if( prev == NULL )
head = head->getNext();
else
prev->setNext(i->getNext());
n--;
return this;
}
void _removehead(){
if( head != NULL ){
Nodo<H>* succ = head->getNext();
delete head;
head = succ;
}
}
Nodo<H>* getHead() const{
return head;
}
void print(){
Nodo<H>* tmp = head;
while( tmp != NULL ){
cout << tmp->getKey() << " ";
tmp = tmp->getNext();
}
cout << endl;
}
friend ostream& operator<<(ostream& stream, const List<H>& l)
{
Nodo<H>* tmp = l.getHead();
while(tmp != NULL){
stream << tmp->getKey() << " ";
tmp = tmp->getNext();
}
return stream;
}
};
template<class H> class MultiList{
private:
List< List<H> >* l;
public:
MultiList(){
l = new List< List<H> >();
}
MultiList<H>* insert(H x){
Nodo< List<H> >* head = l->getHead();
if(head)
{
if( x%2 == 0)
head->getPtrKey()->insert(x);
else
{
List <H> aux;
aux.insert(x);
l->insert(aux);
}
}
return this;
}
void print(){
Nodo< List<H> >* tmp = l->getHead();
while(tmp != NULL){
cout << tmp->getKey() << " ";
tmp = tmp->getNext();
}
}
};
int main(){
List<int>* SingleList = new List<int>();
SingleList->insert(4)->insert(5)->insert(1)->insert(7)->print();
SingleList->del(4)->print();
MultiList<int>* DoubleList = new MultiList<int>();
DoubleList->insert(2)->insert(4)->insert(21)->insert(30)->insert(70)->insert(10);
DoubleList->print();
}
ragazzi non riesco a capire com'è possibile che la multilista che ho creato non visualizzi nulla, il fatto è che la head mi risulta null, quando invece nell'insert(credo di aver inserito correttamente i valori)