Advertisement
zodiak1

Untitled

Feb 15th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <locale.h>
  4. #include <windows.h>
  5. #include <time.h>
  6.  
  7. struct list {
  8.    list(char _n = 0, short _a = 0, list *_next = NULL) : n(_n), a(_a), next(_next) { }
  9.  
  10.    char n;
  11.    short a;
  12.    list *next;
  13. };
  14.  
  15. char random();
  16. void sumLists(list* r, list *a, list *b);
  17. list* fillList(char index, list* next = NULL);
  18.  
  19. const char N = 10;
  20.  
  21. int main()
  22. {
  23.    time_t t;
  24.    srand((UINT) time(&t));
  25.  
  26.    list *P = fillList(N), *Q = fillList(N), *R = new list();
  27.    sumLists(R, P, Q);
  28.  
  29.    return 0 * _getch();
  30. }
  31.  
  32. char random()
  33. {
  34.    char r = rand() % (CHAR_MAX + 1 - CHAR_MIN) + CHAR_MIN;
  35.    return r == 0 ? random() : r;
  36. };
  37.  
  38. list *fillList(char index, list *next)
  39. {
  40.    return index == 0 ? next : fillList(index - 1, new list(N - index, random(), next));
  41. }
  42. void sumLists(list *r, list *a, list *b)
  43. {
  44.    if (a != NULL)
  45.    {
  46.       *r = *new list(a->n, a->a + b->a, new list());
  47.       sumLists(r->next, a->next, b->next);
  48.    }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement