-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1000-sort_deck.c
109 lines (97 loc) · 2.23 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
#include "deck.h"
/**
* *_strcmp - compare two strings
*@s1: pointer 1
*@s2: pointer 2
*Return: An integer + - or 0
*/
int _strcmp(const char *s1, const char *s2)
{
int i = 0;
while (*(s1 + i) && *(s2 + i))
{
if (s1[i] != s2[i])
break;
i++;
}
return (s1[i] - s2[i]);
}
/**
* get_card_value - Getting the int card value to work with
* @card: Actual card to get value
* Return: Card's Value
*
*/
int get_card_value(deck_node_t *card)
{
int value = atoi(card->card->value);
if (value != 0)
return (value);
if (_strcmp(card->card->value, "Jack") == 0)
return (11);
if (_strcmp(card->card->value, "Queen") == 0)
return (12);
if (_strcmp(card->card->value, "King") == 0)
return (13);
return (0);
}
/**
* swap_nodes - Swap nodes in a double linked list
* @prev: pointer to prev node
* @actual: pointer o actual node
* @head: to the head of the deck
* Return: Nothing
*/
void swap_nodes(deck_node_t **prev, deck_node_t *actual,
deck_node_t **head)
{
(*prev)->next = actual->next;
if (actual->next != NULL)
actual->next->prev = *prev;
actual->prev = (*prev)->prev;
actual->next = *prev;
if ((*prev)->prev != NULL)
(*prev)->prev->next = actual;
else
*head = actual; /* prev node is NULL*/
(*prev)->prev = actual;
*prev = actual->prev;
}
/**
* insertion_sort_list - sorts a deck of cards
* in ascending order using the Insertion sort algorithm
* @list: list to look into
* @option: sort by value if 0, otherwise sort by kind
* Return: Nothing
*/
void insertion_sort_list(deck_node_t **list, int option)
{
deck_node_t *actual, *prev;
if (list == NULL || *list == NULL)
return;
for (actual = (*list)->next; actual != NULL; actual = actual->next)
{
prev = actual->prev;
if (!option)
while (actual->prev != NULL && get_card_value(actual) <
get_card_value(actual->prev))
swap_nodes(&prev, actual, list);
else
while (actual->prev != NULL && actual->card->kind <
actual->prev->card->kind)
swap_nodes(&prev, actual, list);
}
}
/**
* sort_deck - sorts a deck of cards.
* @deck: Deck to sort
* Return: Nothing
*
*/
void sort_deck(deck_node_t **deck)
{
if (deck == NULL || *deck == NULL)
return;
insertion_sort_list(deck, 0);
insertion_sort_list(deck, 1);
}