Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. class List;
  9.  
  10. class ListNode
  11. {
  12.     int x;
  13.     double y;
  14.     char z;
  15.  
  16.     ListNode* next;
  17.  
  18. public:
  19.     ListNode(int); // <---- konstruktor!!!!!
  20.  
  21.     void display();
  22.  
  23.     friend class List; // <----- klasa List ma dostęp do wszystkich składników prywatnych klasy ListNode
  24. };
  25.  
  26. class List
  27. {
  28.     int size;
  29.     ListNode* head; // pierwszy element listy
  30.  
  31. public:
  32.     List();
  33.     ~List(); // <----- destruktor
  34.  
  35.     void addNode(int);
  36.     void fillGoodInCorporation(int);
  37.     ListNode* searchNode(int);
  38.     void deleteNode(int);
  39.     void showFirstNodes(int);
  40.     void showLastNodes(int);
  41.     void showNodes();
  42.     void clear();
  43.  
  44.     int getSize();
  45. };
  46.  
  47. // ciało konstruktora
  48. ListNode::ListNode(int x)
  49. {
  50.     this->x = x;
  51.     this->y = 1 + rand() % 100;
  52.     this->z = 'T';
  53.  
  54.     this->next = NULL;
  55. }
  56.  
  57.  
  58. void ListNode::display() //wyswietlanie konkretnego ListNoda
  59. {
  60.     cout << x << " " << y << " " << z << endl;
  61. }
  62.  
  63. List::List()
  64. {
  65.     this->size = 0;
  66.     this->head = NULL;
  67. }
  68.  
  69. List::~List()
  70. {
  71.     this->clear();
  72. }
  73.  
  74. void List::addNode(int x)
  75. {
  76.     if (head == NULL) {
  77.         this->head = new ListNode(x);
  78.         this->size++;
  79.  
  80.         return;
  81.     }
  82.  
  83.     // Lecimy po liście, dopóki nie natrafimy na ostatni element
  84.     ListNode* temp = head;
  85.     ListNode* next = temp->next;
  86.  
  87.     // Czy dodawany element jest mniejszy od pierwszego
  88.     // W tym wypadku nowy element staje się pierwszym
  89.     if (temp->x > x) {
  90.         ListNode* newNode = new ListNode(x);
  91.  
  92.         newNode->next = temp;
  93.         head = newNode;
  94.  
  95.         this->size++;
  96.         return;
  97.     }
  98.  
  99.     while (next) {
  100.  
  101.         // Jeżeli dodajemy elemnt większy od ogona, to ten warunek nigdy nie zajdzie
  102.         // Jeżeli tak nie jest, to pętla zatrzyma się gdzieś w środku
  103.         if (next->x > x) {
  104.             break;
  105.         }
  106.  
  107.         temp = temp->next;
  108.         next = temp->next;
  109.     }
  110.  
  111.     // aktualizacj listy
  112.     ListNode* newNode = new ListNode(x);
  113.  
  114.     newNode->next = next;
  115.     temp->next = newNode;
  116.  
  117.     this->size++;
  118. }
  119.  
  120. void List::fillGoodInCorporation(int X)
  121. {
  122.     for (int i = 0; i < X; i++) {
  123.         this->addNode(99 + rand() % 99001);
  124.     }
  125. }
  126.  
  127. ListNode* List::searchNode(int x)
  128. {
  129.     ListNode* temp = head;
  130.     while (temp) {
  131.         if (temp->x == x) {
  132.             break;
  133.         }
  134.  
  135.         temp = temp->next;
  136.     }
  137.  
  138.     return temp;
  139. }
  140.  
  141. void List::deleteNode(int x)
  142. {
  143.     if (head == NULL) {
  144.         cout << "List is empty!" << endl;
  145.         return;
  146.     }
  147.  
  148.     ListNode* temp = head;
  149.  
  150.     // jeśli lista ma tylko jeden element, to usuwamy head, setujemy na nulla, zerujemy size i tyle
  151.     if (head->x == x) {
  152.         head = temp->next;
  153.         delete temp;
  154.  
  155.  
  156.         this->size--;
  157.         return;
  158.     }
  159.  
  160.     //potrzeba informacji o poprzednim elemencie by posklejac liste
  161.     ListNode* prev = temp;
  162.     temp = temp->next;
  163.  
  164.     while (temp) {
  165.         if (temp->x == x) {
  166.             break;
  167.         }
  168.  
  169.         prev = temp;
  170.         temp = temp->next;
  171.     }
  172.  
  173.     if (temp == NULL) {
  174.         cout << "Node not found!" << endl;
  175.         return;
  176.     }
  177.  
  178.     prev->next = temp->next;
  179.     delete temp;
  180.  
  181.     this->size--;
  182. }
  183.  
  184. void List::showFirstNodes(int Y) {
  185.     ListNode* temp = head;
  186.     for (int i = 0; i < Y; i++) {
  187.         if (temp == NULL) {
  188.             break;
  189.         }
  190.  
  191.         cout << i + 1 << ": ";
  192.         temp->display();
  193.         temp = temp->next;
  194.     }
  195.  
  196.     cout << endl;
  197. }
  198.  
  199. void List::showLastNodes(int Z) {
  200.     ListNode* temp = head;
  201.  
  202.     if (this->size < Z) {
  203.         this->showNodes();
  204.         return;
  205.     }
  206.  
  207.     // obliczamy liczbę elemntów z przodu
  208.     int firstSize = size - Z;
  209.  
  210.     // przesuwamy się na Z-ty od końca elemnt
  211.     for (int i = 0; i < firstSize; i++) {
  212.         temp = temp->next;
  213.     }
  214.  
  215.     // wyświetlamy Z ostatnich elemntów
  216.     for (int i = 0; i < Z; i++) {
  217.         cout << i + 1 << ": ";
  218.         temp->display();
  219.         temp = temp->next;
  220.     }
  221.  
  222.     cout << endl;
  223. }
  224.  
  225. void List::showNodes()
  226. {
  227.     ListNode* temp = head;
  228.  
  229.     int i = 0;
  230.     while (temp) {
  231.         cout << i++ + 1 << ": ";
  232.         temp->display();
  233.         temp = temp->next;
  234.     }
  235.     cout << endl;
  236. }
  237.  
  238. void List::clear()
  239. {
  240.     ListNode* temp = head;
  241.  
  242.     while (temp) {
  243.         ListNode* next = temp->next;
  244.         delete temp;
  245.         temp = next;
  246.     }
  247.  
  248.     this->head = NULL;
  249.     this->size = 0;
  250. }
  251.  
  252. int List::getSize()
  253. {
  254.     return this->size;
  255. }
  256.  
  257. int main()
  258. {
  259.     srand((unsigned) time(NULL));
  260.  
  261.     // File handling bitch
  262.     FILE* file = fopen("inlab02.txt", "r");
  263.  
  264.     if (!file) {
  265.         cerr << "File not found!!" << endl << endl;
  266.         exit(EXIT_FAILURE);
  267.     }
  268.  
  269.     int X, k1, k2, k3, k4, k5;
  270.     fscanf(file, "%d %d %d %d %d %d", &X, &k1, &k2, &k3, &k4, &k5);
  271.     fclose(file);
  272.  
  273.     clock_t begin, end;
  274.     double time_spent;
  275.     begin = clock();
  276.  
  277.     List* list = new List();
  278.  
  279.     list->searchNode(k1);
  280.     list->fillGoodInCorporation(X);
  281.     cout << list->getSize() << endl << endl;
  282.     list->showFirstNodes(20);
  283.     list->addNode(k2);
  284.     list->showFirstNodes(20);
  285.     list->addNode(k3);
  286.     list->showFirstNodes(20);
  287.     list->addNode(k4);
  288.     list->showFirstNodes(20);
  289.     list->addNode(k5);
  290.     list->deleteNode(k3);
  291.     list->showFirstNodes(20);
  292.     list->deleteNode(k2);
  293.     list->showFirstNodes(20);
  294.     list->deleteNode(k5);
  295.     cout << list->getSize() << endl << endl;
  296.     list->searchNode(k5);
  297.     list->showLastNodes(11);
  298.     cout << list->getSize() << endl << endl;
  299.     list->clear();
  300.  
  301.     end = clock();
  302.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  303.  
  304.     delete list;
  305.  
  306.     cout << "Total time: " << time_spent << endl << endl;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement