Advertisement
dmkozyrev

list

Jan 12th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T> class List;
  5.  
  6. template<class T>
  7. class Node
  8.  {
  9.     T data;
  10.     Node<T> * next, * prev;
  11.  public:
  12.     Node(const T & d, Node<T> * n = 0, Node<T> * p = 0);
  13.     ~Node();
  14.    
  15.     void setData(const T & d){data = d;};
  16.     void setNext(Node<T> * n){next = n; if (n != 0) n->prev = this;};
  17.     void setPrev(Node<T> * n){prev = n; if (n != 0) n->next = this;};
  18.    
  19.     T getData() const {return data;};
  20.     Node<T> * getNext() const {return next;};
  21.     Node<T> * getPrev() const {return prev;};
  22.    
  23.     friend List<T>;
  24.  };
  25.  
  26.  template <class T>
  27.  class List
  28.  {
  29.     Node<T> * head, * tail;
  30.    
  31.     int increment(long inc[], long size);
  32.  public:
  33.     List(){head = 0; tail = 0;};
  34.     ~List();
  35.     void show() const;
  36.     void addFront(const T & d);
  37.     void addBack(const T & d);
  38.     Node<T> * cutFront();
  39.     Node<T> * cutBack();
  40.     void delFront(){delete cutFront();};
  41.     void delBack(){delete cutBack();};
  42.    
  43.     int size() const;
  44.     Node<T> * getHead() const {return head;};
  45.     Node<T> * getTail() const {return tail;};
  46.     Node<T> * get(int) const;
  47.     Node<T> * operator [](int i) const {return get(i);};
  48.     void shellSort();
  49.  };
  50.  
  51.  int main()
  52.  {
  53.     List<int> l;
  54.     for(int i = 0; i < 10; l.addFront(i++));
  55.     l.show();
  56.     l.shellSort();
  57.     l.show();
  58.  }
  59.  
  60. template <class T>
  61. int List<T>::increment(long inc[], long N)
  62. {
  63.   int p1, p2, p3, s;
  64.  
  65.   p1 = p2 = p3 = 1;
  66.   s = -1;
  67.   do
  68.   {
  69.         if (++s % 2)
  70.             inc[s] = 8*p1 - 6*p2 + 1;
  71.     else
  72.     {
  73.       inc[s] = 9*p1 - 9*p3 + 1;
  74.       p2 *= 2;
  75.       p3 *= 2;
  76.     }
  77.         p1 *= 2;
  78.   } while (3*inc[s] < N);  
  79.  
  80.   return s > 0 ? --s : 0;
  81. }
  82.  
  83. template<class T>
  84. void List<T>::shellSort()
  85. {
  86.   long k, i, j, seq[40], N = size();
  87.   int s;
  88.  
  89.   // вычисление последовательности приращений
  90.   s = increment(seq, N);
  91.   while (s >= 0) {
  92.     // сортировка вставками с инкрементами inc[]
  93.     k = seq[s--];
  94.  
  95.     for (i = k; i < N; i++) {
  96.       T temp = get(i) -> data;
  97.       for (j = i-k; (j >= 0) && (get(j) -> data > temp); j -= k)
  98.         get(j+k) -> setData(get(j) -> getData());
  99.       get(j+k) -> setData(temp);
  100.     }
  101.   }
  102. }
  103.  
  104.  template<class T>
  105.  Node<T>::Node(const T & d, Node<T> * n, Node<T> * p)
  106.  {
  107.     data = d;
  108.    
  109.     next = n;
  110.     if (n != 0)
  111.         n -> prev = this;
  112.    
  113.     prev = p;
  114.     if(p != 0)
  115.         p -> next = this;
  116.  }
  117.  
  118.  template<class T>
  119.  Node<T>::~Node()
  120.  {
  121. //  cout << "Node destructor start"<< endl;
  122.    
  123.     if (next != 0)
  124.     {
  125. //      cout << "Node destructor: next" << endl;
  126.         Node<T> * temp = next;
  127.         next = 0;
  128.         temp -> prev = 0;
  129.         delete temp;
  130.     }
  131.    
  132. //  cout << "Node destructor continue" << endl;
  133.    
  134.     if (prev != 0)
  135.     {
  136. //      cout << "Node destructor: prev" << endl;
  137.         Node<T> * temp = prev;
  138.         prev = 0;
  139.         temp -> next = 0;
  140.         delete temp;
  141.     }
  142.    
  143. //  cout << "Node destructor final" << endl;
  144.  }
  145.  
  146. template <class T>
  147. int List<T>::size() const
  148. {
  149.     Node<T> * node = head;
  150.     int count = 0;
  151.     while (node != 0)
  152.     {
  153.         node = node -> next;
  154.         ++count;
  155.     }
  156.     return count;
  157. }
  158. template <class T>
  159. Node<T> * List<T>::get(int i) const
  160. {
  161.     if (i < 0) i = 0;
  162.     Node<T> * node = head;
  163.     for(int j = 0; j < i && node -> next != 0; ++j)
  164.         node = node -> next;
  165.     return node;
  166. }
  167.  
  168. template <class T>
  169. void List<T>::show() const
  170. {
  171.     for (Node<T> * node = head; node != 0; node = node -> next)
  172.         cout << node -> data << " ";
  173.     cout << endl;
  174. }
  175.  
  176. template <class T>
  177. List<T>::~List()
  178. {  
  179. //  cout << "List destructor start" << endl;
  180.    
  181.     if (head != 0){
  182. //      cout << "List destructor: head" << endl;
  183.         Node<T> * temp = head;
  184.         head = 0;
  185.         delete temp;
  186.     }
  187.        
  188. //  cout << "List destructor continue" << endl;
  189.    
  190.     if (tail != 0)
  191.     {
  192. //      cout << "List destructor: tail" << endl;
  193.         Node<T> * temp = tail;
  194.         tail = 0;
  195.         delete temp;
  196.     }
  197.    
  198. //  cout << "List destructor final" << endl;
  199. }
  200.  
  201. template<class T>
  202. void List<T>::addFront(const T & d)
  203. {
  204.     head = new Node<T>(d, head, 0);
  205.     if (tail == 0)
  206.         tail = head;
  207. }
  208.  
  209. template<class T>
  210. void List<T>::addBack(const T & d)
  211. {
  212.     tail = new Node<T>(d, 0, tail);
  213.     if (head == 0)
  214.         head = tail;
  215. }
  216.  
  217.  
  218. template<class T>
  219. Node<T> * List<T>::cutFront()
  220. {
  221.     Node<T> * node = head;
  222.     if (node != 0)
  223.     {
  224.         head = head -> next;
  225.         node -> next = 0;
  226.         head -> prev = 0;
  227.     }
  228.     return node;
  229. }
  230.  
  231. template<class T>
  232. Node<T> * List<T>::cutBack()
  233. {
  234.     Node<T> * node = tail;
  235.     if (node != 0)
  236.     {
  237.         tail = tail -> prev;
  238.         node -> prev = 0;
  239.         tail -> next = 0;
  240.     }
  241.     return node;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement