-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_operations.c
More file actions
109 lines (100 loc) · 2.87 KB
/
stack_operations.c
File metadata and controls
109 lines (100 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: scosta-j <scosta-j@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/19 14:28:09 by scosta-j #+# #+# */
/* Updated: 2023/08/22 18:08:25 by scosta-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "stack.h"
/**
* @brief takes a element from stack1 (pop) and puts in the stack2 (push)
*
* @param stack_1 stack which the element is going to be removed (pop)
* @param stack_2 stack which the element is going to be pushed (push)
* @returns 1 if sucessfull and 0 if not succesful
* its not sucessful if the 1 stack is empty
*/
int push(t_list **stack_1, t_list **stack_2)
{
t_list *head_1;
if (!*stack_1)
return (0);
head_1 = *stack_1;
*stack_1 = (*stack_1)->next;
head_1->next = *stack_2;
*stack_2 = head_1;
return (1);
}
/**
* @brief swaps the first two elements of the stack
*
* @param stack the stack that we are going to swap the first two elements
* @return int 1 if sucessful 0 if stack empty or has one element
*/
int swap(t_list **stack)
{
t_list *first;
t_list *second;
t_list *third;
if (!*stack || !(*stack)->next)
return (0);
first = *stack;
second = (*stack)->next;
third = second->next;
first->next = third;
second->next = first;
*stack = second;
return (1);
}
/**
* @brief rotates the stack the first element becomes the last one
*
* @param stack stack in which the operation is going to be performed
* @return int 1 if sucessfull 0 if empty or has one element
*/
int rotate(t_list **stack)
{
t_list *first;
t_list *second;
t_list *last;
if (!*stack || !(*stack)->next)
return (0);
first = *stack;
second = first->next;
last = *stack;
while (last->next)
last = last->next;
first->next = NULL;
last->next = first;
*stack = second;
return (1);
}
/**
* @brief reverse rotate the last element of the stack becomes the first one
*
* @param stack the stack in which the operation is going to be performed
* @return int 1 if is sucessfull 0 if its not
*/
int rev_rotate(t_list **stack)
{
t_list *first;
t_list *last;
t_list *new_last;
if (!*stack || !(*stack)->next)
return (0);
first = *stack;
last = *stack;
while (last->next)
{
new_last = last;
last = last->next;
}
new_last->next = NULL;
last->next = first;
*stack = last;
return (1);
}