cpp2html 0.1-alpha © 2002 Andrea Leofreddi. To get the source click here

// esercizio 2: Linked List con template
template<class T> class LinkedList {
	public:
		class ListNode; // predefinizione

	private:
		ListNode *head;

	public:
		class ListNode {
			public:
				T data;
				ListNode *next;

				ListNode(const T &copy)
				: data(copy), next(NULL) { };
				ListNode(const T &copy, ListNode *nnext)
				: data(copy), next(nnext) { };
		};

		// membri statici
		static const int exception_empty;

		// push
		void push(const T &nelement) {
			ListNode *newnode = new ListNode(nelement, head);
			head = newnode;
		}

		// pop
		T 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
template<class T> const int LinkedList<T>::exception_empty = 0x1;