-
Notifications
You must be signed in to change notification settings - Fork 1
/
List.cpp
149 lines (138 loc) · 4.06 KB
/
List.cpp
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
#include <iostream>
#include <stddef.h>
using namespace std;
// Клас за обектите, които ще се добавят към списъка
class CAT
{
public:
CAT()
{
itsAge = 1; //Конструктор
}
CAT(int age):itsAge(age) {} //Конструктор
~CAT() {}; //Деструктор
int GetAge() const
{
return itsAge; //Член-функция за достъп
}
private:
int itsAge; //Член-променлива
};
// Клас за управление на списъка, подрежда котките по възраст
class Node
{
public:
Node (CAT*); //Конструктор с параметър указател към обект от клас CAT
~Node(); //Деструктор
void SetNext(Node * node)
{
itsNext = node; //Член-функции
}
Node * GetNext() const
{
return itsNext;
}
CAT * GetCat() const
{
return itsCat;
}
void Insert(Node *);
void Display();
int Sum();
private:
CAT *itsCat;
Node * itsNext;
};
//Дефиниция на конструктор
Node::Node(CAT* pCat):
itsCat(pCat),
itsNext(0)
{}
//Дефиниция на деструктор
Node::~Node()
{
cout << "Deleting node...\n";
delete itsCat;
itsCat = 0;
delete itsNext;
itsNext = 0;
}
//Член-функция за подреждане на котките по възраст
void Node::Insert(Node* newNode)
{
if (!itsNext)
itsNext = newNode;
else
{
int NextCatsAge = itsNext->GetCat()->GetAge(); // NextCatsAge възрастта на следващата котка
int NewAge = newNode->GetCat()->GetAge(); // Присвоява на променливата NewAge възрастта на котката от новия възел
int ThisNodeAge = itsCat->GetAge(); // Присвоява на променливата ThisNodeAge възрастта на котката от текущия възел
//Следва проверка дали възрастта на котката от новия възел е по-голяма или равна на възрастта на котката от текущия възел и същевременно по-малка от възрастта на следващата котка.
if ( NewAge >= ThisNodeAge && NewAge < NextCatsAge )
{
// Ако да указателят от новия възел се насочва към следващия възел, а указателят от текущия към следващия възел се насочва към новия възел
newNode->SetNext(itsNext);
itsNext = newNode;
}
else
itsNext-> Insert (newNode);//Ако не функцията Insert() се прилага върху следвашия възел (т.е той става текущ)
}
}
//Член функция за извеждане на подредените по възраст котки
void Node::Display()
{
if (itsCat->GetAge() > 0)
{
cout << itsCat->GetAge() << " years old\n";
}
if (itsNext) {
itsNext->Display();
}
}
int Node::Sum()
{
int n = itsCat->GetAge();
cout<<"n"<<n<<endl;
if (itsNext)
{
return n+itsNext->Sum();
}
else
{
return n;
}
}
//Главна функция
int main()
{
cout<<fin(6)<<endl;
int b = 0;
if (!b) {
cout<<"fgfg"<<endl;
}
if (b == 0) {
cout<<"BB"<<endl;
}
Node *pNode = 0;
CAT * pCat = new CAT(0);
int age;
Node *pHead = new Node(pCat);
while (1)
{
cout << "New Cat's age? (0 to quit): ";
cin >> age;
if (!age) {
break;
}
pCat = new CAT(age);
pNode = new Node(pCat);
pHead->Insert(pNode);
}
//int age = pHead->GetCat().GetAge();
//cout<<age;
pHead->Display();
cout<<pHead->Sum();
delete pHead;
cout << "Exiting...\n\n";
return 0;
}