#ifndef H_LIST #define H_LIST #include "node.h" template class sList { private: sNode *mHead; sNode *mItem; long mCount; public: sList(); ~sList(); void add(T); void remove(); void clear(); void moveFirst(); void moveNext(); T getData(); bool find(T); long count(); bool eof(); long index(); void toIndex(long); }; template sList::sList() { mHead = new sNode(NULL, NULL, NULL); moveFirst(); // reset mCount = 0; } template sList::~sList() { delete mHead; // set off chain reaction } template void sList::add(T data) { sNode *a; a = new sNode(data, mHead, mHead->mNext); // bridge outward if (mHead->mNext != NULL) // if next node exists mHead->mNext->mPrev = a; // bridge backward mHead->mNext = a; // bridge forward moveFirst(); // reset mCount++; } template void sList::remove() { if (mItem != NULL) { mItem->mPrev->mNext = mItem->mNext; // bridge forward if (mItem->mNext != NULL) // if next node exists { mItem->mNext->mPrev = mItem->mPrev; // bridge backward mItem->mNext = NULL; // unhinge } delete mItem; // delete moveFirst(); // reset mCount--; } } template void sList::clear() { moveFirst(); // reset delete mItem; // set off chain reaction mCount = 0; } template void sList::moveFirst() { mItem = mHead->mNext; // move to first usable node } template void sList::moveNext() { if (mItem != NULL) mItem = mItem->mNext; } template T sList::getData() { if (mItem != NULL) return mItem->mData; return NULL; } template bool sList::find(T data) { for (moveFirst(); getData() != NULL; moveNext()) if (getData() == data) return true; return false; } template long sList::count() { return mCount; } template bool sList::eof() { if (mItem == NULL) return true; return false; } template long sList::index() { long result; sNode *a; result = 0; for (a = mHead->mNext; a != NULL; a = a->mNext) { if (a == mItem) return ((mCount - 1) - result); result++; } return -1; } template void sList::toIndex(long index) { int i; i = 0; for (moveFirst(); getData() != NULL; moveNext()) { if (i == ((mCount - 1) - index)) return; i++; } } #endif