Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.07 KB | None | 0 0
  1. //SDIZO I1 213A LAB01
  2. //Mateusz Rynkiewicz
  3. //rm39424@zut.edu.pl
  4. #define _CRTDBG_MAP_ALLOC
  5. #include "stdafx.h"
  6. #include <string>
  7. #include <iostream>
  8. #include <ctype.h>
  9. #include <time.h>
  10.  
  11.  
  12. struct Lista {
  13.     int a; //kluczowa, wyjatkowa
  14.     double b;
  15.     char c;
  16.     Lista* next;
  17.     Lista* prev;
  18. };
  19.  
  20. //Wstawia nowy element za podany
  21. void AddAfter(Lista* start, Lista* nowa) {
  22.     nowa->prev = start;
  23.     nowa->next = start->next;
  24.     start->next->prev = nowa;
  25.     start->next = nowa;
  26. }
  27. //Wstawia nowy element przed podany
  28. void AddBefore(Lista* start, Lista* nowa) {
  29.     nowa->next = start;
  30.     nowa->prev = start->prev;
  31.     start->prev->next = nowa;
  32.     start->prev = nowa;
  33. }
  34. //wstawienie elementu do listy
  35. Lista* WstawElement(Lista* start, Lista* nowy) {
  36.     //gdy zero elementów
  37.    
  38.     if (start->a == NULL) {
  39.         start->a = nowy->a;
  40.         start -> b = rand();
  41.         start->c = 'T';
  42.         start->next = start;
  43.         start->prev = start;
  44.         return start;
  45.     }
  46.    
  47.    
  48.     //gdy mniejszy element
  49.     if (start->a > nowy->a) {
  50.         AddBefore(start, nowy);
  51.         return  nowy;
  52.     }
  53.     //gdy jeden element
  54.     if (start->a == start->next->a) {
  55.         AddAfter(start, nowy);
  56.         return start;
  57.     }
  58.     //gdy wiekszy lub rowny element
  59.     Lista* obecny = start;
  60.     for (;;) {
  61.         if (obecny->a == nowy->a) {
  62.             std::cout << "Klucz jest juz zajety" << std::endl;
  63.             return start;
  64.         }
  65.  
  66.         if ((obecny->a < nowy->a) && ((obecny->next->a > nowy->a)||(obecny->next->a < obecny->a))) {
  67.             AddAfter(obecny, nowy);
  68.             return start;
  69.         }
  70.         obecny = obecny->next;
  71.  
  72.     }
  73.  
  74. }
  75. //Element z 1. (a podane, b losowe, c='T'
  76. Lista* ElementZad1(Lista* start, int klucz) {
  77.     Lista* nowy = (Lista*)malloc(sizeof(Lista));//nowy element
  78.     nowy->a = klucz;
  79.     nowy->b = rand();
  80.     nowy->c = 'T';
  81.     start = WstawElement(start, nowy);
  82.     return start;
  83. }
  84. //wstawienie x nowych elementow
  85. Lista* WstawX(Lista* start,int x) {
  86.     Lista* nowy = (Lista*)malloc(sizeof(Lista));
  87.     nowy->a= (rand() % 24975)+ (rand() % 24975)+ (rand() % 24971) + 99;
  88.  
  89.     for (int i = 0; i < x; i++) {
  90.         Lista* nowy = (Lista*)malloc(sizeof(Lista));
  91.         nowy->a = (rand() % 24975) + (rand() % 24975) + (rand() % 24971) + 99;
  92.         start = WstawElement(start, nowy);
  93.     }
  94.     return start;
  95. }
  96. //zwraca pozycje klucza, zwraca -1, gdy nie ma klucza
  97. int SzukajKlucz(Lista* start, int klucz) {
  98.     if (start->a == NULL)return -1;
  99.     for (int i = 0;; i++) {
  100.         if (start->a == klucz) {
  101.             return i;
  102.         }
  103.         if (klucz<start->a || start->a>start->next->a)
  104.             return -1;
  105.        
  106.             start = start->next;
  107.        
  108.     }
  109. }
  110. //wypisuje, pozycje klucza na liscie
  111. void WypiszPozycje(Lista* start, int klucz) {
  112.     int a = SzukajKlucz(start, klucz);
  113.     if (a == -1) {
  114.         std::cout<< "Element z kluczem " << klucz << " nie znajduje sie w liscie"<< std::endl;
  115.     }
  116.     else {
  117.         std::cout << "Element z kluczem " << klucz << " znajduje sie na pozycji " << a << std::endl;
  118.     }
  119.  
  120. }
  121. // usuwa element o danym kluczu
  122. Lista* UsunElement(Lista* start, int klucz) {
  123.     int pozycja = SzukajKlucz(start, klucz);
  124.     if (pozycja == -1)return start;
  125.     Lista* obecny = start;
  126.     for (int i = 0; i < pozycja; i++)obecny=obecny->next;
  127.     obecny->next->prev = obecny->prev;
  128.     obecny->prev->next = obecny->next;
  129.     if (klucz == 0) {
  130.         start = start->next;
  131.  
  132.     }
  133.     free(obecny);
  134.     return start;
  135.    
  136.  
  137. }
  138. //wypisuje kluczowe z listy od początku
  139. void WypiszPierwsze(Lista* start,int x) {
  140.     std::cout << "Wypisanie pierwszych: " << std::endl;
  141.     if (start->a == NULL) {
  142.         std::cout << "Lista jest pusta" << std::endl;
  143.         return;
  144.     }
  145.     for (int i = 0; i < x; i++) {
  146.         std::cout << "Kluczowa z elementu " << i << " = " << start->a << std::endl;
  147.         if (start->a > start->next->a) {
  148.             std::cout << "Wszystkie zmienne zostały wypisane " << std::endl;
  149.                 return;
  150.         }
  151.         start = start->next;
  152.     }
  153.     return;
  154. }
  155. //wypisuje kluczowe z listy od konca
  156. void WypiszOstatnie(Lista* start, int x) {
  157.     std::cout << "Wypisanie ostatnich: " << std::endl;
  158.     if (start->a == NULL) {
  159.         std::cout << "Lista jest pusta" << std::endl;
  160.         return;
  161.     }
  162.     start = start->prev;
  163.     for (int i = 0; i < x; i++) {
  164.         std::cout << "Kluczowa z elementu " << i << "od konca = " << start->a << std::endl;
  165.         if (start->a < start->prev->a) {
  166.             std::cout << "Wszystkie zmienne zostały wypisane " << std::endl;
  167.             return;
  168.         }
  169.         start = start->prev;
  170.     }
  171.     return;
  172. }
  173. //wypisuje liczbe elementow w liscie
  174. void LiczbaElementow(Lista* start) {
  175.     int liczbaEl = 1;
  176.     if (start->a == NULL) {
  177.         std::cout << "W liscie nie ma elementow" << std::endl;
  178.         return;
  179.     }
  180.     for (;;) {
  181.         if (start->a > start->next->a) {
  182.             std::cout << "Liczba elementow wynosi :" << liczbaEl << std::endl;
  183.             return;
  184.         }
  185.         liczbaEl++;
  186.         start = start->next;
  187.     }
  188. }
  189. //Usuwa wszystkie elementy
  190. void FreeLista(Lista* start) {
  191.     start->a = NULL;
  192.     start = start->next;
  193.     for (;;) {
  194.         start = start->next;
  195.         free(start->prev);
  196.         if (start->next->a == NULL) {
  197.             free(start);
  198.             return;
  199.         }
  200.        
  201.     }
  202.  
  203. }
  204.  
  205.  
  206. int main()
  207. {
  208. //  srand(time(NULL));
  209.     clock_t begin, end;  double time_spent;  begin = clock();
  210.    
  211.     int* plik = (int*)malloc(sizeof(int) * 6);
  212.     FILE *fp;
  213.     fopen_s(&fp, "inlab02.txt", "r");
  214.     if (fp == NULL)
  215.         return -1;
  216.     fscanf_s(fp, "%d %d %d %d %d %d", &plik[0], &plik[1], &plik[2], &plik[3], &plik[4], &plik[5]);
  217.     fclose(fp);
  218.  
  219.     Lista* start = (Lista*)malloc(sizeof(Lista));//element
  220.     start->a = NULL;
  221.  
  222.     WypiszPozycje(start, plik[1]);
  223.     start = WstawX(start, plik[0]);
  224.     WypiszPierwsze(start, 20);
  225.     start = ElementZad1(start, plik[2]);
  226.     WypiszPierwsze(start, 20);
  227.     start = ElementZad1(start, plik[3]);
  228.     WypiszPierwsze(start, 20);
  229.     start = ElementZad1(start, plik[4]);
  230.     WypiszPierwsze(start, 20);
  231.     start = ElementZad1(start, plik[5]);
  232.     start = UsunElement(start, plik[3]);
  233.     WypiszPierwsze(start, 20);
  234.     start = UsunElement(start, plik[2]);
  235.     WypiszPierwsze(start, 20);
  236.     start = UsunElement(start, plik[5]);
  237.     LiczbaElementow(start);
  238.     WypiszPozycje(start, plik[5]);
  239.     WypiszOstatnie(start, 11);
  240.     FreeLista(start);
  241.     WypiszOstatnie(start, 11);
  242.     LiczbaElementow(start);
  243.  
  244.     free(start);
  245.  
  246.     end = clock();  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  247.     std::cout << "Czas: " << time_spent << " sekund" << std::endl;
  248.     system("pause");
  249.     _CrtDumpMemoryLeaks();
  250.     return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement