#include using namespace std; template class Node{ //friend class List;//!! public: //!! x data; Node *next; }; template class List{ private: Node *cur; Node *head; Node *prev; public: List(); //~List(); int ListIsEmpty(); int ListIsFull(); int CurIsEmpty(); void ToFirst(); int AtFirst(); int AtEnd(); void Advance(); void InsertAfter(x e); void Insert(x e); void Delete(); void StoreInfo(x e); void Error(); x RetrieveInfo(); x RetrieveNextInfo(); Node* MakeNode(x e); }; int main() { List l; l.Insert(2); l.Insert(4); l.Insert(6); l.Insert(8); int i; l.ToFirst(); do { i = l.RetrieveInfo(); cout << i; l.Advance(); }while(!l.CurIsEmpty()); return 0; } template List::List(){ cur = NULL; head = NULL; prev = NULL; } /*template List::~List(){ ToFirst(); while(!ListIsEmpty()){ Delete(); Advance(); } }*/ template int List::CurIsEmpty(){ if (cur == NULL) return 1; else return 0; } template int List::ListIsEmpty(){ if (head == NULL) return 1; else return 0; } template int List::ListIsFull(){ return 0; } template void List::ToFirst(){ cur = head; prev = NULL; } template int List::AtFirst(){ return (cur == head ? 1 : 0); } template int List::AtEnd(){ if (ListIsEmpty()) return 1; else if (CurIsEmpty()) return 0; else if (cur->next == NULL) return 1; else return 0; } template void List::Advance(){ if (CurIsEmpty()){ Error(); } else{ prev = cur; cur = cur->next; } } template void List::InsertAfter(x e){ Node *p; p = MakeNode(e); if (ListIsEmpty()){ head = p; cur = p; prev = NULL; } else if (CurIsEmpty()){ Error(); } else{ p->next = cur->next; cur->next = p; } } template void List::Insert(x e){ Node *p; p = MakeNode(e); if (ListIsEmpty()){ head = p; cur = p; prev = NULL; } else if (AtFirst()){ p->next = cur; head = p; cur = p; } else{ p->next = cur; cur = p; prev->next = p; } } template void List::Delete(){ if (CurIsEmpty()){ Error(); } else if (AtFirst()){ Node *p; p = cur; cur = cur->next; head = cur; delete p; } else { Node *p; p = cur; prev->next = cur->next; cur = cur->next; delete p; } } template void List::StoreInfo(x e){ if (CurIsEmpty()){ Error(); } else{ cur->data = e; } } template x List::RetrieveInfo(){ if (CurIsEmpty()){ Error(); return -1; } else{ return (cur->data); } } template x List::RetrieveNextInfo(){ if (ListIsEmpty() || CurIsEmpty() || AtEnd()){ Error(); return -1; } else{ return (cur->next->data); } } template Node* List::MakeNode(x e){ Node *p; p = new Node; p->data = e; p->next = NULL; return p; } template void List::Error(){ cout << "Error occured. no node exist!\n"; }