Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5.   char *slowo;
  6.   struct node * next;
  7. } node_t;
  8.  
  9. void drukuj(node_t * head) {
  10.   if(head != 0) {
  11.     node_t * current = head;
  12.     while(current != NULL) {
  13.       printf("%s\n", current->slowo);
  14.       current = current->next;
  15.     }
  16.   }
  17.   else
  18.     printf("pusta lista\n");
  19. }
  20.  
  21. void wstaw(node_t ** head, char *wyraz) {
  22.   node_t * new_node = malloc(sizeof(node_t));
  23.   new_node->slowo = wyraz;
  24.   new_node->next = *head;
  25.   *head = new_node;
  26. }
  27.  
  28. long int szukaj(node_t * head, char *wyraz) {
  29.   node_t * current = head;
  30.   while(current != NULL) {
  31.     if(current->slowo == wyraz)
  32.       return current;
  33.     current = current->next;
  34.   }
  35.   return NULL;
  36. }
  37.  
  38. void szukaj2(node_t * head) {
  39.   node_t * new_node = head;
  40.   if(new_node == NULL) {
  41.     printf("nie ma takiego elementu\n");
  42.   }
  43.   else
  44.     printf("%s\n", new_node->slowo);
  45. }
  46.  
  47. long int prev(node_t * head) {
  48.   if(head + 2 == NULL)
  49.     return NULL;
  50.   else
  51.     return head + 2;
  52. }
  53.  
  54. void usun(node_t ** head, char *wyraz) {
  55.   long int x = szukaj(*head, wyraz);
  56.   //node_t * usuwany = x;
  57.   //usuwany->next = usuwany - 2;
  58.   //printf("XXXXXXXXXX %d %d\n", usuwany->next, x - 32);
  59.   // printf("TET, %s\n", usuwany->next->slowo);
  60.   if(x != NULL) {
  61.     long int poprzedni = prev(x);
  62.     // printf("TET %ld, %ld\n", poprzedni, usuwany);
  63.     if(x != *head) {
  64.       node_t * prev = poprzedni;
  65.       if(prev->next->next == NULL)
  66.         prev->next = 0;
  67.       // printf("ASDASD %s, %s\n", prev->slowo, usuwany->slowo);
  68.       else
  69.         prev->next = x - 32;
  70.       // printf("TET %d\n", prev->next);
  71.     }
  72.     else {
  73.       *head -=2;
  74.     }
  75.   }
  76. }
  77.  
  78. void kasuj(node_t ** head) {
  79.   while(*head != NULL) {
  80.     free(*head);
  81.     *head = (*head)->next;
  82.   }
  83.   //free(*head);
  84. }
  85.  
  86. long int bezPowtorzen(node_t * head) {
  87.   node_t * new_node = malloc(sizeof(node_t));
  88.   node_t * current = head;
  89.   new_node->slowo = current->slowo;
  90.   while(current != NULL) {
  91.     char *czyWyraz = current->slowo;
  92.     if(szukaj(new_node, czyWyraz) == 0) {
  93.       wstaw(&new_node, czyWyraz);
  94.     }
  95.     current = current->next;
  96.   }
  97.   return new_node;
  98. }
  99.  
  100. long int scal(node_t ** l1, node_t ** l2) {
  101.   node_t * new_node = malloc(sizeof(node_t));
  102.   node_t * current = *l1;
  103.   node_t * current2 = *l2;
  104.   new_node->slowo = current->slowo;
  105.   current = current->next;
  106.   while(current != NULL) {
  107.     wstaw(&new_node, current->slowo);
  108.     current = current->next;
  109.   }
  110.   while(current2 != NULL) {
  111.     wstaw(&new_node, current2->slowo);
  112.     current2 = current2->next;
  113.   }
  114.   new_node = bezPowtorzen(new_node);
  115.   kasuj(l1);
  116.   kasuj(l2);
  117.   return new_node;
  118.  
  119. }
  120.  
  121.  
  122. int main() {
  123.   node_t * lista = malloc(sizeof(node_t));
  124.  
  125.   lista->slowo = "knur";
  126.   wstaw(&lista, "dzik");
  127.   wstaw(&lista, "dzik");
  128.   wstaw(&lista, "lis");
  129.   wstaw(&lista, "kuna");
  130.   wstaw(&lista, "sowa");
  131.   wstaw(&lista, "lis");
  132.   wstaw(&lista, "wiewiorka");
  133.   wstaw(&lista, "dzieciol");
  134.  
  135.   drukuj(lista);
  136.   printf("\n");
  137.  
  138.   szukaj2(szukaj(lista, "dzik"));
  139.   szukaj2(szukaj(lista, "wiewiorka"));
  140.   szukaj2(szukaj(lista, "asdasdasd"));
  141.  
  142.   usun(&lista, "knur");
  143.   printf("\n");
  144.  
  145.   drukuj(lista);
  146.  
  147.   // kasuj(&lista);
  148.   // drukuj(lista);
  149.  
  150.   printf("\n");
  151.  
  152.   node_t * lista2 = bezPowtorzen(lista);
  153.   printf("\nLISTA 2:\n");
  154.   drukuj(lista2);
  155.   printf("\n");
  156.  
  157.   wstaw(&lista2, "lania");
  158.   wstaw(&lista2, "zbik");
  159.   wstaw(&lista, "zbik");
  160.   wstaw(&lista, "kojot");
  161.  
  162.   node_t * lista3 = scal(&lista, &lista2);
  163.   printf("\nLISTA 3:\n");
  164.   drukuj(lista3);
  165.   printf("\n");
  166.   printf("\nLISTY 1 I 2 PO SCALENIU:\n");
  167.   drukuj(lista);
  168.   drukuj(lista2);
  169.   return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement