Guest User

Untitled

a guest
Nov 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #ifndef _H_LIST
  2. #define _H_LIST
  3.  
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. template<class T>
  8. class CELEM {
  9. protected:
  10.     CELEM<T> *pNext;
  11.     CELEM<T> *pPrev;
  12.     T *pItem;
  13. public:
  14.     CELEM() {
  15.         pNext = pPrev = NULL;
  16.         pItem = NULL;
  17.     }
  18.     CELEM(CELEM<T>* pBefore, T*pI) {
  19.         pItem = pI;
  20.         pNext = pBefore->pNext;
  21.         pPrev = pBefore;
  22.         pBefore->pNext->pPrev = this;
  23.         pBefore->pNext = this;
  24.     }
  25.     ~CELEM() {
  26.         pNext->pPrev = pPrev;
  27.         pPrev->pNext = pNext;/* delete pItem; */
  28.     }
  29.     CELEM<T>* getNext() {
  30.         return pNext;
  31.     }
  32.     CELEM<T>* getPrev() {
  33.         return pPrev;
  34.     }
  35.     T* getItem() {
  36.         return pItem;
  37.     }
  38. };
  39.  
  40. template<class T>
  41. class CLIST: public CELEM<T> {
  42. public:
  43.     CLIST() :
  44.         CELEM<T> () {
  45.         this->pNext = this->pPrev = this;
  46.         this->pCurr = NULL;
  47.     }
  48.     int insHead(T*pI) {
  49.         return (this->pCurr = new CELEM<T> (this, pI)) ? 1 : 0;
  50.     }
  51.     int insTail(T*pI) {
  52.         return (this->pCurr = new CELEM<T> (this->pPrev, pI)) ? 1 : 0;
  53.     }
  54.     int insBef(T*pI) {
  55.         return this->pCurr ? ((this->pCurr = new CELEM<T> (this->pCurr->getPrev(), pI)) ? 1
  56.                 : 0) : 0;
  57.     }
  58.     int insBeh(T*pI) {
  59.         return this->pCurr ? ((this->pCurr = new CELEM<T> (this->pCurr, pI)) ? 1 : 0) : 0;
  60.     }
  61.     T* getFirst() {
  62.         return (this->pCurr = this->pNext)->getItem();
  63.     }
  64.     T* getLast() {
  65.         return (this->pCurr = this->pPrev)->getItem();
  66.     }
  67.     T* getNext() {
  68.         return (this->pCurr ? ((this->pCurr = this->pCurr->getNext())->getItem()) : NULL);
  69.     }
  70.     T* getPrev() {
  71.         return (this->pCurr ? ((this->pCurr = this->pCurr->getPrev())->getItem()) : NULL);
  72.     }
  73.     void remCurr() {
  74.         delete this->pCurr;
  75.         this->pCurr = NULL;
  76.     }
  77.     int insert(T* pI) {
  78.         for (T* p0 = getFirst(); p0 && (*p0 < *pI); p0 = getNext())
  79.             ;
  80.         return (this->pCurr = new CELEM<T> (this->pCurr->getPrev(), pI)) ? 1 : 0;
  81.     }
  82.     ~CLIST() {
  83.         T* p0;
  84.         while (p0 = getFirst())
  85.             remCurr();
  86.     }
  87. };
  88.  
  89. #endif
Add Comment
Please, Sign In to add comment