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