Advertisement
193030

Reverse linked list from k, n

Nov 14th, 2021
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     struct Node* next;
  9. };
  10.  
  11. void read_list(struct Node** head)
  12. {
  13.     struct Node* p;
  14.     int x;
  15.     while (1 == scanf_s("%d", &x))
  16.     {
  17.         p = (struct Node*)malloc(sizeof(struct Node));
  18.         p->data = x;
  19.         p->next = *head;
  20.         *head = p;
  21.         p = NULL;
  22.     }
  23. }
  24.  
  25. void print_list(struct Node** head)
  26. {
  27.     struct Node* p = *head;
  28.     while (p)
  29.     {
  30.         printf("%d ", p->data);
  31.         p = p->next;
  32.     }
  33.     printf("\n");
  34. }
  35.  
  36. void reverse_list_in_range(struct Node** head, int k, int n)
  37. {
  38.     struct Node* lastHead;
  39.     lastHead = *head;
  40.     while (--k)
  41.         lastHead = lastHead->next;
  42.     struct Node* prev = NULL;
  43.     struct Node* current = lastHead->next;
  44.     struct Node* lastTemp = lastHead->next;
  45.     struct Node* next = NULL;
  46.     while (current != NULL && --n != -1) {
  47.         next = current->next;
  48.         current->next = prev;
  49.         prev = current;
  50.         current = next;
  51.     }
  52.     lastHead->next = prev;
  53.     struct Node* p = *head;
  54.     while (p->next)
  55.         p = p->next;
  56.     p->next = next;
  57.  
  58. }
  59.  
  60.  
  61. void print_random(struct Node** head)
  62. {
  63.     struct Node* p = *head;
  64.     struct Node* ls = *head;
  65.     int listSize = 0;
  66.     // преброяване на възлите
  67.     while (ls)
  68.     {
  69.         ls = ls->next;
  70.         listSize++;
  71.     }
  72.     // генериране на горна и долна граница за произволен интервал в списъка
  73.     int lower = 0;
  74.     int upper = listSize - 1;
  75.     int numStart = (rand() % (upper - lower + 1)) + lower;
  76.     lower = numStart + 1;
  77.     upper = listSize;
  78.     int numEnd = (rand() % (upper - lower + 1)) + lower;
  79.     int iterationsToPrint = numEnd - numStart;
  80.  
  81.     // принтиране на произволния интервал
  82.     while (numStart--)
  83.         p = p->next;
  84.     while (iterationsToPrint--)
  85.     {
  86.         printf("%d ", p->data);
  87.         p = p->next;
  88.     }
  89.  
  90. }
  91.  
  92.  
  93. int main()
  94. {
  95.     // Примерен вход: 8 9 10 20 30 40 50 60 70 a
  96.     struct Node* head = NULL;
  97.     read_list(&head); // четене на списъка от конзола
  98.     print_list(&head); // принтиране на списъка
  99.     int k = 0;
  100.     int n = 4;
  101.     reverse_list_in_range(&head, k, n); // Обръщане на n елемента от k елемент
  102.     print_list(&head); // принтиране на обърнатия списък
  103.     srand(time(NULL)); // 'захранване' на rand()
  104.     print_random(&head); // принтиране на произволно избран участък на вече обърнатия списък
  105.  
  106. }
  107.  
  108. // working 11;25
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement