Advertisement
Hamoudi30

Untitled

Nov 6th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node {
  4.     int data;
  5.     struct Node* next;
  6. };
  7. int cnt = 0;
  8. struct Node *reverse (struct Node *head, int k, int SZ) {
  9.   if (!head)
  10.     return NULL;
  11.  
  12.   struct Node *current = head;
  13.   struct Node *next = NULL;
  14.   struct Node *prev = NULL;
  15.   int count = 0;
  16.   if (abs(cnt - SZ) < k)
  17.     return head;
  18.   while (current != NULL && count < k) {
  19.     cnt++;
  20.     next = current->next;
  21.     current->next = prev;
  22.     prev = current;
  23.     current = next;
  24.     count++;
  25.   }
  26.   if (next != NULL)
  27.     head->next = reverse(next, k, SZ);
  28.   return prev;
  29. }
  30.  
  31. void push(struct Node** head_ref, int new_data) {
  32.   struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
  33.  
  34.   new_node->data = new_data;
  35.  
  36.   new_node->next = (*head_ref);
  37.  
  38.   (*head_ref) = new_node;
  39. }
  40.  
  41. const int MAX_SZ = 10009;
  42. int arr[MAX_SZ];
  43.  
  44. int main(void) {
  45.   struct Node *head = NULL;
  46.  
  47.   int nodes, sz;
  48.   scanf("%d%d", &nodes, &sz);
  49.   int x;
  50.   for (int i = 0; i < nodes; ++i) {
  51.     scanf("%d", &x);
  52.     arr[i] = x;
  53.   }
  54.   for (int i = nodes - 1; i >= 0; --i) {
  55.     push(&head, arr[i]);
  56.   }
  57.  
  58.   head = reverse(head, sz, nodes);
  59.  
  60.   while (head != NULL) {
  61.     printf("%d\n", head->data);
  62.     head = head->next;
  63.   }
  64.   return (0);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement