The output is:
THIS IS THE BEGINNING!
LIST 5 4 3 2 1 END
LIST 4 3 2 1 END
LIST 3 2 1 END
LIST 2 1 END
LIST 1 END
LIST END
#include <iostream.h>
class element ;
class list ;
typedef int element_type;
class element
{
private:
element_type value;
element *next;
public:
void setValue(element_type v)
{
value = v;
};
element_type getValue()
{
return value;
};
element()
{
value = 0;
next = NULL;
};
element(element_type v)
{
element();
setValue(v);
};
element * getNext(){
return next ;
}
void makeNext(element *a){
next = a;
};
// ~element();
element * print()
{
cout << getValue();
return next;
}
};
class list
{
public:
void addElement(element_type val)
{
element *tmp;
tmp = new element(val);
tmp->makeNext(head);
head = tmp;
};
element *popElement(){
element * tmp;
tmp = head;
head = head->getNext();
return tmp;
}
list()
{
head = NULL;
};
void print(){
element *current;
cout << "LIST " ;
current = head;
while( current != NULL)
{
current = current->print();
cout << " ";
}
cout << " END";
}
private:
element *head;
};
main(){
int i;
list *L;
cout << "THIS IS THE BEGINNING! \n";
L = new list;
for(i=1 ; i <= 5; i++)
{
L->addElement((element_type) i);
}
L->print();
cout << "\n";
for(i=1 ; i <= 5; i++) {
L->popElement();
L->print();
cout << "\n";
}
}