Advertisement
xSiRON

Lista Circolare

Jul 2nd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. template <class H> class CircularList{
  2.     Nodo<H> *head, *current;
  3.     int n;
  4.  
  5.     Nodo<H>* _search(H x){
  6.         Nodo<H>* tmp = head->getNext();
  7.         while(tmp != head && *tmp->getKey() != x)
  8.             tmp = tmp->getNext();
  9.         return tmp;
  10.     }
  11.  
  12. public:
  13.     CircularList(){
  14.         head = new Nodo<H>();
  15.         head->setNext(head);
  16.         head->setPrev(head);
  17.         n = 0;
  18.     }
  19.  
  20.     void insert(H x){
  21.         Nodo<H>* nd = new Nodo<H>(new H(x));
  22.         if(isEmpty()){
  23.             nd->setNext(head);
  24.             nd->setPrev(head);
  25.             head->setNext(nd);
  26.             head->setPrev(nd);
  27.         }else{
  28.             nd->setNext(head->getNext());
  29.             nd->setPrev(head);
  30.             head->getNext()->setPrev(nd);
  31.             head->setNext(nd);
  32.         }
  33.         n++;
  34.     }
  35.  
  36.     void del(H x){
  37.         Nodo<H>* tmp = _search(x);
  38.         if(tmp != head){
  39.             tmp->getNext()->setPrev(tmp->getPrev());
  40.             tmp->getPrev()->setNext(tmp->getNext());
  41.             delete tmp;
  42.             n--;
  43.         }
  44.     }
  45.  
  46.     H* begin(){
  47.         current = head;
  48.         if(head->getNext()) return next();
  49.         return NULL;
  50.     }
  51.  
  52.     H* next(){
  53.         if(current->getNext() == head) return NULL;
  54.         current = current->getNext();
  55.         return current->getKey();
  56.     }
  57.  
  58.     int isEmpty(){ return n == 0; }
  59.     int getSize(){ return n; }
  60. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement