template struct elem_link1 { T inf; elem_link1 *link; }; template class LList { public: LList(); ~LList(); LList(LList const &); LList& operator=(LList const &); bool empty() const; void IterStart(elem_link1* = NULL); elem_link1* Iter(); void ToEnd(T const &); void InsertAfter(elem_link1 *, T const &); void InsertBefore(elem_link1 *, T const &); void DeleteElem(elem_link1 *, T &); int DeleteAfter(elem_link1 *, T &); int DeleteBefore(elem_link1 *, T &); int length(); void concat(LList const &); void reverse(); void print(); private: elem_link1 *Start, *End, *Current; void DeleteList(); void CopyList(LList const &); }; template LList::LList() { Start = End = Current = NULL; } template LList::~LList() { DeleteList(); } template LList::LList(LList const &r) { CopyList(r); } template LList& LList::operator=(LList const &r) { if (this != &r) { DeleteList(); CopyList(r); } return *this; } template void LList::DeleteList() { if (Start) { elem_link1 *p; while (Start) { p = Start; Start = Start->link; delete p; } End = NULL; } } template void LList::CopyList(LList const &r) { Start = End = NULL; elem_link1 *p = r.Start; while (p) { ToEnd(p->inf); p = p->link; } } template bool LList::empty() const { return Start == NULL; } template void LList::IterStart(elem_link1 *p) { if (p) Current = p; else Current = Start; } template elem_link1* LList::Iter() { elem_link1 *p = Current; if (Current) Current = Current->link; return p; } template int LList::length() { int n = 0; IterStart(); while (Iter()) n++; return n; } template void LList::ToEnd(T const &x) { Current = End; End = new elem_link1; End->inf = x; End->link = NULL; if (Current) Current->link = End; else Start = End; } template void LList::InsertAfter(elem_link1 *p, T const &x) { elem_link1 *q = new elem_link1; q->inf = x; q->link = p->link; if (p == End) End = q; p->link = q; } template void LList::InsertBefore(elem_link1 *p, T const &x) { elem_link1 *q = new elem_link1; *q = *p; p->inf = x; p->link = q; if (End == p) End = q; } template int LList::DeleteAfter(elem_link1 *p, T &x) { if (p == End) return 0; elem_link1 *q = p->link; x = q->inf; p->link = q->link; if (End == q) End = p; delete q; return 1; } template void LList::DeleteElem(elem_link1 *p, T &x) { if (p == Start) { x = p->inf; if (Start == End) { Start = NULL; End = NULL; delete p; } else { Start = Start->link; delete p; } } else { elem_link1 *q = Start; while (q->link != p) q = q->link; DeleteAfter(q,x); } } template int LList::DeleteBefore(elem_link1 *p, T &x) { if (p == Start) return 0; elem_link1 *q = Start; while (q->link != p) q = q->link; DeleteElem(q,x); return 1; } template void LList::concat(LList const &L) { elem_link1 *p = L.Start; while (p) { ToEnd(p->inf); p = p->link; } } template void LList::reverse() { LList L; elem_link1 *p = Start; if (p) { L.ToEnd(p->inf); p = p->link; while (p) { L.InsertBefore(L.Start, p->inf); p = p->link; } } *this = L; } template void LList::print() { elem_link1 *p = Start; while (p) { cout<< p->inf <<" "; p = p->link; } cout<<"\n"; }