Advertisement
PaweU

ll

Jan 10th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct xy {
  5. int x;
  6. struct xy* link;
  7. };
  8.  
  9. void add (struct xy* head) {
  10. struct xy* tmp;
  11. tmp = (struct xy *) malloc (sizeof (struct xy));
  12. scanf ("%d", &tmp->x);
  13. tmp->link = NULL;
  14.  
  15. if (head == NULL) {
  16. head->x = tmp->x;
  17. head->link = tmp->link;
  18. }
  19. else {
  20. struct xy* q;
  21. q = head;
  22. while (q->link != NULL) {
  23. q = q->link;
  24. }
  25. q->link = tmp;
  26. }
  27. }
  28.  
  29. void print (struct xy* head) {
  30. struct xy* current = head;
  31. if (current == NULL) printf ("Brak danych w liscie.\n");
  32. while (current != NULL) {
  33. printf("x: %d\n", current->x);
  34. current = current->link;
  35. }
  36. }
  37.  
  38.  
  39. int main(void) {
  40. struct xy* head = (struct xy*) malloc (sizeof (struct xy));
  41.  
  42. add(head);
  43. printf ("Linked list:\n");
  44. //printf ("%d", head->x);
  45. print(head);
  46. printf ("\nWypisane.");
  47.  
  48.  
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement