Facebook
From Rude Macaque, 4 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 219
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Car                      // struktura elementu
  6. {
  7.         char number[32];
  8.         char model[64];
  9. };
  10.  
  11. typedef struct Car Car;
  12.  
  13. struct ListElement              //struktura elementu listy (dodanie wskaznika na nastepny element)
  14. {
  15.         Car car;
  16.         struct ListElement* next;
  17. };
  18.  
  19. typedef struct ListElement ListElement;
  20.  
  21. struct List             // struktura listy (wskaznik na element)
  22. {
  23.         ListElement* head;
  24. };
  25.  
  26. typedef struct List List;
  27.  
  28. void pushFront(List* list, const char* number, const char* model)       // dodawanie na poczatek listy
  29. {
  30.         ListElement* element = (ListElement*)malloc(sizeof(ListElement));
  31.  
  32.         strcpy(element->car.number, number);
  33.         strcpy(element->car.model, model);
  34.  
  35.         element->next = list->head;
  36.         list->head = element;
  37. }
  38.  
  39. void pushBack(List* list, const char* number, const char* model)                // dodawanie na koniec listy
  40. {
  41.         ListElement* current = list->head;
  42.         ListElement* element = (ListElement*)malloc(sizeof(ListElement));
  43.         element->next = NULL;
  44.  
  45.         strcpy(element->car.number, number);
  46.         strcpy(element->car.model, model);
  47.  
  48.         if (list->head != 0)            // moze byc (current != 0)
  49.         {
  50.                 while (current->next != 0)
  51.                 {
  52.                         current = current->next;
  53.                 }
  54.                 current->next = element;
  55.         }
  56.         else
  57.                 list->head = element;
  58. }
  59.  
  60. void listOfElements(List* list)         // wylistowanie elementow
  61. {
  62.         ListElement* current = list->head;
  63.  
  64.         while (current != 0)
  65.         {
  66.                 printf("%s %sn", current->car.number, current->car.model);
  67.                 current = current->next;
  68.         }
  69. }
  70.  
  71. void popFront(List* list)               // usuwanie z poczatku listy
  72. {
  73.         ListElement* current = list->head;
  74.  
  75.         if (current != 0)
  76.         {
  77.                 list->head = current->next;
  78.                 free(current);
  79.         }
  80. }
  81.  
  82. void popBack(List* list)                // usuwanie z konca listy
  83. {
  84.         ListElement* current = list->head;
  85.         if (current != 0)
  86.         {
  87.                 if (current->next != 0)
  88.                 {
  89.                         while (current->next->next != 0)
  90.                         {
  91.                                 current = current->next;
  92.                         }
  93.                         free(current->next);
  94.                         current->next = NULL;
  95.                 }
  96.                 else
  97.                 {
  98.                         free(list->head);
  99.                         list->head = NULL;
  100.                 }
  101.         }
  102. }
  103.  
  104. int getNumberOfElements(List* list)             // zwracanie liczby elementow
  105. {
  106.         int number = 0;
  107.         ListElement* current = list->head;
  108.  
  109.         while (current != 0)
  110.         {
  111.                 number++;
  112.                 current = current->next;
  113.         }
  114.        
  115.         return number;
  116. }
  117.  
  118. ListElement* getAtIndex(List* list, int index) // zwracanie indexu(adresu do elementu)
  119. {
  120.         int i = 0;
  121.         ListElement* current = list->head;
  122.  
  123.         while (current != 0)
  124.         {
  125.                 if (i++ == index)
  126.                 {
  127.                         return current;
  128.                 }
  129.                 current = current->next;
  130.         }
  131.  
  132.         return 0;
  133. }
  134.  
  135. void insert(List* list, int index, const char* number, const char* model) // wstawianie przed wybranym elementem
  136. {
  137.         ListElement* current = list->head;
  138.  
  139.         ListElement* neighbor = getAtIndex(list, index); // pobranie elementu przed ktorym ma byc wstawiony nowy
  140.         if (current == neighbor)
  141.         {
  142.                 pushFront(list, number, model);
  143.         }
  144.         else
  145.         {
  146.                 while (current->next != neighbor)
  147.                 {
  148.                         current = current->next;
  149.                 }
  150.  
  151.                 current->next = (ListElement*)malloc(sizeof ListElement);
  152.                 strcpy(current->next->car.number, number);
  153.                 strcpy(current->next->car.model, model);
  154.                 current->next->next = neighbor;
  155.  
  156.         }
  157. }
  158.  
  159. void remove(List* list, int index)  // usuwanie wybranego elementu
  160. {
  161.         ListElement* current = list->head;
  162.         ListElement* neighbor = getAtIndex(list, index);
  163.  
  164.         if (current == neighbor)
  165.         {
  166.                 popFront(list);
  167.         }
  168.         else
  169.         {
  170.                 while (current->next != neighbor)
  171.                 {
  172.                         current = current->next;
  173.                 }
  174.  
  175.                 current->next = neighbor->next;
  176.                 free(neighbor);
  177.         }
  178. }
  179.  
  180. void clear(List* list)          // czyszczenie listy
  181. {
  182.         ListElement* current = list->head;
  183.  
  184.         while (current != 0)
  185.         {
  186.                 printf("Usuwam %s %sn", current->car.number, current->car.model);
  187.                 ListElement* p = current->next;
  188.                 free(current);
  189.                 current = p;
  190.         }
  191. }
  192.  
  193. int main(int argc, char** argv)
  194. {
  195.         List list = { 0 };              // zerowanie head'a (wskaznika listy)
  196.         int number;                             // zmienna dla liczby elementow
  197.  
  198.         pushFront(&list, "ABC 111", "Ford 1000");
  199.         pushFront(&list, "ABC 222", "Ford 1001");
  200.         pushFront(&list, "ABC 333", "Ford 1002");
  201.         pushFront(&list, "ABC 444", "Ford 1003");
  202.         pushFront(&list, "ABC 555", "Ford 1004");
  203.         pushFront(&list, "ABC 666", "Ford 1005");
  204.         pushFront(&list, "ABC 777", "Ford 1006");
  205.         pushFront(&list, "ABC ABC", "Ford 1007");
  206.  
  207.         listOfElements(&list);
  208.  
  209.         number = getNumberOfElements(&list);
  210.         printf("nLiczba elementow listy: %dnn", number);
  211.  
  212.         pushBack(&list, "ABC DEF", "Ford 1008");
  213.  
  214.         listOfElements(&list);
  215.  
  216.         number = getNumberOfElements(&list);
  217.         printf("nLiczba elementow listy: %dnn", number);
  218.  
  219.         insert(&list, 5, "DDD DDD", "Ford 1008");
  220.         insert(&list, 0, "EEE EEE", "Ford 1009");
  221.  
  222.         //popBack(&list);
  223.  
  224.         listOfElements(&list);
  225.  
  226.         number = getNumberOfElements(&list);
  227.         printf("nLiczba elementow listy: %dnn", number);
  228.  
  229.         //popFront(&list);
  230.  
  231.         remove(&list, 5);
  232.         remove(&list, 0);
  233.  
  234.         listOfElements(&list);
  235.  
  236.         number = getNumberOfElements(&list);
  237.         printf("nLiczba elementow listy: %dnn", number);
  238.  
  239.         clear(&list);
  240.  
  241.         return 0;
  242. }