Advertisement
Tobiahao

Kolokwium_01

Apr 26th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. /*
  2.  1. Implementacja listy która przechowuje: id,tytul,autor. Implementacja funkcji, która dodaje pole i funkcje która wyświetla wszystkie pola.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define MAX_STRING_SIZE 40
  9.  
  10. struct list_node{
  11.     unsigned int id;
  12.     char tytul[MAX_STRING_SIZE];
  13.     char autor[MAX_STRING_SIZE];
  14.    
  15.     struct list_node *next;
  16. } *list_pointer;
  17.  
  18. struct list_node *create_list(unsigned int id, char *tytul, char *autor)
  19. {
  20.     struct list_node *first_node = (struct list_node*)malloc(sizeof(struct list_node));
  21.     if(first_node){
  22.         first_node->id = id;
  23.         strncpy(first_node->tytul, tytul, MAX_STRING_SIZE);
  24.         strncpy(first_node->autor, autor, MAX_STRING_SIZE);
  25.         first_node->next = NULL;
  26.     }
  27.     return first_node;
  28. }
  29.  
  30. struct list_node *add_first(struct list_node *list_pointer, struct list_node *new_node)
  31. {
  32.     new_node->next = list_pointer;
  33.     return new_node;
  34. }
  35.  
  36. struct list_node *find_spot(struct list_node *list_pointer, int id)
  37. {
  38.     struct list_node *previous = NULL;
  39.     while(list_pointer && list_pointer->id < id){
  40.         previous = list_pointer;
  41.         list_pointer = list_pointer->next;
  42.     }
  43.     return previous;
  44. }
  45.  
  46. void add_in_middle_or_at_back(struct list_node *previous, struct list_node *new_node)
  47. {
  48.     new_node->next = previous->next;
  49.     previous->next = new_node;
  50. }
  51.  
  52. struct list_node *add_node(struct list_node *list_pointer, unsigned int id, char *tytul, char *autor)
  53. {
  54.     struct list_node *new_node = (struct list_node*)malloc(sizeof(struct list_node));
  55.     if(new_node){
  56.         new_node->id = id;
  57.         strncpy(new_node->tytul, tytul, MAX_STRING_SIZE);
  58.         strncpy(new_node->autor, autor, MAX_STRING_SIZE);
  59.         new_node->next = NULL;
  60.             if(list_pointer->id >= id)
  61.                 return add_first(list_pointer, new_node);
  62.             else{
  63.                 struct list_node *previous = find_spot(list_pointer, id);
  64.                 add_in_middle_or_at_back(previous, new_node);
  65.             }
  66.     }
  67.     return list_pointer;
  68. }
  69.  
  70. void print_list(struct list_node *list_pointer)
  71. {
  72.     while(list_pointer){
  73.         printf("Id: %d\nTytul: %s\nAutor: %s\n", list_pointer->id, list_pointer->tytul, list_pointer->autor);
  74.         list_pointer = list_pointer->next;
  75.     }
  76.     printf("\n");
  77. }
  78.  
  79. int main(void)
  80. {
  81.     list_pointer = create_list(1, "Ksiazka 1", "Autor 1");
  82.    
  83.     list_pointer = add_node(list_pointer, 2, "Ksiazka 2", "Autor 2");
  84.     list_pointer = add_node(list_pointer, 4, "Ksiazka 4", "Autor 4");
  85.     list_pointer = add_node(list_pointer, 3, "Ksiazka 3", "Autor 3");
  86.     print_list(list_pointer);
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement