Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_SI / Pract. 10 / task2 / ListofPoints.h
blob: 79ab3b64e68cb4a7f8338c192a0e3aa1271eb549 (plain) (blame)
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
#pragma once
#include <iostream>
struct Point
{
	int x;
	int y;

	void Print()
	{
		std::cout << x <<"; "<< y << std::endl;
	}
};

struct ListofPoints
{
	const int RESIZE = 5;
	int size=10;
	int currentSize=0;
	Point* arr = new Point[size];

	void addPoint(const Point& point)
	{
		if (currentSize>=size)
			resize();		
		arr[currentSize] = point;
		currentSize++;
	}

	void resize()
	{
		int totalSize = RESIZE + size;
		Point* arrTemp = new Point[totalSize];
		for (size_t i = 0; i < size; i++)
		{
			arrTemp[i] = arr[i];
		}
		Free();
		size = totalSize;
		arr = arrTemp;
		arrTemp = nullptr;
	}

	void clear()
	{
		Free();
		currentSize = 0;
		size = 0;
	}

	void pop()
	{
		int LastElement = currentSize - 1;
		arr[LastElement].x = arr[LastElement].y = 0;
		currentSize--;
	}
	
	void Free()
	{
		delete[] arr;
		arr = nullptr;
	}
	

	void Print()
	{
		for (unsigned i = 0; i < currentSize; i++)
		{
			arr[i].Print();
		}
	}
};