-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_ops.c
92 lines (83 loc) · 2.22 KB
/
stack_ops.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_ops.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cgleason <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/25 22:12:59 by cgleason #+# #+# */
/* Updated: 2018/07/25 22:31:36 by cgleason ### ########.fr */
/* */
/* ************************************************************************** */
#include "swap_push.h"
t_dblstck *dblstck_new(int num)
{
t_dblstck *new_ds;
new_ds = (t_dblstck *)malloc(sizeof(t_dblstck));
if (!new_ds)
return (NULL);
new_ds->next = new_ds;
new_ds->prev = new_ds;
new_ds->num = num;
return (new_ds);
}
void add(t_dblstck **atop, t_dblstck *to_add)
{
if (*atop == NULL)
*atop = to_add;
to_add->next = *atop;
to_add->prev = (*atop)->prev;
(*atop)->prev->next = to_add;
(*atop)->prev = to_add;
*atop = to_add;
}
void rotate(t_dblstck **atop, t_bool rev)
{
if (*atop == NULL)
return ;
if (rev)
*atop = (*atop)->prev;
else
*atop = (*atop)->next;
}
void swap(t_dblstck **atop)
{
t_dblstck *first;
t_dblstck *second;
first = *atop;
if (first == NULL)
return ;
if (first->next->next == first)
{
*atop = first->next;
return ;
}
second = first->next;
first->prev->next = second;
second->next->prev = first;
second->prev = first->prev;
first->next = second->next;
first->prev = second;
second->next = first;
*atop = second;
}
void push(t_dblstck **dst_top, t_dblstck **src_top)
{
t_dblstck *tmp;
if (*src_top == NULL)
return ;
tmp = *src_top;
*src_top = (*src_top)->next;
tmp->next->prev = tmp->prev;
tmp->prev->next = tmp->next;
if (tmp->next == tmp)
*src_top = NULL;
if (*dst_top == NULL)
{
tmp->next = tmp;
tmp->prev = tmp;
*dst_top = tmp;
}
else
add(dst_top, tmp);
}