Advertisement
Tobiahao

Kolokwium_05

Apr 26th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. /*
  2. Napisz funkcję zwracającą wskaźnik na ten element listy, który przechowuje zadaną wartość.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct list_node
  9. {
  10.     int data;
  11.     struct list_node *next;
  12. };
  13.  
  14.  
  15. void fill_list(struct list_node **n)
  16. {
  17.     *n = (struct list_node *) malloc(sizeof(struct list_node));
  18.     (*n)->next = (struct list_node *) malloc(sizeof(struct list_node));
  19.     (*n)->next->next = (struct list_node *) malloc(sizeof(struct list_node));
  20.     (*n)->next->next->next = (struct list_node *) malloc(sizeof(struct list_node));
  21.     (*n)->next->next->next->next = (struct list_node *) malloc(sizeof(struct list_node));
  22.     (*n)->next->next->next->next->next = NULL;
  23.     (*n)->data = 0;
  24.     (*n)->next->data =1;
  25.     (*n)->next->next->data = 2;
  26.     (*n)->next->next->next->data = 3;
  27.     (*n)->next->next->next->next->data = 4;
  28. }
  29.  
  30. struct list_node *znajdz_wskaznik(struct list_node *n, int data)
  31. {
  32.     while(n){
  33.         if(n->data == data){
  34.             printf("Znaleziono!\n");
  35.             return n;
  36.         }
  37.         n = n->next;
  38.     }
  39.     printf("Nie znaleziono!\n");
  40.     return NULL;
  41. };
  42.  
  43. int main(void)
  44. {
  45.     struct list_node *lista = (struct list_node*)malloc(sizeof(struct list_node));
  46.     fill_list(&lista);
  47.     znajdz_wskaznik(lista, 4);
  48.     free(lista);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement