Advertisement
YEZAELP

SMMR-238: Surprise Gift

Jun 5th, 2021
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <bits/stdc++.h> // include everything
  2. using namespace std;
  3.  
  4. /*
  5. STAFF'S FUNCTIONS
  6. */
  7.  
  8. struct Node {
  9.     int val;
  10.     Node *next;
  11. };
  12.  
  13. int get_list_size(Node* head) {
  14.     return head == NULL ? 0 : 1 + get_list_size(head->next);
  15. }
  16.  
  17. void print_list(Node* head) {
  18.     if (head == NULL)
  19.         return;
  20.     printf("%d ", head->val);
  21.     print_list(head->next);
  22. }
  23.  
  24. Node* insert_front(Node* head, int val) {
  25.     Node* new_node = new Node; // same as malloc(sizeof(Node))
  26.     new_node->val = val;
  27.     new_node->next = head;
  28.     return new_node;
  29. }
  30.  
  31. Node* free_list(Node* head) {
  32.     if (head != NULL) {
  33.         free_list(head->next);
  34.         delete head; // same as free(head)
  35.     }
  36.     return NULL;
  37. }
  38.  
  39. /*
  40. STUDENT'S FUNCTIONS
  41. */
  42.  
  43. Node* get_tail(Node* head) {
  44.     // your code here
  45.     if(head -> next == nullptr) return head;
  46.     head = get_tail(head -> next);
  47.     return head;
  48. }
  49.  
  50. Node* split_at_k(Node* head, int k) {
  51.     // your code here
  52.     if(k == 1){
  53.         Node *cur = head -> next;
  54.         head -> next = nullptr;
  55.         return cur;
  56.     }
  57.     head = split_at_k(head -> next, k-1);
  58.     return head;
  59. }
  60.  
  61. Node* flip_chunks_of_two(Node* head) {
  62.     // your code here
  63.     if(head == nullptr) return nullptr;
  64.     if(head -> next == nullptr) return head;
  65.     Node *a, *b, *c;
  66.     a = head;
  67.     b = head -> next;
  68.     c = head -> next -> next;
  69.     b -> next = a;
  70.     a -> next = flip_chunks_of_two(c);
  71.     return b;
  72. }
  73.  
  74. /*
  75. STAFF'S MAIN
  76. */
  77.  
  78. void print_list_addresses(Node* head) {
  79.     if (head == NULL)
  80.         return;
  81.     printf("%p ", head);
  82.     print_list_addresses(head->next);
  83. }
  84.  
  85. void print_list(Node* head, const char* name) {
  86.     printf("%s\n", name);
  87.     print_list(head);
  88.     printf("\n");
  89.     print_list_addresses(head);
  90.     printf("\n");
  91. }
  92.  
  93. int main()
  94. {
  95.     // build input list
  96.     int n, q;
  97.     scanf("%d%d", &n, &q);
  98.     int input[n];
  99.     for (int i = 0; i < n; ++i)
  100.         scanf("%d", &input[i]);
  101.     Node* head = NULL;
  102.     for (int i = n-1; i >= 0; --i)
  103.         head = insert_front(head, input[i]);
  104.     print_list(head, "Before: Input");
  105.  
  106.     if (q == 1) {
  107.         Node* tail = get_tail(head);
  108.         print_list(tail, "After: Tail node");
  109.     } else if (q == 2) {
  110.         int k; scanf("%d", &k);
  111.         Node* sec2 = split_at_k(head, k);
  112.         print_list(head, "After: Front list");
  113.         print_list(sec2, "After: Back list");
  114.     } else if (q == 3) {
  115.         Node* newhead = flip_chunks_of_two(head);
  116.         print_list(newhead, "After: Flipped pairs");
  117.     }
  118.  
  119.     return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement