SHARE
TWEET

Untitled

a guest Jan 14th, 2020 64 in 15 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define new(type) (type *)malloc(sizeof(type))
  6.  
  7. typedef struct node
  8. {
  9.     int value;
  10.     struct node *next, *prev;
  11. } Node;
  12.  
  13. typedef struct list
  14. {
  15.     int size;
  16.     Node *head;
  17.     Node *tail;
  18. } List;
  19.  
  20. List * create()
  21. {
  22.     List *start = new(List);
  23.     start -> size = 0;
  24.     Node *first = new(Node);
  25.     Node *end   = new(Node);
  26.     start -> head = first;
  27.     start -> tail = end;
  28.     first -> prev = NULL; end -> next = NULL;
  29.     first -> next = end; end -> prev = start;
  30.     return start;
  31. }
  32.  
  33. void add(List *list, int value)
  34. {
  35.     Node *new = new(Node);
  36.     new -> value = value;
  37.     new -> next  = list -> tail;
  38.     Node *t = list -> head;
  39.     while (t -> next != list -> tail)
  40.     {
  41.         t = t -> next;
  42.     }
  43.     t -> next = new;
  44.     new -> prev = t;
  45.     list -> size++;
  46. }
  47.  
  48. int randint()
  49. {
  50.     return (rand() % (100));
  51. }
  52.  
  53. List * create_random(int len)
  54. {
  55.     List *new = create();
  56.     for (int i = 0; i < len; i++)
  57.     {
  58.         add(new, randint());
  59.     }
  60.     return new;
  61. }
  62.  
  63. void print_all_list(List *list)
  64. {
  65.     Node *start = list -> head -> next;
  66.     while(start && start -> next)
  67.     {
  68.         printf("%d ", start -> value);
  69.         start = start -> next;
  70.     }
  71.     printf("\n");
  72. }
  73.  
  74. void print_from_to(List *list, int from, int to)
  75. {
  76.     Node *start = list -> head -> next;
  77.     while (from > 0)
  78.     {
  79.         start = start -> next;
  80.         from--;
  81.     }
  82.     Node *end   = list -> head -> next;
  83.     while (to > 0)
  84.     {
  85.         end   = end -> next;
  86.         to--;
  87.     }
  88.     while (start != end)
  89.     {
  90.         printf("%d ", start -> value);
  91.         start = start -> next;
  92.     }
  93.     printf("%d\n", start -> value);
  94. }
  95.  
  96. void min_max_sr(List *a, List *b)
  97. {
  98.     int sr = 0,
  99.         minA = a -> head -> value,
  100.         maxA = a -> head -> value,
  101.         minB = b -> head -> value,
  102.         maxB = b -> head -> value;
  103.  
  104.     Node *tmp = a -> head -> next;
  105.     while (tmp)
  106.     {
  107.        
  108.     }
  109.  
  110.     printf("%d %d %d\n", MIN(minA, minB), MAX(maxA, maxB), sr);
  111. }
  112.  
  113. int main()
  114. {
  115.     srand(time(NULL));
  116.     List *a = create();
  117.     for (int i = 0; i < 10; i++)
  118.     {
  119.         add(a, i);
  120.     }
  121.     print_from_to(a, 4, 7);
  122.     return 0;
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top