Advertisement
xSiRON

PROG2_Esame-08-04-2015

Apr 27th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class H> class SDList{
  5. public:
  6.     virtual SDList<H>* ins(H x) = 0;
  7.     virtual void print() = 0;  
  8. };
  9.  
  10. template <class H> class Nodo{
  11.     H key;
  12.     Nodo<H> *next, *prev;
  13.  
  14. public:
  15.     Nodo(H key, Nodo<H>* next = NULL, Nodo<H>* prev = NULL){
  16.         this->key = key;
  17.         this->next = next;
  18.         this->prev = prev;
  19.     }
  20.  
  21.     H getKey(){ return key; }
  22.     Nodo<H>* getNext(){ return next; }
  23.     Nodo<H>* getPrev(){ return prev; }
  24.  
  25.     void setNext(Nodo<H>* next){
  26.         this->next = next;
  27.     }
  28.  
  29.     void setPrev(Nodo<H>* prev){
  30.         this->prev = prev;
  31.     }
  32. };
  33.  
  34. template <class H> class MySDList : public SDList<H>{
  35.     Nodo<H> *header, *trailer;
  36.     Nodo<H> **array;
  37.  
  38.     Nodo<H>* _search(Nodo<H>* n){
  39.         for(int i = 0; array[i] && i < 9; i++){
  40.             Nodo<H>* tmp = array[i];
  41.             while(tmp != array[i+1]){
  42.                 if(tmp->getKey() >= n->getKey()) return tmp;
  43.                 tmp = tmp->getNext();
  44.             }
  45.         }
  46.         return NULL;
  47.     }
  48.  
  49.     Nodo<H>* __search(H x){
  50.         for(int i = 0; array[i] && i < 9; i++){
  51.             Nodo<H>* tmp = array[i];
  52.             while(tmp != array[i+1]){
  53.                 if(tmp->getKey() == x) return tmp;
  54.                 tmp = tmp->getNext();
  55.             }
  56.         }
  57.         return NULL;
  58.     }
  59.  
  60.     void ptrOrder(){
  61.         Nodo<H>* tmp = header->getNext();
  62.         int i = 0;
  63.         while(tmp != trailer){
  64.             while(tmp->getKey() > i*10)
  65.                 array[i++] = tmp;
  66.             tmp = tmp->getNext();
  67.         }
  68.     }
  69.  
  70. public:
  71.     MySDList(){
  72.         trailer = new Nodo<H>(0);
  73.         header = new Nodo<H>(0, trailer);
  74.         trailer->setPrev(header);
  75.         array = new Nodo<H>*[10];
  76.         for(int i = 0; i < 10; i++) array[i] = NULL;
  77.     }
  78.  
  79.     SDList<H>* ins(H x){
  80.         Nodo<H> *n = new Nodo<H>(x),
  81.                 *tmp = _search(n);
  82.  
  83.         if(tmp){
  84.             n->setNext(tmp);
  85.             n->setPrev(tmp->getPrev());
  86.             tmp->getPrev()->setNext(n);
  87.             tmp->setPrev(n);
  88.         }else{
  89.             n->setNext(trailer);
  90.             n->setPrev(trailer->getPrev());
  91.             trailer->getPrev()->setNext(n);
  92.             trailer->setPrev(n);
  93.         }
  94.  
  95.         ptrOrder();
  96.         return this;
  97.     }
  98.  
  99.     MySDList<H>* del(H x){
  100.         Nodo<H>* tmp = __search(x);
  101.         if(tmp){
  102.             int i = 0, j = 0;
  103.             while(!j){
  104.                 if(array[i] == tmp){
  105.                     j = 1;
  106.                     array[i] = NULL;
  107.                 }else i++;
  108.             }
  109.  
  110.             tmp->getPrev()->setNext(tmp->getNext());
  111.             tmp->getNext()->setPrev(tmp->getPrev());
  112.             delete tmp;
  113.  
  114.  
  115.  
  116.             ptrOrder();
  117.         }
  118.         return this;
  119.     }
  120.  
  121.     int search(H x){
  122.         Nodo<H>* tmp = __search(x);
  123.         if(tmp) return 1;
  124.         return 0;
  125.     }
  126.  
  127.     void print(){
  128.         Nodo<H>* tmp = header->getNext();
  129.         while(tmp != trailer){
  130.             cout << tmp->getKey() << " ";
  131.             tmp = tmp->getNext();
  132.         }
  133.     }
  134.  
  135.     void ptrPrint(){
  136.         for(int i = 0; array[i] && i < 10; i++)
  137.             cout << array[i]->getKey() << " ";
  138.         cout << endl;
  139.     }
  140.  
  141. };
  142.  
  143. int main(){
  144.     MySDList<int>* list = new MySDList<int>();
  145.     list->ins(7)->ins(13)->ins(2)->ins(6)
  146.         ->ins(9)->ins(10)->ins(21)->ins(32)->ins(4)->ins(12)->print();
  147.  
  148.     cout << endl << endl;
  149.     cout << list->search(13) << endl;
  150.     cout << list->search(21) << endl;
  151.     cout << list->search(25) << endl << endl;
  152.  
  153.     list->del(7)->del(4)->del(32)->del(10)->print();
  154.  
  155.     cout << endl << endl;
  156.  
  157.     list->ptrPrint();
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement