Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   movements.c                                        :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: myener <myener@student.42.fr>              +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2019/07/16 17:37:08 by myener            #+#    #+#             */
  9. /*   Updated: 2019/10/08 16:24:53 by myener           ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include "push_swap.h"
  14.  
  15. int     same_data(t_pslist *curr, int fin)
  16. {
  17.     int i;
  18.  
  19.     i = 0;
  20.     while (curr && i < fin)
  21.     {
  22.         if (!curr->next)
  23.             break ;
  24.         if (curr->data != curr->next->data)
  25.             return (0);
  26.         curr = curr->next;
  27.         i++;
  28.     }
  29.     return (1);
  30. }
  31.  
  32. int     push(t_pslist **src, t_pslist **dest, t_psflag *flag)
  33. {
  34.     t_pslist    *tmp;
  35.     t_pslist    *tmp_dest;
  36.  
  37.     if (!(*src))
  38.         return (0);
  39.     tmp = (*src)->next;
  40.     (*src)->type = ((*src)->type == 'a' ? 'b' : 'a');
  41.     tmp_dest = (*dest);
  42.     (*dest) = (*src);
  43.     (*dest)->next = tmp_dest;
  44.     (*dest)->prev = NULL;
  45.     (*src)->prev = NULL;
  46.     if (tmp_dest)
  47.         tmp_dest->prev = (*src);
  48.     (*src) = tmp;
  49.     if (*src)
  50.         (*src)->prev = NULL;
  51.     if (flag->instruc)
  52.         flag->instruc = ft_free_join(flag->instruc,
  53.         ((*dest)->type == 'b' ? "pb " : "pa "));
  54.     else
  55.         flag->instruc = ft_strdup(((*dest)->type == 'b' ? "pb " : "pa "));
  56.     return (1);
  57. }
  58.  
  59. int     rot(t_pslist **head, int rot, t_psflag *f)
  60. {
  61.     t_pslist    *tmp;
  62.     t_pslist    *tail;
  63.  
  64.     tmp = NULL;
  65.     tail = NULL;
  66.     if ((*head)->next)
  67.         while (rot > 0)
  68.         {
  69.             if (f->instruc)
  70.                 f->instruc = ft_free_join(f->instruc,
  71.                 ((*head)->type == 'a' ? "ra " : "rb "));
  72.             else
  73.                 f->instruc = ft_strdup((*head)->type == 'a' ? "ra " : "rb ");
  74.             tail = *head;
  75.             tmp = *head;
  76.             (*head) = tmp->next;
  77.             while (tail && tail->next)
  78.                 tail = tail->next;
  79.             tail->next = tmp;
  80.             tmp->prev = tail;
  81.             tmp->next = NULL;
  82.             (*head)->prev = NULL;
  83.             rot--;
  84.         }
  85.     return (1);
  86. }
  87.  
  88. int     rrot(t_pslist **head, int rot, t_psflag *f)
  89. {
  90.     t_pslist    *tmp;
  91.     t_pslist    *tail;
  92.  
  93.     tmp = NULL;
  94.     tail = NULL;
  95.     if ((*head)->next)
  96.         while (rot > 0)
  97.         {
  98.             if (f->instruc)
  99.                 f->instruc = ft_free_join(f->instruc,
  100.                 ((*head)->type == 'a' ? "rra " : "rrb "));
  101.             else
  102.                 f->instruc = ft_strdup((*head)->type == 'a' ? "rra " : "rrb ");
  103.             tail = *head;
  104.             while (tail && tail->next)
  105.                 tail = tail->next;
  106.             tmp = tail;
  107.             tail = tail->prev;
  108.             tmp->next = (*head);
  109.             (*head)->prev = tmp;
  110.             (*head) = (*head)->prev;
  111.             tail->next = NULL;
  112.             rot--;
  113.         }
  114.     return (1);
  115. }
  116.  
  117. int     swap(t_pslist *p1, t_pslist *p2, t_psflag *f)
  118. {
  119.     int     tmp_data;
  120.     char    tmp_type;
  121.  
  122.     tmp_data = p1->data;
  123.     p1->data = p2->data;
  124.     p2->data = tmp_data;
  125.     tmp_type = p1->type;
  126.     p1->type = p2->type;
  127.     p2->type = tmp_type;
  128.     if (f->instruc)
  129.         f->instruc = ft_free_join((f->instruc),
  130.         (p1->type == 'a' ? "sa " : "sb "));
  131.     else
  132.         f->instruc = ft_strdup((p1->type == 'a' ? "sa " : "sb "));
  133.     return (1);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement