Repository navigation
Expand file tree
/
Copy pathlinked_list.c
More file actions
134 lines (121 loc) · 2.15 KB
/
Copy pathlinked_list.c
File metadata and controls
134 lines (121 loc) · 2.15 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "push_swap.h"
t_llist *make_list(t_arg *arg)
{
t_llist *list;
t_node *node;
list = init_list('a');
list->size = 0;
while (arg)
{
append(list, ft_atoi(arg->str));
arg = arg->next;
}
return (list);
}
void add_node(t_llist *list, int value)
{
t_node *new_node;
t_node *node;
new_node = malloc(sizeof(t_node));
new_node->value = value;
if (list->head == NULL)
{
list->head = new_node;
list->tail = new_node;
list->head->next = new_node;
list->head->prev = new_node;
list->tail->next = new_node;
list->tail->prev = new_node;
}
else
{
new_node->next = list->head;
list->head->prev = new_node;
new_node->prev = list->tail;
list->tail->next = new_node;
list->head = new_node;
}
list->size++;
}
void append(t_llist *list, int value)
{
t_node *node;
node = malloc(sizeof(t_node));
node->value = value;
if (list->head == NULL)
{
list->head = node;
list->tail = node;
list->head->next = node;
list->head->prev = node;
list->tail->next = node;
list->tail->prev = node;
}
else
{
list->tail->next = node;
node->prev = list->tail;
list->head->prev = node;
node->next = list->head;
list->tail = node;
}
list->size++;
}
int pop(t_llist *list)
{
int value;
t_node *node;
node = list->head;
value = list->head->value;
if (list->size != 1)
{
list->head->next->prev = list->tail;
list->tail->next = list->head->next;
list->head = list->head->next;
free(node);
}
else
{
list->head = NULL;
list->tail = NULL;
}
list->size--;
return (value);
}
void print_list(t_llist *list)
{
t_node *node;
int i;
printf("Head: %-3d\nTail: %-3d\n", list->head->value, list->tail->value);
i = 0;
node = list->head;
printf("=== up ===\n");
while (i < list->size)
{
printf("%d = %d\n", i, node->value);
node = node->next;
i++;
}
node = list->tail;
i--;
printf("=== down===\n");
while (i >= 0)
{
printf("%d = %d\n", i, node->value);
node = node->prev;
i--;
}
}
void print_node(t_node *node, int size)
{
int i;
printf("==== print node ====\n");
i = 0;
while (i < size)
{
printf("%d ", node->value);
node = node->next;
i++;
}
printf("\n====================\n");
}