This repository was archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck_list.cpp
More file actions
174 lines (164 loc) · 2.7 KB
/
Copy pathdeck_list.cpp
File metadata and controls
174 lines (164 loc) · 2.7 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
struct Node
{
int Value;
Node *Next;
Node *Prev;
};
class deck
{
Node *Head, *Tail;
public:
deck();
void push_front(int );
void push_back(int );
bool pop_front(int &);
bool pop_back(int &);
int size();
bool isEmpty();
void print();
~deck();
};
deck::deck()
{
Head = new Node;
Tail = new Node;
Head->Next = Tail;
Head->Prev = NULL;
Tail->Next = NULL;
Tail->Prev = Head;
}
deck::~deck()
{
Node *t = Head->Next, *p;
delete Head;
while (t != Tail)
{
p = t;
t = t->Next;
delete p;
}
delete Tail;
}
void deck::push_front(int item)
{
Node *t = new Node;
t->Value = item;
t->Next = Head->Next;
t->Prev = Head;
Head->Next = t;
(t->Next)->Prev = t;
}
void deck::push_back(int item)
{
Node *t = new Node;
t->Value = item;
t->Next = Tail;
t->Prev = Tail->Prev;
Tail->Prev = t;
(t->Prev)->Next = t;
}
bool deck::pop_front(int &out)
{
if (isEmpty())
{
cout << "Error: deck is empty..." << endl;
return false;
}
Node *t = Head->Next;
out = t->Value;
Head->Next = t->Next;
(t->Next)->Prev = Head;
delete t;
return true;
}
bool deck::pop_back(int &out)
{
if (isEmpty())
{
// cout << "Error: deck is empty..." << endl;
return false;
}
Node *t = Tail->Prev;
out = t->Value;
Tail->Prev = t->Prev;
(t->Prev)->Next = Tail;
delete t;
return true;
}
bool deck::isEmpty()
{
if (Head->Next == Tail)
return true;
return false;
}
int deck::size()
{
int count = 0;
Node *t = Head;
while (t->Next != Tail)
count++;
return count;
}
void deck::print()
{
Node *t = Head->Next;
// cout << "=============================" << endl;
while (t != Tail)
{
cout << setw(5) << t->Value << " ";
t = t->Next;
}
// cout << endl << "=============================" << endl;
}
void createMatrix(deck *(&Matrix), int size)
{
Matrix = new deck [size];
for (int IndxLine = 0; IndxLine < size; IndxLine++)
{
for (int IndxColumn = 0; IndxColumn < size; IndxColumn++)
{
if (IndxColumn % 2 == 0)
Matrix[IndxLine].push_back(rand() % 101 - 50);
else
Matrix[IndxLine].push_front(rand() % 101 - 50);
}
Matrix[IndxLine].print();
cout << endl;
}
}
void fillVector(deck *Matrix, int size, deck &v)
{
int value;
for (int i = 0; i < size; i++)
{
for (int j = 0; j <= i; j++)
Matrix[i].pop_front(value);
v.push_back(value);
}
cout << endl;
//v.print();
}
void printVector(int *vector, int size)
{
for (int i = 0; i < size; i++)
cout << vector[i] << " ";
cout << endl;
}
int main()
{
srand(time(NULL));
int size = 0;
deck *Matrix;
deck vector;
cout << "Enter size of matrix: ";
cin >> size;
createMatrix(Matrix, size);
fillVector(Matrix, size, vector);
vector.print();
return 0;
}