Advertisement
Gilgamesh858

esame-150302.cpp

Jun 9th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename H> class MultiList {
  5.     public:
  6.     virtual MultiList<H>* ins(H x) = 0;
  7.     virtual int multiplicity(H x) = 0;
  8.     virtual void print() = 0;
  9. };
  10.  
  11. template <typename H> class Node
  12. {  
  13.     private:
  14.         H key;
  15.         Node<H> *next, *prev;
  16.         int mul;
  17.     public:
  18.         Node(H x)
  19.         {
  20.             key = x;
  21.             mul = 1;
  22.             next = prev = NULL;
  23.         }
  24.         H getKey(){ return key; }
  25.         int getMul(){ return mul; }
  26.         Node<H>* getNext() { return next; }
  27.         Node<H>* getPrev() { return prev; }
  28.         void setNext(Node<H>* next) { this->next = next; }
  29.         void setPrev(Node<H>* prev) { this->prev = prev; }
  30.         void incMul(){ mul++; }
  31.         void decMul(){ mul--; }
  32. };
  33.  
  34. template <typename H> class MyMultiList: public MultiList<H>
  35. {
  36.     private:
  37.         Node<H> *head, *tail;
  38.  
  39.         Node<H>* isBigger(H x){
  40.             Node<H> *temp = head;
  41.             while(temp){
  42.                 if( temp->getKey() > x )
  43.                     return temp->getPrev();
  44.                 temp = temp->getNext();
  45.             }
  46.             return tail;
  47.         }
  48.  
  49.     public:
  50.         MyMultiList(){
  51.             head = tail = NULL;
  52.         }
  53.         bool isEmpty(){
  54.             if(!head)
  55.                 return true;
  56.             return false;          
  57.         }
  58.         MyMultiList<H>* ins(H x){
  59.             if(isEmpty()){
  60.                 Node<H>* temp = new Node<H>(x);
  61.                 head = tail = temp;
  62.                 return this;
  63.             }
  64.             Node<H> *p = isBigger(x);
  65.             if(p == NULL){
  66.                 Node<H>* temp = new Node<H>(x);
  67.                 temp->setNext(head);
  68.                 head->setPrev(temp);
  69.                 head = temp;
  70.                 return this;
  71.             }
  72.             if(p && p!=tail){
  73.                 if(p->getKey() == x){
  74.                     p->incMul();
  75.                     return this;
  76.                 }
  77.                 Node<H>* temp = new Node<H>(x);
  78.                 temp->setNext(p->getNext());
  79.                 p->getNext()->setPrev(temp);
  80.                 p->setNext(temp);
  81.                 temp->setPrev(p);
  82.                 return this;
  83.             }  
  84.             if(p->getKey() == x){
  85.                 p->incMul();
  86.                 return this;
  87.             }
  88.             Node<H>* temp = new Node<H>(x);
  89.             tail->setNext(temp);
  90.             temp->setPrev(tail);
  91.             tail = temp;
  92.             return this; // รจ___รฉ
  93.         }
  94.         Node<H>* search(H x){
  95.             if(isEmpty()){
  96.                 cout << "Lista Vuota!" << endl;
  97.                 return NULL;
  98.             }
  99.             Node<H> *temp = head;
  100.             while(temp){
  101.                 if(temp->getKey() == x)
  102.                     return temp;
  103.                 temp = temp->getNext();
  104.             }
  105.             return NULL;
  106.         }
  107.         int rank(H x){
  108.             if(isEmpty()){
  109.                 return 0;
  110.             }
  111.             if(!search(x))
  112.                 return 0;
  113.             int i = 1;
  114.             Node<H> *temp = head;
  115.             while(temp->getKey() != x){
  116.                 i += temp->getMul();
  117.                 temp = temp->getNext();
  118.             }
  119.             return i;
  120.         }
  121.         MyMultiList<H>* del(H x){
  122.             if(isEmpty()){
  123.                 cout << "Lista Vuota!" << endl;
  124.                 return this;
  125.             }
  126.             if(!search(x)){
  127.                 cout << "Elemento Inesistente " << x << endl;
  128.                 return this;
  129.             }
  130.             Node<H>* temp = search(x);
  131.             if(temp->getMul() > 1){
  132.                 temp->decMul();
  133.                 return this;
  134.             }
  135.             if(temp == head){
  136.                 head = head->getNext();
  137.                 head->setPrev(NULL);
  138.                 delete(temp);
  139.                 return this;
  140.             }
  141.             if( temp == tail ){
  142.                 tail = tail->getPrev();
  143.                 tail->setNext(NULL);
  144.                 delete(temp);
  145.                 return this;
  146.             }
  147.             temp->getPrev()->setNext(temp->getNext());
  148.             temp->getNext()->setPrev(temp->getPrev());
  149.             temp->setPrev(NULL);
  150.             temp->setNext(NULL);
  151.             delete(temp);
  152.             return this;
  153.         }
  154.  
  155.         int multiplicity(H x){
  156.             if(!search(x))
  157.                 return 0;
  158.             return search(x)->getMul();
  159.         }
  160.         void print(){
  161.             if(isEmpty()){
  162.                 cout << "Lista Vuota!" << endl;
  163.                 return;
  164.             }
  165.             Node<H> *temp = head;
  166.             while(temp){
  167.                 for(int i = 0 ; i < temp->getMul() ; i++){
  168.                     cout << temp->getKey();
  169.                     if(temp->getNext() != NULL || i < temp->getMul()-1)
  170.                         cout  << ",";
  171.                 }
  172.                 temp = temp->getNext();
  173.             }
  174.             cout << endl;
  175.         }
  176.  
  177. };
  178.  
  179. int main(){
  180.     MyMultiList<int> *T = new MyMultiList<int>();
  181.     T->ins(10)->ins(5)->ins(8)->ins(3)->ins(5)->ins(1)->ins(6)->ins(5)->ins(8)->ins(1)->ins(12)->ins(7);
  182.     T->print();
  183.     T->del(6)->del(5)->del(8)->del(3)->del(8);
  184.     T->print();
  185.     cout << T->rank(5) << " " << T->rank(6) << " " << T->rank(12) << endl;
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement