Advertisement
Guest User

Untitled

a guest
May 28th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tnode {
  5.     char value;
  6.     struct tnode *next;
  7. }node;
  8.  
  9. node *dodaj_na_poczatek(node *head, char val) {
  10.     node *tmp = malloc(sizeof(node));
  11.     if (!tmp)
  12.         exit(-1);
  13.     tmp->value = val;
  14.     tmp->next = head;
  15.     return tmp;
  16. };
  17.  
  18. void wypisz_liste_na_ekran(const node *elem) {
  19.     if (!elem) {
  20.         printf("Lista jest pusta.\n");
  21.         return;
  22.     }
  23.     while (elem) {
  24.         printf("%6c", elem->value);
  25.         elem = elem->next;
  26.     }
  27. }
  28.  
  29. node *zwolnij_pamiec_zajmowana_przez_liste(node *elem) {
  30.     while (elem) {
  31.         node *next = elem->next;
  32.         free(elem);
  33.         elem = next;
  34.     }
  35.     return NULL;
  36. }
  37.  
  38. node *odwroc_liste(node *head) {
  39.     node *cur = head;
  40.     node *prev = NULL;
  41.     node *next = NULL;
  42.     while (cur) {
  43.         next = cur->next;
  44.         cur->next = prev;
  45.         prev = cur;
  46.         cur = next;
  47.     }
  48.     return prev;
  49. }
  50.  
  51. int main(void) {
  52.     node *head = NULL;
  53.     head = dodaj_na_poczatek(head, 'i');
  54.     wypisz_liste_na_ekran(head); printf("\n");
  55.     head = dodaj_na_poczatek(head, 'u');
  56.     wypisz_liste_na_ekran(head); printf("\n");
  57.     head = dodaj_na_poczatek(head, 'd');
  58.     wypisz_liste_na_ekran(head); printf("\n");
  59.     head = dodaj_na_poczatek(head, 'f');
  60.     wypisz_liste_na_ekran(head); printf("\n");
  61.  
  62.     head = odwroc_liste(head);
  63.     printf("Po odwroceniu:\n");
  64.     wypisz_liste_na_ekran(head);
  65.  
  66.     head = zwolnij_pamiec_zajmowana_przez_liste(head);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement