-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
103 lines (93 loc) · 2.23 KB
/
stack.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
93
94
95
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yenoh <yenoh@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/13 12:36:00 by yenoh #+# #+# */
/* Updated: 2023/11/13 12:36:00 by yenoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void init_stack(t_stack *stack)
{
stack->len = 0;
stack->head = NULL;
stack->tail = NULL;
}
void push_left(t_stack *stack, int num)
{
t_node *new;
new = (t_node *)malloc(sizeof(t_node));
new->num = num;
new->prev = NULL;
if (stack->len == 0)
{
new->next = NULL;
stack->head = new;
stack->tail = new;
}
else
{
new->next = stack->head;
stack->head->prev = new;
stack->head = new;
}
stack->len++;
}
void push(t_stack *stack, int num)
{
t_node *new;
new = (t_node *)malloc(sizeof(t_node));
new->num = num;
new->next = NULL;
if (stack->len == 0)
{
new->prev = NULL;
stack->head = new;
stack->tail = new;
}
else
{
stack->tail->next = new;
new->prev = stack->tail;
stack->tail = new;
}
stack->len++;
}
int pop_left(t_stack *stack)
{
t_node *tmp;
int n;
tmp = stack->head;
n = tmp->num;
stack->head = stack->head->next;
if (stack->len == 1)
stack->tail = stack->head;
else
stack->head->prev = NULL;
stack->len--;
free(tmp);
return (n);
}
int pop(t_stack *stack)
{
t_node *tmp;
int n;
tmp = stack->tail;
n = tmp->num;
if (stack->len == 1)
{
stack->tail = NULL;
stack->head = NULL;
}
else
{
tmp->prev->next = NULL;
stack->tail = tmp->prev;
}
stack->len--;
free(tmp);
return (n);
}