Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. typedef struct s_list
  2. {
  3. struct s_list *next;
  4. void *data;
  5. }
  6. /* ************************************************************************** */
  7. /* */
  8. /* ::: :::::::: */
  9. /* main.c :+: :+: :+: */
  10. /* +:+ +:+ +:+ */
  11. /* By: iprokofy <iprokofy@42.fr> +#+ +:+ +#+ */
  12. /* +#+#+#+#+#+ +#+ */
  13. /* Created: 2017/08/22 10:56:34 by iprokofy #+# #+# */
  14. /* Updated: 2017/08/22 10:56:36 by iprokofy ### ########.fr */
  15. /* */
  16. /* ************************************************************************** */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. void ft_list_reverse_fun(t_list *begin_list)
  22. {
  23. t_list *current;
  24. t_list *prev;
  25. t_list *next;
  26.  
  27. if (begin_list == NULL)
  28. return ;
  29. current = begin_list;
  30. prev = NULL;
  31. while (current)
  32. {
  33. next = current->next;
  34. current->next = prev;
  35. prev = current;
  36. current = next;
  37. }
  38. begin_list = prev;
  39. }
  40.  
  41. t_list *ft_create_elem(void *data)
  42. {
  43. t_list *list;
  44.  
  45. list = (t_list*)malloc(sizeof(t_list));
  46. list->data = data;
  47. list->next = NULL;
  48. return (list);
  49. }
  50.  
  51. void ft_list_push_back(t_list **begin_list, void *data)
  52. {
  53. t_list *current;
  54.  
  55. if (*begin_list == NULL)
  56. {
  57. *begin_list = ft_create_elem(data);
  58. return ;
  59. }
  60. current = *begin_list;
  61. while (current->next)
  62. current = current->next;
  63. current->next = ft_create_elem(data);
  64. }
  65.  
  66. void print_list(t_list *list)
  67. {
  68. if (list == NULL)
  69. {
  70. printf("NULL LIST\n");
  71. return ;
  72. }
  73. while (list)
  74. {
  75. int *a = list->data;
  76. printf("%d - ", *a);
  77. list = list->next;
  78. }
  79. printf("NULL\n");
  80. }
  81.  
  82. int main()
  83. {
  84. t_list *list;
  85. int a = 9;
  86. int b = 10;
  87. int c = 90;
  88. int d = 9111;
  89.  
  90. list = NULL;
  91. ft_list_push_back(&list, (void*)&a);
  92. ft_list_push_back(&list, (void*)&b);
  93. ft_list_push_back(&list, (void*)&c);
  94. ft_list_push_back(&list, (void*)&d);
  95. print_list(list);
  96. ft_list_reverse_fun(list);
  97. print_list(list);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement