-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1000-sort_deck.c
More file actions
131 lines (127 loc) · 2.75 KB
/
1000-sort_deck.c
File metadata and controls
131 lines (127 loc) · 2.75 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
#include "deck.h"
/**
* aux_num_fun - turn into integer card value
* @head_tmp1: pointer to the list
* Return: integer rep
**/
int aux_num_fun(deck_node_t *head_tmp1)
{
int aux_num, j;
int num[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
char val[13] = {'A', '2', '3', '4', '5', '6', '7',
'8', '9', '1', 'J', 'Q', 'K'};
for (j = 0; j < 13; j++)
{
if (head_tmp1->card->value[0] == val[j])
aux_num = num[j];
}
return (aux_num);
}
/**
* num_sort - sorts a doubly linked list of integers, 4 stages
* @list: pointer to the list head
* Return: no return
**/
void num_sort(deck_node_t **list)
{
deck_node_t *head_tmp1, *head_tmp2, *aux1, *aux2;
int flag = 0, i, aux_num1, aux_num2;
unsigned int k;
head_tmp1 = *list;
head_tmp2 = *list;
for (i = 0; i < 4; i++)
{ k = head_tmp1->card->kind;
while (head_tmp1->next && head_tmp1->next->card->kind == k)
{
aux_num1 = aux_num_fun(head_tmp1);
aux_num2 = aux_num_fun(head_tmp1->next);
flag = 0;
head_tmp2 = head_tmp1;
while (head_tmp2 && head_tmp2->card->kind == k && aux_num1 > aux_num2)
{
aux1 = head_tmp2;
aux2 = head_tmp2->next;
aux1->next = aux2->next;
if (aux2->next)
aux2->next->prev = aux1;
aux2->prev = aux1->prev;
aux2->next = aux1;
aux1->prev = aux2;
if (aux2->prev)
aux2->prev->next = aux2;
head_tmp2 = aux2->prev;
if (!aux2->prev)
*list = aux2;
flag = 1;
if (!head_tmp2)
break;
aux_num1 = aux_num_fun(head_tmp2);
aux_num2 = aux_num_fun(head_tmp2->next);
}
if (flag == 0)
head_tmp1 = head_tmp1->next;
}
head_tmp1 = head_tmp1->next;
}
}
/**
* kind_sort - sorts a doubly linked list of integers
* in ascending order using the Insertion sort ailgorithm
* @list: pointer to the list head
* Return: no return
**/
void kind_sort(deck_node_t **list)
{
deck_node_t *head_tmp1, *head_tmp2, *aux1, *aux2;
int flag;
if (list)
{
head_tmp1 = *list;
head_tmp2 = *list;
while (list && head_tmp1->next)
{
if (head_tmp1->next)
{
flag = 0;
head_tmp2 = head_tmp1;
while (head_tmp2 && head_tmp2->card->kind > head_tmp2->next->card->kind)
{
aux1 = head_tmp2;
aux2 = head_tmp2->next;
aux1->next = aux2->next;
if (aux2->next)
aux2->next->prev = aux1;
if (aux2)
{
aux2->prev = aux1->prev;
aux2->next = aux1;
}
if (aux1)
aux1->prev = aux2;
if (aux2->prev)
aux2->prev->next = aux2;
head_tmp2 = aux2->prev;
if (!aux2->prev)
*list = aux2;
flag = 1;
}
}
if (flag == 0)
head_tmp1 = head_tmp1->next;
}
}
}
/**
* sort_deck - sorts a deck of cards
* @deck: ponter to the deck
* Return: no return
*
**/
void sort_deck(deck_node_t **deck)
{
if (deck)
{
kind_sort(deck);
num_sort(deck);
}
}