-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_basic1.c
More file actions
94 lines (83 loc) · 2 KB
/
Copy pathstack_basic1.c
File metadata and controls
94 lines (83 loc) · 2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_basic1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sryou <sryou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 13:53:17 by sryou #+# #+# */
/* Updated: 2022/07/27 14:17:49 by sryou ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_node *stack_newnode(int value)
{
t_node *node;
node = (t_node *)malloc(sizeof(t_node));
if (node == 0)
return (0);
node->value = value;
node->next = 0;
node->prev = 0;
return (node);
}
void stack_push_back(t_stack *st, int value)
{
t_node *temp;
if (st == 0)
return ;
temp = stack_newnode(value);
st->size++;
if (st->front == 0)
{
st->front = temp;
st->back = temp;
}
else
{
temp->prev = st->back;
st->back->next = temp;
st->back = temp;
}
}
void stack_push_front(t_stack *st, int value)
{
t_node *temp;
if (st == 0)
return ;
temp = stack_newnode(value);
st->size++;
if (st->front == 0)
{
st->front = temp;
st->back = temp;
}
else
{
temp->next = st->front;
st->front->prev = temp;
st->front = temp;
}
}
int stack_pop_back(t_stack *st)
{
int ret;
t_node *temp;
ret = st->back->value;
temp = st->back;
st->back = temp->prev;
stack_delnode(temp);
st->size--;
return (ret);
}
int stack_pop_front(t_stack *st)
{
int ret;
t_node *temp;
ret = st->front->value;
temp = st->front;
st->front = temp->next;
stack_delnode(temp);
st->size--;
return (ret);
}