-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDECK.cpp
More file actions
130 lines (104 loc) · 2.95 KB
/
DECK.cpp
File metadata and controls
130 lines (104 loc) · 2.95 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
// Ñòðóêòóðû ËÐ-2
// 5 âàðèàíò
// Äåêè. âåùåñòâåííûé ýëåìåíò. Âûáîðêà ýë èç íà÷àëà. Äîáàâëåíèå ýë. â êîíåö
#include <iostream>
struct DeckEntry {
float value;
DeckEntry* prev;
DeckEntry* next;
};
void generateRandomDeck(DeckEntry* begin, DeckEntry* end, int size) {
begin->value = 10000.f * (float)rand() / RAND_MAX;
begin->prev = NULL;
end->next = NULL;
DeckEntry* previous = begin;
for (int i = 0; i < size - 2; i++)
{
DeckEntry* current = new DeckEntry;
current->value = 10000.f * (float)rand() / RAND_MAX;
current->prev = previous;
previous->next = current;
previous = current;
}
previous->next = end;
end->value = 10000.f * (float)rand() / RAND_MAX;
end->prev = previous;
}
void printDeck(DeckEntry* begin) {
DeckEntry* current = begin;
std::cout << "DECK:\n";
int counter = 0;
while (current != NULL) {
std::cout << '[' << counter << "] " << current->value << std::endl;
counter++;
current = current->next;
}
}
float PopFront(DeckEntry* begin) {
DeckEntry* next = begin->next;
float val = begin->value;
begin->value = next->value;
begin->next = next->next;
begin->prev = NULL;
return val;
}
void PushBack(DeckEntry* end, float value) {
DeckEntry* newend = new DeckEntry;
newend->value = value;
newend->prev = end->prev;
newend->next = NULL;
end->next = newend;
}
void clearDeck(DeckEntry* &begin, DeckEntry* &end) {
DeckEntry* current = begin;
while (current != NULL) {
DeckEntry* buf = current;
current = current->next;
delete buf;
}
begin = NULL;
end = NULL;
}
int main()
{
srand(1109);
int input = 0;
DeckEntry* begin = NULL;
DeckEntry* end = NULL;
while (true) {
std::cout << "0) Exit" << std::endl;
std::cout << "1) Create Deck" << std::endl;
std::cout << "2) Print Deck" << std::endl;
std::cout << "3) Clear Deck" << std::endl;
std::cout << "4) Pop Front" << std::endl;
std::cout << "5) Push Back" << std::endl;
std::cin >> input;
switch (input)
{
case 0:
return 0;
break;
case 1:
std::cout << "Size: ";
std::cin >> input;
begin = new DeckEntry;
end = new DeckEntry;
generateRandomDeck(begin, end, input);
break;
case 2:
printDeck(begin);
break;
case 3:
clearDeck(begin, end);
break;
case 4:
std::cout << "Front value: " << PopFront(begin) << std::endl;
break;
case 5:
std::cout << "Value: ";
std::cin >> input;
PushBack(end, input);
break;
}
}
}