Advertisement
Squidy

rozproszone C

Mar 5th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. typedef struct
  8. {
  9.     char text[256];
  10.     struct lista *next;// = NULL;
  11.  
  12. }lista;
  13.  
  14. typedef struct
  15. {
  16.     int hash;
  17.     struct lista *wsk;
  18.  
  19.     struct pierscien *next;
  20.     struct pierscien *before;
  21.  
  22. }pierscien;
  23.  
  24. void show_everything(pierscien* pierwszy)
  25. {
  26.  
  27.     lista *obecny = malloc(sizeof *obecny);
  28.     pierwszy = pierwszy->next;
  29.  
  30.     while (pierwszy->wsk != NULL)
  31.     {
  32.         obecny = pierwszy->wsk;
  33.         printf("Hash listy- %d \n", pierwszy->hash);
  34.         while (obecny != NULL)
  35.         {
  36.             printf("%s\n", obecny->text);
  37.             obecny = obecny->next;
  38.         }
  39.         pierwszy = pierwszy->next;
  40.     }
  41.     printf("\n");
  42. }
  43.  
  44. pierscien usun(pierscien *root, int hash)
  45. {
  46.     root = root->next;
  47.     while (root->wsk != NULL)
  48.     {
  49.         if (root->hash == hash)
  50.             break;
  51.         root = root->next;
  52.     }
  53.     pierscien *usuwany = malloc(sizeof *usuwany);
  54.     usuwany = root;
  55.     root = root->next;
  56.     root->before = usuwany->before;
  57.     root = root->before;
  58.     root->next = usuwany->next;
  59.     free(usuwany);
  60.  
  61.     while (root->wsk != NULL)
  62.         root = root->next;
  63.  
  64.     return *root;
  65. }
  66.  
  67. void show(pierscien* pierwszy, int hash)
  68. {
  69.     pierscien *root = malloc(sizeof *root);
  70.     root = pierwszy;
  71.     pierwszy->next;
  72.     while (pierwszy != NULL)
  73.     {
  74.         //printf("Ten hash to: %d \n", pierwszy->hash);
  75.         if (pierwszy->hash == hash)
  76.             break;
  77.         if (pierwszy == root)
  78.         {
  79.             printf("Nie istnieje \n");
  80.             return;
  81.         }
  82.         pierwszy = pierwszy->next;
  83.        
  84.     }
  85.  
  86.     lista *obecny = malloc(sizeof *obecny);
  87.  
  88.     obecny = pierwszy->wsk;
  89.     while (obecny != NULL)
  90.     {
  91.         printf("%s\n", obecny->text);
  92.         obecny = obecny->next;
  93.     }
  94.     printf("\n");
  95.     //for (int i = 0; my_string[i] != NULL; i++)
  96.     //  printf("%c", my_string[i]);
  97. }
  98.  
  99. int hashuj(char tab[256])
  100. {
  101.     int wynik = 0;
  102.     //printf("%s", tab);
  103.     for (int i = 0; tab[i] != NULL; i++)
  104.         wynik += tab[i];
  105.    
  106.     wynik = wynik % 8;
  107.     printf("%d\n", wynik);
  108.  
  109.     return wynik;
  110. }
  111.  
  112.  
  113. pierscien insert(pierscien *root, char tab[256])
  114. {
  115.     int hash = hashuj(tab);
  116.     int size = 0;
  117.     lista *first = malloc(sizeof *first);
  118.     first->next = NULL;
  119.     bool czy_istnieje = false;
  120.     strcpy(first->text, tab);
  121.     //pierscien *poczatkowy = root;
  122.     pierscien *nastepny = malloc(sizeof *nastepny);
  123.     nastepny->hash = hash;
  124.     nastepny->wsk = first;
  125.  
  126.     if (root->next == root)
  127.     {
  128.         nastepny->next = root;
  129.         nastepny->before = root;
  130.         //tekst tez ustalony
  131.         //hash juz ustalony
  132.  
  133.         root->next = nastepny;
  134.         root->before = nastepny;
  135.         return *root;
  136.     }
  137.  
  138.     root = root->next;
  139.     while (root->hash != NULL) //czy root->next != NULL
  140.     {
  141.         //printf("To jest: %d \n", root->hash);
  142.         if (root->hash == hash)
  143.         {
  144.             czy_istnieje = true;
  145.             break;
  146.         }
  147.         root = root->next;
  148.         size++;
  149.     }
  150.  
  151.     ///*if (root->hash == hash)
  152.     //*/    czy_istnieje = true;
  153.  
  154.    
  155.    
  156.     if (czy_istnieje)// czy wchodze
  157.     {
  158.         //printf("Jestem tu");
  159.         lista *obecny = malloc(sizeof *obecny);
  160.         obecny = root->wsk;
  161.  
  162.         while (obecny->next != NULL)
  163.         {
  164.             if (strcmp(obecny->text, first->text) < 0)
  165.                 obecny = obecny->next;
  166.             else
  167.                 break;
  168.         }
  169.         first->next = obecny->next;
  170.         obecny->next = first;
  171.  
  172.         //nastepny->wsk =
  173.     }
  174.     else
  175.     {
  176.         if (size == 1) //tylko dwa elementy
  177.         {
  178.             //root = root->next;
  179.             if (nastepny->hash > root->hash)
  180.             {
  181.                 nastepny->next = root;
  182.                 root->before = nastepny;
  183.  
  184.                 root = root->next;
  185.  
  186.                 nastepny->before = root;
  187.                 root->next = nastepny;
  188.  
  189.                 root = root->before;
  190.                 return *root;
  191.             }
  192.             else
  193.             {
  194.                 root->next = nastepny;
  195.                 nastepny->before = root;
  196.  
  197.                 root = root->before;
  198.  
  199.                 nastepny->next = root;
  200.                 root->before = nastepny;
  201.  
  202.                 root = root->next;
  203.                 return *root;
  204.             }
  205.         }
  206.         root = root->next;
  207.         while (root->hash != NULL)
  208.         {
  209.             if (nastepny->hash <= root->hash)
  210.             {
  211.                 nastepny->next = root;
  212.                 root = root->before;
  213.                 root->next = nastepny;
  214.                 nastepny->before = root;
  215.                 root = nastepny->next;
  216.                 root->before = nastepny;
  217.                 break;
  218.             }
  219.             root = root->next;
  220.         }
  221.         if (root->hash == NULL)
  222.         {
  223.             nastepny->next = root;
  224.             root = root->before;
  225.             root->next = nastepny;
  226.             nastepny->before = root;
  227.             root = nastepny->next;
  228.             root->before = nastepny;
  229.         }
  230.     }
  231.     while (root->hash != NULL)
  232.         root = root->next;
  233.     return *root;
  234. }
  235.  
  236.  
  237. int main()
  238. {
  239.    
  240.     char rozkaz[30];
  241.     int index;
  242.     printf("Wprowadz tekst:\n");
  243.  
  244.  
  245.     //printf("%s", my_string);
  246.     //lista *poczatek = malloc(sizeof *poczatek);
  247.     //strcpy(poczatek->text, "karol");
  248.     //poczatek->next = NULL;
  249.  
  250.  
  251.     pierscien *root = malloc(sizeof *root);
  252.     root->next = root;
  253.     root->hash = NULL;
  254.     root->before = root;
  255.     root->wsk = NULL;
  256.  
  257.     while (true)
  258.     {
  259.         scanf("%30s", rozkaz);
  260.         if (strcmp(rozkaz, "dodaj") == 0)
  261.         {
  262.             char my_string[256];
  263.             scanf("%256s", my_string);
  264.             *root = insert(root, my_string);
  265.         }
  266.         else if (strcmp(rozkaz, "pokaz") == 0)
  267.         {
  268.             //printf("Podaj wartosc hash: \n");
  269.            
  270.             scanf("%d", &index);
  271.             show(root, index);
  272.         }
  273.         else if (strcmp(rozkaz, "wszystko") == 0)
  274.         {
  275.             show_everything(root);
  276.         }
  277.         else if (strcmp(rozkaz, "usun") == 0)
  278.         {
  279.             scanf("%d", &index);
  280.             *root = usun(root, index);
  281.         }
  282.        
  283.     }
  284.  
  285.  
  286.  
  287.     system("PAUSE");
  288.     return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement