// esercizio 1: Linked List
class Data {
private:
int data; // dato della class
public:
int get_data() { return data; };
Data(int ndata)
: data(ndata) { };
};
class LinkedList {
public:
class ListNode; // predefinizione
private:
ListNode *head;
public:
class ListNode {
public:
Data data;
ListNode *next;
ListNode(const Data ©)
: data(copy), next(NULL) { };
ListNode(const Data ©, ListNode *nnext)
: data(copy), next(nnext) { };
};
// membri statici
static const int exception_empty;
// push
void push(const Data &nelement) {
ListNode *newnode = new ListNode(nelement, head);
head = newnode;
}
// pop
Data pop() {
if(!head)
throw exception_empty;
ListNode *p = head;
head = head->next;
}
ListNode *get_head() {
return head;
}
ListNode *get_tail() {
ListNode *p;
for(p = head; p->next; p = p->next);
return p;
}
LinkedList()
: head(NULL) { };
};
// Inizializzazione delle static
const int LinkedList::exception_empty = 0x1;