Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <time.h>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. struct Node
  11. {
  12.     int i;
  13.     double d;
  14.     char c;
  15.     Node* prev;
  16.     Node* next;
  17. };
  18.  
  19. class List
  20. {
  21. public:
  22.     List()
  23.     {
  24.         m_head = NULL;
  25.     }
  26.  
  27.     bool add_node(int key)
  28.     {
  29.         if (m_head == NULL)
  30.         {
  31.             Node* node = new Node();
  32.             node->c = 'T';
  33.             node->d = double(rand() % 1000) + double(rand() % 100) / 100.0;
  34.             node->i = key;
  35.             node->prev = node;
  36.             node->next = node;
  37.  
  38.             m_head = node;
  39.             return true;
  40.         }
  41.         else
  42.         {
  43.             if (key < m_head->i)
  44.             {
  45.                 Node* newHead = new Node();
  46.                 newHead->c = 'T';
  47.                 newHead->d = double(rand() % 1000) + double(rand() % 100) / 100.0;
  48.                 newHead->i = key;
  49.                 newHead->prev = m_head->prev;
  50.                 newHead->next = m_head;
  51.  
  52.                 m_head->prev->next = newHead;
  53.                 m_head->prev = newHead;
  54.                 m_head = newHead;
  55.                 return true;
  56.             }
  57.             else if (key == m_head->i)
  58.             {
  59.                 return false;
  60.             }
  61.             else if (key > m_head->prev->i)
  62.             {
  63.                 Node* newTail = new Node();
  64.                 newTail->c = 'T';
  65.                 newTail->d = double(rand() % 1000) + double(rand() % 100) / 100.0;
  66.                 newTail->i = key;
  67.                 newTail->prev = m_head->prev;
  68.                 newTail->next = m_head;
  69.  
  70.                 m_head->prev->next = newTail;
  71.                 m_head->prev = newTail;
  72.                 return true;
  73.             }
  74.             else if (key == m_head->prev->i)
  75.                 return false;
  76.             else
  77.             {
  78.                 Node* actual = m_head;
  79.                 Node* tail = m_head->prev;
  80.  
  81.                 while (actual != tail)
  82.                 {
  83.                     if (actual->i == key)
  84.                     {
  85.                         return false;
  86.                     }
  87.                     else if (actual->i < key && actual->next->i > key)
  88.                     {
  89.                         Node* newNode = new Node();
  90.                         newNode->c = 'T';
  91.                         newNode->d = double(rand() % 1000) + double(rand() % 100) / 100.0;
  92.                         newNode->i = key;
  93.                         newNode->prev = actual;
  94.                         newNode->next = actual->next;
  95.  
  96.                         actual->next->prev = newNode;
  97.                         actual->next = newNode;
  98.                         return true;
  99.                     }
  100.                     actual = actual->next;
  101.                 }
  102.             }
  103.             return false;
  104.         }
  105.     }
  106.  
  107.     void add_nodes(int count)
  108.     {
  109.         srand(time(NULL));
  110.         while (count > 0)
  111.         {
  112.             int random_number = (rand() + rand() + rand()) % 99901 + 99;
  113.             if (add_node(random_number)) count--;
  114.         }
  115.        
  116.     }
  117.  
  118.     Node* find(int key)
  119.     {
  120.         if (m_head == NULL)
  121.             return NULL;
  122.  
  123.         Node* actual = m_head;
  124.  
  125.         do {
  126.             if (actual->i == key) return actual;
  127.             actual = actual->next;
  128.         } while (actual != m_head);
  129.  
  130.         return NULL;
  131.     }
  132.  
  133.     bool erase(int key)
  134.     {
  135.         Node* node = find(key);
  136.         if (node == NULL)
  137.             return false;
  138.  
  139.         if (element_count() == 1)
  140.         {
  141.             m_head = NULL;
  142.         }
  143.         else if (node == m_head)
  144.         {
  145.             m_head->prev->next = node->next;
  146.             m_head->next->prev = m_head->prev;
  147.             m_head = node->next;
  148.         }
  149.         else if (node == m_head->prev)
  150.         {
  151.             Node* old_tail = m_head->prev;
  152.             m_head->prev = old_tail->prev;
  153.             old_tail->prev->next = m_head;
  154.         }
  155.         else
  156.         {
  157.             node->prev->next = node->next;
  158.             node->next->prev = node->prev;
  159.         }
  160.  
  161.         delete node;
  162.         return true;
  163.     }
  164.  
  165.     void erase_all()
  166.     {
  167.         while (m_head)
  168.         {
  169.             erase(m_head->i);
  170.         }
  171.     }
  172.  
  173.     int element_count()
  174.     {
  175.         if (m_head == NULL)
  176.         {
  177.             return 0;
  178.         }
  179.         else
  180.         {
  181.             int count = 0;
  182.             Node* node = m_head;
  183.  
  184.             do {
  185.                 count++;
  186.                 node = node->next;
  187.             } while (node != m_head);
  188.  
  189.             return count*2;
  190.         }
  191.     }
  192.  
  193.     int element_count2()
  194.     {
  195.         if (m_head == NULL)
  196.         {
  197.             return 0;
  198.         }
  199.         else
  200.         {
  201.             int count = 0;
  202.             Node* node = m_head;
  203.  
  204.             do {
  205.                 count++;
  206.                 node = node->next;
  207.             } while (node != m_head);
  208.  
  209.             return count;
  210.         }
  211.     }
  212.  
  213.     void show_from_start(int count)
  214.     {
  215.         if (count > element_count())
  216.         {
  217.             std::cout << "Brak wezlow w liscie!\n";
  218.             return;
  219.         }
  220.  
  221.         Node* actual = m_head;
  222.         int i = 1;
  223.         while (count > 0)
  224.         {
  225.             std::cout << " " << actual->i << " " << i;
  226.             actual = actual->next;
  227.             i++;
  228.             count--;
  229.            
  230.         }
  231.         std::cout << '\n';
  232.     }
  233.  
  234.     void funkcja2(int count)
  235.     {
  236.         if (count > element_count2())
  237.         {
  238.             std::cout << "Brak wezlow w liscie!\n";
  239.             return;
  240.         }
  241.  
  242.         Node* actual = m_head;
  243.         while (count > 0)
  244.         {
  245.             std::cout << " " << actual->i;
  246.             actual = actual->next;
  247.             count--;
  248.  
  249.         }
  250.         std::cout << '\n';
  251.     }
  252.    
  253. private:
  254.     Node * m_head;
  255. };
  256.  
  257. int main()
  258. {
  259.     char c;
  260.     cout << "Czy rozklad kluczy ma byc losowy? [t/n] "; cin >> c;
  261.     if (c == 't' || c == 'T')
  262.     {
  263.         int N;
  264.         cout << "Podaj ilosc kluczy do wylosowania: " << endl;
  265.         cin >> N;
  266.  
  267.         clock_t begin, end;
  268.         double time_spent;
  269.  
  270.         begin = clock();
  271.  
  272.         List list;
  273.         list.add_nodes(N);
  274.         printf("%d elementow\n", list.element_count2());
  275.     //  list.funkcja2(N);
  276.  
  277.         end = clock();
  278.  
  279.         time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  280.         printf("Minelo %f sekund\n", time_spent);
  281.  
  282.     }
  283.     else
  284.     {
  285.         int N;
  286.         cout << "Podaj ilosc kluczy do wylosowania: " << endl;
  287.         cin >> N;
  288.  
  289.         clock_t begin, end;
  290.         double time_spent;
  291.  
  292.         begin = clock();
  293.  
  294.         List list;
  295.         list.add_nodes(N);
  296.         printf("%d elementow\n", list.element_count());
  297.     //  list.show_from_start(N);
  298.  
  299.         end = clock();
  300.  
  301.         time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  302.         printf("Minelo %f sekund\n", time_spent);
  303.     }
  304.  
  305.     system("pause");
  306.     return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement