SHARE
TWEET

Untitled

a guest Jan 14th, 2020 68 in 22 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. int get_min(List *a)
  97. {
  98.     Node *first = a -> head -> next;
  99.     int min = first -> value;
  100.     while (first -> next != a -> tail)
  101.     {
  102.         min = (first -> value < min) ? first -> value : min;
  103.         first = first -> next;
  104.     }
  105.     return min;
  106. }
  107.  
  108. int get_max(List *a)
  109. {
  110.     Node *first = a -> head -> next;
  111.     int max = first -> value;
  112.     while (first -> next != a -> tail)
  113.     {
  114.         max = (first -> value > max) ? first -> value : max;
  115.         first = first -> next;
  116.     }
  117.     return max;
  118. }
  119.  
  120. int sr(List *a)
  121. {
  122.    
  123. }
  124.  
  125. int main()
  126. {
  127.     srand(time(NULL));
  128.     List *a = create();
  129.     for (int i = 0; i < 10; i++)
  130.     {
  131.         add(a, i);
  132.     }
  133.     print_from_to(a, 4, 7);
  134.     return 0;
  135. }
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