The_Law

Untitled

Dec 1st, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct list
  5. {
  6.     int val;
  7.     struct list *next;
  8. };
  9.  
  10. void tofront(struct list **ppl)
  11. {
  12.     struct list *tmp = *ppl;
  13.     int n = 0;
  14.  
  15.     while(tmp != NULL)
  16.     {
  17.         ++n;
  18.         tmp = tmp->next;
  19.     }
  20.  
  21.     struct list *tmp1 = *ppl;
  22.     int it = 0;
  23.  
  24.     while (it < n - (n / 3) - 1)
  25.     {
  26.         tmp1 = tmp1->next;
  27.         ++it;
  28.     }
  29.  
  30.     struct list *tmp2 = tmp1->next;
  31.     tmp1->next = NULL;
  32.     tmp->next = *ppl;
  33.     *ppl = tmp1;
  34. }
  35.  
  36. int main(void)
  37. {
  38.     struct list *head = NULL;
  39.     struct list *tmp = (struct list*)malloc(sizeof(struct list));
  40.     tmp->val = 1;
  41.     tmp->next = head;
  42.     head = tmp;
  43.  
  44.     struct list *p = head;
  45.  
  46.     int res = tofront(&head);
  47.     printf("%d", res);
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment