SHARE
TWEET

list

a guest Aug 20th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef LISTA_H_INCLUDED
  2. #define LISTA_H_INCLUDED
  3.  
  4. #include <iostream>
  5.  
  6.  
  7. template<typename T>
  8. struct node
  9. {
  10.     T dato;
  11.     node* nextNode;
  12.     node* previousNode;
  13.  
  14.     node(const T& elem,node* next,node* previous):dato(elem),
  15.     nextNode(next),previousNode(previous){}
  16.  
  17. };
  18.  
  19. template<typename T>
  20. class lista
  21. {
  22.     size_t listSize;
  23.     node<T>* listFront;
  24.     node<T>* listBack;
  25.  
  26. public:
  27.     lista()
  28.     {
  29.         listSize = 0;
  30.         listFront = nullptr;
  31.         listBack = nullptr;
  32.     }
  33.     ~lista()
  34.     {
  35.  
  36.     }
  37.     bool empty() const;
  38.     size_t size() const;
  39.     void push_front(const T& elem);
  40.     void push_back(const T& elem);
  41.     const T& front() const;
  42.     const T& back() const;
  43.     void pop_front();
  44.     void pop_back();
  45.     void sort();
  46.     T& operator [](size_t idx) const;
  47. };
  48.  
  49. template<typename T>
  50. bool lista<T>::empty() const
  51. {
  52.     return listSize == 0;
  53. }
  54.  
  55. template<typename T>
  56. size_t lista<T>::size() const
  57. {
  58.     return listSize;
  59. }
  60.  
  61. template<typename T>
  62. void lista<T>::push_front(const T &elem)
  63. {
  64.     node<T>* inserted;
  65.     if(empty())
  66.     {
  67.         inserted = new node(elem,listBack,listFront);
  68.         listFront = inserted;
  69.         listBack = inserted;
  70.  
  71.     }
  72.     else
  73.     {
  74.        inserted = new node(elem, listBack, nullptr);
  75.        listFront->previousNode = inserted;
  76.        listFront = inserted;
  77.     }
  78.     ++listSize;
  79. }
  80.  
  81. template<typename T>
  82. void lista<T>::push_back(const T &elem)
  83. {
  84.     node<T>* inserted;
  85.     if(empty())
  86.     {
  87.         inserted = new node(elem,listBack,listFront);
  88.         listFront = inserted;
  89.         listBack = inserted;
  90.  
  91.     }
  92.     else
  93.     {
  94.        inserted = new node(elem, nullptr, listFront);
  95.        listFront->nextNode = inserted;
  96.        listFront = inserted;
  97.     }
  98.     ++listSize;
  99. }
  100.  
  101. template<typename T>
  102. const T &lista<T>::front() const
  103. {
  104.     if(empty())
  105.         throw invalid_argument("esta vacia");
  106.     return listFront->dato;
  107. }
  108.  
  109. template<typename T>
  110. const T &lista<T>::back() const
  111. {
  112.     if(empty())
  113.         throw invalid_argument("esta vacia");
  114.     return listBack->dato;
  115. }
  116.  
  117. template<typename T>
  118. void lista<T>::pop_front()
  119. {
  120.     if(empty())
  121.         throw invalid_argument("Esta vacia");
  122.     node<T>* eliminar = listFront;
  123.     listFront = listFront->nextNode;
  124.     if(listFront != nullptr)
  125.         listFront->anterior = nullptr;
  126.     delete eliminar;
  127.     eliminar = nullptr;
  128.     --listSize;
  129. }
  130.  
  131. template<typename T>
  132. void lista<T>::pop_back()
  133. {
  134.     if(empty())
  135.         throw invalid_argument("Esta vacia");
  136.     node<T>* eliminar = listBack;
  137.     listBack = listBack->previousNode;
  138.     if(listBack != nullptr)
  139.         listBack->nextNode=nullptr;
  140.     delete eliminar;
  141.     eliminar = nullptr;
  142.     --listSize;
  143. }
  144.  
  145. template<typename T>
  146. void lista<T>::sort()
  147. {
  148.     node<T> *temp = listFront;
  149.     node<T> *sig = temp->nextNode;
  150.     for (size_t k = 0; k < listSize - 1; ++k) {
  151.         for (size_t i = 0; i < listSize - 1; ++i) {
  152.             if (sig->dato < temp->dato){
  153.                 T auxiliar = temp->dato;
  154.                 temp->dato = sig->dato;
  155.                 sig->dato = auxiliar;
  156.             }
  157.             temp = temp->nextNode;
  158.             sig = temp->nextNode;
  159.         }
  160.         temp = listFront;
  161.         sig = temp->nextNode;
  162.     }
  163. }
  164.  
  165.  
  166. template<typename T>
  167. T &lista<T>::operator [](size_t idx) const
  168. {
  169.     if(empty())
  170.         throw invalid_argument("Esta vacia");
  171.     else if(idx >= listSize)
  172.         throw invalid_argument("index fuera de rango");
  173.     node<T>* temp = listFront;
  174.     for(size_t i = 0; i < idx;++i)
  175.         temp = temp->nextNode;
  176.     return temp->dato;
  177. }
  178.  
  179. #endif // LISTA_H_INCLUDED
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top