-
Notifications
You must be signed in to change notification settings - Fork 1
/
1000-sort_deck.c
138 lines (123 loc) · 2.88 KB
/
1000-sort_deck.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
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
135
136
137
138
#include "deck.h"
/**
* sort_deck - Function that sorts a deck of cards
* @deck: A pointer to the first node of deck_node_t doubly-linked list
* Return: None
*/
void sort_deck(deck_node_t **deck)
{
if (deck == NULL || *deck == NULL || (*deck)->next == NULL)
return;
sort_kind(deck);
sort_value(deck);
}
/**
* _strcmp - Function that compares 2 strings
* @first: The first string
* @second: The second string
*
* Return: +ve if first is greater, 0 if equal, -ve if first is less
*/
int _strcmp(const char *first, const char *second)
{
while (*first && *second && *first == *second)
{
first++;
second++;
}
if (*first != *second)
return (*first - *second);
return (0);
}
/**
* card_value - Functuin that returns the value of a card
* @card: A pointer to a deck_node_t card
* Return: Numerical value of the card
*/
char card_value(deck_node_t *card)
{
if (_strcmp(card->card->value, "Ace") == 0)
return (0);
if (_strcmp(card->card->value, "1") == 0)
return (1);
if (_strcmp(card->card->value, "2") == 0)
return (2);
if (_strcmp(card->card->value, "3") == 0)
return (3);
if (_strcmp(card->card->value, "4") == 0)
return (4);
if (_strcmp(card->card->value, "5") == 0)
return (5);
if (_strcmp(card->card->value, "6") == 0)
return (6);
if (_strcmp(card->card->value, "7") == 0)
return (7);
if (_strcmp(card->card->value, "8") == 0)
return (8);
if (_strcmp(card->card->value, "9") == 0)
return (9);
if (_strcmp(card->card->value, "10") == 0)
return (10);
if (_strcmp(card->card->value, "Jack") == 0)
return (11);
if (_strcmp(card->card->value, "Queen") == 0)
return (12);
return (13);
}
/**
* sort_kind - Function that sorts cards based on kind w/ insertion sort
* @deck: A pointer to the head of a deck_node_t doubly-linked list
* Return: None
*/
void sort_kind(deck_node_t **deck)
{
deck_node_t *i, *tmp, *shuf;
for (i = (*deck)->next; i != NULL; i = tmp)
{
tmp = i->next;
shuf = i->prev;
while (shuf != NULL && shuf->card->kind > i->card->kind)
{
shuf->next = i->next;
if (i->next != NULL)
i->next->prev = shuf;
i->prev = shuf->prev;
i->next = shuf;
if (shuf->prev != NULL)
shuf->prev->next = i;
else
*deck = i;
shuf->prev = i;
shuf = i->prev;
}
}
}
/**
* sort_value - Function that sorts cards based on value w/ insertion sort
* @deck: A pointer to the head of a deck_node_t doubly-linked list
* Return: None
*/
void sort_value(deck_node_t **deck)
{
deck_node_t *i, *tmp, *shuf;
for (i = (*deck)->next; i != NULL; i = tmp)
{
tmp = i->next;
shuf = i->prev;
while (shuf != NULL && shuf->card->kind == i->card->kind &&
card_value(shuf) > card_value(i))
{
shuf->next = i->next;
if (i->next != NULL)
i->next->prev = shuf;
i->prev = shuf->prev;
i->next = shuf;
if (shuf->prev != NULL)
shuf->prev->next = i;
else
*deck = i;
shuf->prev = i;
shuf = i->prev;
}
}
}