Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _H_LIST
- #define _H_LIST
- #include <cstdlib>
- using namespace std;
- template<class T>
- class CELEM {
- protected:
- CELEM<T> *pNext;
- CELEM<T> *pPrev;
- T *pItem;
- public:
- CELEM() {
- pNext = pPrev = NULL;
- pItem = NULL;
- }
- CELEM(CELEM<T>* pBefore, T*pI) {
- pItem = pI;
- pNext = pBefore->pNext;
- pPrev = pBefore;
- pBefore->pNext->pPrev = this;
- pBefore->pNext = this;
- }
- ~CELEM() {
- pNext->pPrev = pPrev;
- pPrev->pNext = pNext;/* delete pItem; */
- }
- CELEM<T>* getNext() {
- return pNext;
- }
- CELEM<T>* getPrev() {
- return pPrev;
- }
- T* getItem() {
- return pItem;
- }
- };
- template<class T>
- class CLIST: public CELEM<T> {
- public:
- CLIST() :
- CELEM<T> () {
- this->pNext = this->pPrev = this;
- this->pCurr = NULL;
- }
- int insHead(T*pI) {
- return (this->pCurr = new CELEM<T> (this, pI)) ? 1 : 0;
- }
- int insTail(T*pI) {
- return (this->pCurr = new CELEM<T> (this->pPrev, pI)) ? 1 : 0;
- }
- int insBef(T*pI) {
- return this->pCurr ? ((this->pCurr = new CELEM<T> (this->pCurr->getPrev(), pI)) ? 1
- : 0) : 0;
- }
- int insBeh(T*pI) {
- return this->pCurr ? ((this->pCurr = new CELEM<T> (this->pCurr, pI)) ? 1 : 0) : 0;
- }
- T* getFirst() {
- return (this->pCurr = this->pNext)->getItem();
- }
- T* getLast() {
- return (this->pCurr = this->pPrev)->getItem();
- }
- T* getNext() {
- return (this->pCurr ? ((this->pCurr = this->pCurr->getNext())->getItem()) : NULL);
- }
- T* getPrev() {
- return (this->pCurr ? ((this->pCurr = this->pCurr->getPrev())->getItem()) : NULL);
- }
- void remCurr() {
- delete this->pCurr;
- this->pCurr = NULL;
- }
- int insert(T* pI) {
- for (T* p0 = getFirst(); p0 && (*p0 < *pI); p0 = getNext())
- ;
- return (this->pCurr = new CELEM<T> (this->pCurr->getPrev(), pI)) ? 1 : 0;
- }
- ~CLIST() {
- T* p0;
- while (p0 = getFirst())
- remCurr();
- }
- };
- #endif
Add Comment
Please, Sign In to add comment