252 / 158 / 118
Регистрация: 26.11.2012
Сообщений: 384
|
06.10.2016, 14:13
|
2
|
C++ | 1
| void queue ::push (figure *) // где имя переменной для хранения указателя? |
|
Добавлено через 1 час 42 минуты
Я бы сделал так
Figure
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
| //header
#pragma once
#include <iostream>
class Figure{
int i;
public:
Figure();
Figure(const int&);
virtual ~Figure();
virtual double GetSquare();
virtual void PrintName();
friend std::ostream& operator << (std::ostream&, const Figure&);
};
//c++
#include "Figure.h"
Figure::Figure():i(0)
{
}
Figure::Figure(const int & i): i(i){}
Figure::~Figure()
{
}
double Figure::GetSquare()
{
return 10.0;
}
void Figure::PrintName(){
std::cout << "Figure Name" << i;
}
std::ostream & operator<<(std::ostream & out, const Figure &f)
{
out << f.i;
return out;
} |
|
Node
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
| //header
#pragma once
#include "Figure.h"
struct Node{
Figure data;
Node*next;
Node();
Node(Figure, Node* = nullptr);
Node(const Node&);
Node(Node&&);
Node& operator = (const Node&);
Node& operator = (Node&&);
~Node();
};
//c++
#include "node.h"
Node::Node()
: data(Figure()), next(nullptr)
{}
Node::Node(Figure d, Node *n)
: data(d), next(n)
{}
Node::Node(const Node &other)
: data(other.data), next(other.next)
{}
Node::Node(Node &&other)
: data(other.data), next(other.next)
{
other.data = Figure(); other.next = nullptr;
}
Node & Node::operator=(const Node &other){
if (this != &other) {
data = other.data;
next = other.next;
}
return *this;
}
Node & Node::operator=(Node &&other){
if (this != &other) {
data = other.data;
next = other.next;
other.data = Figure();
other.next = nullptr;
}
return *this;
}
Node::~Node(){
} |
|
Queue
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
| //header
#pragma once
#include "node.h"
#include "Figure.h"
#include <iostream>
class Queue{
Node *first, *last;
size_t count;
void push_back(const Figure&);
void clear();
public:
Queue();
Queue(const Queue&);
Queue(Queue&&);
~Queue();
Queue &operator = (const Queue&);
Queue &operator = (Queue&&);
void push(const Figure&);
void pop();
Figure top()const;
size_t size()const;
friend std::ostream& operator << (std::ostream &out, const Queue&);
};
//c++
#include "queue.h"
void Queue::push_back(const Figure& f){
Node *tmp = new Node(f);
if (!first) first = tmp;
if (last) last->next = tmp;
last = tmp;
count++;
}
void Queue::clear(){
while (first) {
last = first;
first = first->next;
delete last;
}
first = last = nullptr;
count = 0;
}
Queue::Queue()
:first(nullptr), last(nullptr), count(0)
{}
Queue::Queue(const Queue &other)
: first(nullptr), last(nullptr), count(0)
{
Node *tmp = other.first;
while (tmp) {
push_back(tmp->data);
tmp = tmp->next;
}
}
Queue::Queue(Queue &&other)
: first(other.first), last(other.last), count(other.count)
{
other.first = other.last = nullptr;
other.count = 0;
}
Queue & Queue::operator=(const Queue &other){
if (this != &other) {
clear();
Node*tmp = other.first;
while (tmp) {
push_back(tmp->data);
tmp = tmp->next;
}
}
return *this;
}
Queue & Queue::operator=(Queue &&other){
if (this != &other) {
first = other.first;
last = other.last;
count = other.count;
other.first = other.last = nullptr;
other.count = 0;
}
return *this;
}
void Queue::push(const Figure &f){
first = new Node(f, first);
if (!last) last = first;
count++;
}
void Queue::pop(){
Node* tmp = first;
first = first->next;
delete tmp;
if (!first) last = nullptr;
count--;
}
Figure Queue::top() const{
return first ->data;
}
size_t Queue::size() const{
return count;
}
Queue::~Queue(){
clear();
}
std::ostream & operator<<(std::ostream & out, const Queue &q){
Node *tmp = q.first;
while (tmp) {
out << tmp->data << " | ";
tmp = tmp->next;
}
return out;
} |
|
main
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <iostream>
#include "queue.h"
#include "Figure.h"
int main()
{
Queue q;
for (int i = 0; i < 10; i++) q.push(Figure(i + 1));
Queue q1(q);
Queue q2;
q2 = q;
std::cout << q << std::endl;
std::cout << q1 << std::endl;
std::cout << q2 << std::endl;
while (q.size() > 0) q.pop();
std::cout << q << std::endl;
system("pause");
return 0;
} |
|
Добавлено через 5 минут
Класс "Figure" у меня для тестирования, реализацию напишите свою
0
|