-
Notifications
You must be signed in to change notification settings - Fork 0
/
1000-sort_deck.c
94 lines (89 loc) · 1.89 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
#include "deck.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* get_val - gets the relative value of a card from it's string value
* @str: string value of the card
*
* Return: relative value of the card (0 through 12)
*/
int get_val(const char *str)
{
int i;
char *array[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
for (i = 0; i < 13; i++)
{
if (strcmp(str, array[i]) == 0)
{
return (i);
}
}
exit(1);
}
/**
* swap_node - swaps a node with the next node in the list
* @list: double pointer to the beginning of the list
* @node: node to swap
*
* Return: void
*/
void swap_node(deck_node_t **list, deck_node_t *node)
{
node->next->prev = node->prev;
if (node->prev)
node->prev->next = node->next;
else
*list = node->next;
node->prev = node->next;
node->next = node->next->next;
node->prev->next = node;
if (node->next)
node->next->prev = node;
}
/**
* sort_deck - sorts a linked list deck of cards
* @deck: double pointer to the deck to sort
*
* Return: void
*/
void sort_deck(deck_node_t **deck)
{
char swapped = 1, c1, c2;
deck_node_t *current;
if (deck == NULL || *deck == NULL || (*deck)->next == NULL)
return;
current = *deck;
while (swapped != 0)
{
swapped = 0;
while (current->next != NULL)
{
c1 = get_val(current->card->value) + 13 * current->card->kind;
c2 = get_val(current->next->card->value) + 13 * current->next->card->kind;
if (c1 > c2)
{
swap_node(deck, current);
swapped = 1;
}
else
current = current->next;
}
if (swapped == 0)
break;
swapped = 0;
while (current->prev != NULL)
{
c1 = get_val(current->card->value) + 13 * current->card->kind;
c2 = get_val(current->prev->card->value) + 13 * current->prev->card->kind;
if (c1 < c2)
{
swap_node(deck, current->prev);
swapped = 1;
}
else
current = current->prev;
}
}
}