Advertisement
dmkozyrev

list

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