Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4.  
  5. typedef struct NEXTELEMENT
  6. {
  7.     struct NEXTELEMENT *next;
  8.     char character;
  9.     unsigned int line;
  10. } EDITORLIST;
  11.  
  12. typedef struct LISTHEAD
  13. {
  14.     struct LISTHEAD *next;
  15. } LISTHEAD;
  16.  
  17. void add_element_of_list(EDITORLIST *_my_list, char *_character, unsigned int *_line, unsigned int *_position_in_line)
  18. {
  19.     /*
  20.     * @_my_list             pierwszy element listy
  21.     * @_character           znak przekazywany do listy
  22.     * @_line                numer lini w ktorej ma znajdowac sie znak
  23.     * @_position_in_line    przechowuje wartosc ktory to znak w lini
  24.     * @line_counter         zlicza linie w petli
  25.     * @position_counter     po osiagnieciu odpowiedniej petli zlicza znaki
  26.     * @new_element          tworzy nowy element listy dodawany po @element_finder
  27.     */
  28.     unsigned int line_counter = 1, position_counter = 1;
  29.     EDITORLIST *new_element;
  30.     while (_my_list->next != NULL)
  31.     {
  32.         if (line_counter == _line)
  33.         {
  34.             if (position_counter == _position_in_line)
  35.             {
  36.                 new_element = malloc(sizeof(EDITORLIST));
  37.                 new_element->character = _character;
  38.                 new_element->line = _line;
  39.                 new_element->next = NULL;
  40.                 _my_list->next = new_element;
  41.                 return;
  42.             }
  43.             else
  44.             {
  45.                 position_counter++;
  46.             }
  47.         }
  48.         else if (_my_list->next->character == '\n')
  49.         {
  50.             line_counter++;
  51.         }
  52.         _my_list = _my_list->next;
  53.  
  54.     }
  55. }
  56.  
  57. void delete_element_from_list(EDITORLIST *_my_list, unsigned int *_line, unsigned int *_position_in_line)
  58. {
  59.     /*
  60.     * @_my_list             pierwszy element listy
  61.     * @_line                numer lini w ktorej ma znajdowac sie znak
  62.     * @_position_in_line    przechowuje wartosc ktory to znak w lini
  63.     * @line_counter         zlicza linie w petli
  64.     * @position_counter     po osiagnieciu odpowiedniej petli zlicza znaki
  65.     * @del_element          wskazuje na element poprzedzajacy ten do usuniecia
  66.     */
  67.     unsigned int line_counter = 1, position_counter = 1;
  68.     while (_my_list->next != NULL)
  69.     {
  70.  
  71.         if (line_counter == _line)
  72.         {
  73.             if (position_counter == _position_in_line)
  74.             {
  75.                 EDITORLIST *del_element;
  76.                 del_element = _my_list->next;
  77.                 _my_list->next = del_element->next;
  78.                 free(del_element);
  79.                 return;
  80.             }
  81.             else
  82.             {
  83.                 position_counter++;
  84.             }
  85.         }
  86.         else if (_my_list->next->character == '\n')
  87.         {
  88.             line_counter++;
  89.         }
  90.         _my_list = _my_list->next;
  91.     }
  92. }
  93.  
  94. EDITORLIST *my_list;
  95.  
  96. int main()
  97. {
  98.     unsigned int a = 4;
  99.     unsigned int b = 5;
  100.     char c = 'c';
  101.  
  102.     add_element_of_list(&my_list, &c, &a, &b);
  103.     delete_element_from_list(&my_list, &a, &b);
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement