а вот то что я наработал
Код:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class ViolationList;
class Tree
{
public:
friend ViolationList;
struct Node // one element of the tree
{
int value; // useful data
Node* parent;
Node* right;
Node* left;
void ShowNode()
{
cout << value << "\t";
}
};
private:
Node* root;
// int count;
public:
Tree()
{
root = 0;
}
bool IsEmpty()
{
return root == 0;
}
~Tree()
{
Clear();
}
private:
void ShowTree(Node* elem)
{
if (elem != 0)
{
ShowTree(elem->left);
elem->ShowNode();
ShowTree(elem->right);
}
}
public:
void ShowTree()
{
cout << "\n\n";
ShowTree(root);
cout << "\n\n";
}
Node* GetRoot()
{
return root;
}
private:
int GetCount(Node* elem, int count)
{
if (elem != 0)
{
count = GetCount(elem->left, count);
count++;
count = GetCount(elem->right, count);
}
return count;
}
public:
int GetCount()
{
int count = 0;
count = GetCount(root, count);
return count;
}
private:
void Clear(Node* elem)
{
if (elem != 0)
{
Clear(elem->left);
Clear(elem->right);
delete elem;
elem = 0;
}
}
public:
void Clear()
{
if (!IsEmpty())
{
Clear(root);
root = 0;
}
}
Node* FindNode(int value)
{
if (IsEmpty())
return 0;
else
{
Node* f = root;
while (true)
{
if (value < f->value)
{
if (f->left != 0)
f = f->left;
else
break;
}
else if (value > f->value)
{
if (f->right != 0)
f = f->right;
else
break;
}
else
return f;
}
return 0;
}
}
void AddNode(int value)
{
if (FindNode(value))
return;
Node* n = new Node;
n->right = n->left = 0;
n->value = value;
Node* parent = 0;
if (IsEmpty())
{
root = n;
root->parent = parent;
}
else
{
Node*p = root;
while (p != 0)
{
parent = p;
if (n->value > p->value)
p = p->right;
else
p = p->left;
}
if (n->value < parent->value)
parent->left = n;
else
parent->right = n;
n->parent = parent;
}
}
Tree& operator = (const Tree& obj)
{
if (!IsEmpty())
Clear();
AddNode(obj.root->value);
Copy(obj.root);
return *this;
}
Tree(const Tree& obj)
{
root = 0;
AddNode(obj.root->value);
Copy(obj.root);
}
void Copy(Node* elem)
{
if (elem->left != 0)
AddNode(elem->left->value);
if (elem->right != 0)
AddNode(elem->right->value);
if (elem->left != 0)
Copy(elem->left);
if (elem->right != 0)
Copy(elem->right);
}
/*void DeleteNode(int value)
{
Node* d = FindNode(value);
if (d == 0)
return;
Node* parent = d->parent;
if (d == root && GetCount() == 1)
{
Clear();
return;
}
if (d->left == 0 && d->right == 0)
{
if (parent->left == d)
parent->left = 0;
else
parent->right = 0;
delete d;
return;
}
if (d->left == 0 && d->right != 0)
{
if (parent->left == d)
{
parent->left = d->right;
}
else
{
parent->right = d->right;
}
d->right->parent = parent;
delete d;
return;
}
if (d->left != 0 && d->right == 0)
{
if (parent->left == d)
{
parent->left = d->left;
}
else
{
parent->right = d->left;
}
d->left->parent = parent;
delete d;
return;
}
if (d->left != 0 && d->right != 0)
{
Node*r = d->right;
if (r->right == 0 && r->left == 0)
{
d->value = r->value;
d->right = 0;
delete r;
}
else if (r->left != 0)
{
Node*p = r->left;
while (p->left != 0)
p = p->left;
d->value = p->value;
if (p->right == 0)
p->parent->left = 0;
else
p->parent->left = p->right;
delete p;
}
else
{
d->value = r->value;
d->right = r->right;
delete r;
}
}
}
*/
};
//ViolationList – список типа Violation
class ViolationList
{
public:
struct Violation
{
int key; //«ключ-значение» номер автомобиля
char* offense;
Violation *pNext = nullptr;
};
private:
Violation *head = nullptr;
Violation *tail = nullptr;
int count = 0;
public:
ViolationList()
{
}
~ViolationList()
{
while (head != NULL)
{
Violation *temp = head->pNext;
delete head;
head = temp;
}
}
//добавыть правонарушения
void add(char* value)
{
Violation* newElem = new Violation();
newElem->offense = value;
if (tail == nullptr)
{
head = newElem;
}
else
{
tail->pNext = newElem;
}
tail = newElem;
count++;
}
//удалить правонарушения
void removeAt(int index)
{
Violation *pTemp = head;
for (int i = 1; i<index&&pTemp->pNext != NULL; i++)
pTemp = pTemp->pNext;
if (index == 0) {
head = head->pNext;
delete pTemp;
pTemp = head;
}
else {
Violation *pTempDel = pTemp->pNext;
pTemp->pNext = pTempDel->pNext;
delete pTempDel;
}
}
void show()
{
if (head != NULL) {
Violation * pTemp = head;
while (pTemp != NULL) {
cout << pTemp->offense << endl;
pTemp = pTemp->pNext;
}
}
else
cout << "List is empty!" << endl;
}
};
class MyMap
{
int size = 0;
int capacity = 16;
public:
class CarInfo
{
string key = "";
int value = 0;
public:
CarInfo(string key, int value)
{
this->key = key;
this->value = value;
}
string GetKey()
{
return key;
}
int GetValue()
{
return value;
}
void SetValue(int value)
{
this->value = value;
}
};
private:
CarInfo ** values = new CarInfo *[capacity];
public:
MyMap()
{
for (int i = 0; i < capacity; i++)
{
values[i] = nullptr;
}
}
int Get(string key)
{
for (int i = 0; i < size; i++)
{
if (values[i] != nullptr)
{
if (values[i]->GetKey() == key)
{
return values[i]->GetValue();
}
}
}
return -1;
}
void Put(string key, int value)
{
bool insert = true;
for (int i = 0; i < size; i++)
{
if (values[i]->GetKey() == key)
{
values[i]->SetValue(value);
insert = false;
}
}
if (insert)
{
EnsureCapacity();
values[size++] = new CarInfo(key, value);
}
}
private:
void EnsureCapacity()
{
if (size == capacity)
{
capacity *= 2;
CarInfo ** temp = new CarInfo *[capacity];
for (int i = 0; i < capacity; i++)
{
if (i < size) temp[i] = values[i];
else temp[i] = nullptr;
}
delete[]values;
values = temp;
}
}
public:
int GetSize() const
{
return size;
}
void Remove(string key)
{
for (int i = 0; i < size; i++)
{
if (values[i]->GetKey() == key)
{
values[i] = nullptr;
size--;
CondenseArray(i);
}
}
}
private:
void CondenseArray(int start)
{
for (int i = start; i < size; i++)
{
values[i] = values[i + 1];
}
}
};
void main()
{
setlocale(0, "RU");
ViolationList bt;
bt.add("Эксплуатация ТС, номерные знаки, регистрация ТС");
bt.add("Прочие нарушения");
bt.add("Перевозка людей и грузов, буксировка, учебная езда");
bt.show();
cout << endl;
Tree v;
v.AddNode(23);
v.ShowTree();
MyMap map;
map.Put("Andrew", 11);
map.Put("Helen", 12);
map.Put("Denis", 10);
if (map.Get("Andrew") == 11)
{
cout << "yes 1\n";
}
for (int i = 0; i < 10; i++)
{
char* s = new char[5];
_itoa_s(i, s, 5, 10);
map.Put(string(s), i);
delete[]s;
}
if (map.GetSize() == 13)
{
cout << "yes 2\n";
}
if (map.Get("5") == 5)
{
cout << "yes 3\n";
}
}