Pojemnik na dynamicznej tablicy jednowymiarowej
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 | #include<iostream>
using namespace std;
class Figura {
private:
string nazwa;
public:
virtual double pole() = 0;
virtual string typ() = 0;
Figura(string n) {
nazwa = n;
}
string get_nazwa() {
return nazwa;
}
};
class Kwadrat : public Figura {
private:
double bok;
public:
Kwadrat(string n, double b) : Figura(n) {
bok = b;
}
double pole() {
return bok * bok;
}
string typ() {
return "kwadrat";
}
};
class Trojkat : public Figura {
private:
double podstawa;
double wysokosc;
public:
Trojkat(string n, double p, double w) : Figura(n) {
podstawa = p;
wysokosc = w;
}
double pole() {
return (podstawa * wysokosc) / 2;
}
string typ() {
return "trojkat";
}
};
class Pojemnik {
private:
Figura** wsk;
int licznik;
int rozmiar;
public:
Pojemnik(int r) {
r = rozmiar;
wsk = new Figura*[rozmiar];
licznik = 0;
}
void wyswietlanie() {
for(int i = 0; i < licznik; i++) {
cout << wsk[i]->get_nazwa() << " " << wsk[i]->typ() << " " << wsk[i]->pole() << " ";
}
}
void add(Figura* figura) {
if(licznik == rozmiar) {
rozmiar += 2;
Figura** temp = new Figura*[rozmiar];
for(int i = 0; i < rozmiar - 2; i++) {
temp[i] = wsk[i];
}
wsk = temp;
}
wsk[licznik] = figura;
for(int i = 0; i < licznik - 1; i++) {
for(int j = 0; j < licznik - i - 1; j++) {
if((wsk[j]->pole() > wsk[j + 1]->pole()) or ((wsk[j]->pole() == wsk[j + 1]->pole()) and wsk[j]->get_nazwa() > wsk[j + 1]->get_nazwa())) {
swap(wsk[j], wsk[j + 1]);
}
}
}
licznik++;
}
void removee(string nazwa) {
for(int i = 0; i < licznik; i++) {
if (wsk[i]->get_nazwa() == nazwa) {
delete wsk[i];
for(int j = i; j < licznik - 1; j++) {
wsk[j] = wsk[j + 1];
}
licznik--;
i--;
}
}
}
};
int main() {
int N;
cin >> N;
int M;
cin >> M;
Pojemnik pojemnik(N);
for(int i = 0; i < M; i++) {
string typ, nazwa;
double dlugosc, wysokosc;
cin >> typ;
if(typ == "trojkat") {
cin >> nazwa >> dlugosc >> wysokosc;
pojemnik.add(new Trojkat(nazwa, dlugosc, wysokosc));
}
if(typ == "kwadrat") {
cin >> nazwa >> dlugosc;
pojemnik.add(new Kwadrat(nazwa, dlugosc));
}
}
int L;
cin >> L;
for(int i= 0; i < L; i++) {
string nazwa;
cin >> nazwa;
pojemnik.removee(nazwa);
}
pojemnik.wyswietlanie();
return 0;
}
|
Lista jednokierunkowa
Lista dwukierunkowa
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #include<iostream>
#include<cmath>
#include <cassert>
using namespace std;
template <typename T>
class Lista;
template <typename T>
class Element {
private:
T wartosc;
Element<T> *next;
Element<T> *prev;
public:
Element(T w, Element<T> *n = nullptr, Element<T> *p = nullptr) {
wartosc = w;
next = n;
prev = p;
}
~Element() {}
T getData() {
return wartosc;
}
Element<T> *getNext() {
return next;
}
Element<T> *getPrev() {
return prev;
}
friend class Lista<T>;
};
template <typename T>
class Lista {
private:
Element<T> *first;
Element<T> *last;
public:
Lista() {
first = nullptr;
last = nullptr;
}
~Lista() {}
void add(T val) {
Element<T>* newElement = new Element<T>(val);
if (!first) {
first = last = newElement;
} else {
Element<T>* current = first;
Element<T>* prev = nullptr;
while (current && val > current->getData()) {
prev = current;
current = current->getNext();
}
if (!current) {
newElement->prev = last;
last->next = newElement;
last = newElement;
} else {
newElement->next = current;
newElement->prev = prev;
if (prev) {
prev->next = newElement;
} else {
first = newElement;
}
current->prev = newElement;
}
}
}
void removeElement(T val) {
Element<T>* current = first;
while (current) {
if (current->getData() == val) {
Element<T>* next = current->getNext();
Element<T>* prev = current->getPrev();
if (prev) {
prev->next = next;
} else {
first = next;
}
if (next) {
next->prev = prev;
} else {
last = prev;
}
Element<T>* temp = current;
current = next;
delete temp;
} else {
current = current->getNext();
}
}
}
friend ostream& operator<<(ostream& os, Lista<T>& list) {
Element<T> *current = list.last;
while (current) {
os << current->getData() << " ";
current = current->getPrev();
}
return os;
}
};
class Zespolona {
private:
float real;
float imaginary;
public:
Zespolona(float r = 0, float i = 0) {
real = r;
imaginary = i;
}
~Zespolona() {}
float calculateMagnitude() const {
return sqrt(real * real + imaginary * imaginary);
}
bool operator<(const Zespolona& other) const {
if (calculateMagnitude() == other.calculateMagnitude()) {
if (real == other.real) {
return imaginary < other.imaginary;
}
return real < other.real;
}
return calculateMagnitude() < other.calculateMagnitude();
}
bool operator>(const Zespolona& other) const {
return other < *this;
}
bool operator==(const Zespolona& other) const {
return real == other.real && imaginary == other.imaginary;
}
friend ostream& operator<<(ostream& os, const Zespolona& complex) {
os << complex.real;
if (complex.imaginary != 0) {
if(complex.imaginary < 0) {
os << complex.imaginary << "i";
}
if(complex.imaginary > 0) {
os << "+" << complex.imaginary << "i";
}
}
return os;
}
};
int main() {
int M, N;
cin >> M;
Lista<Zespolona> lista;
for (int i = 0; i < M; i++) {
float real, imaginary;
cin >> real;
cin >> imaginary;
lista.add(Zespolona(real, imaginary));
}
cout << lista;
cin >> N;
for (int i = 0; i < N; ++i) {
float real, imaginary;
cin >> real;
cin >> imaginary;
lista.removeElement(Zespolona(real, imaginary));
}
cout << lista;
return 1;
}
|
ONP
Drzewo poszukiwań binarnych
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | #include<iostream>
using namespace std;
template <typename T>
class Drzewo;
template <typename T>
class Element {
private:
Element<T> *left_child;
Element<T> *right_child;
T wartosc;
public:
Element() {}
Element(T w, Element<T> *l = nullptr, Element<T> *r = nullptr) {
wartosc = w;
left_child = l;
right_child = r;
}
~Element() {}
friend class Drzewo<T>;
};
template <typename T>
class Drzewo {
private:
Element<T> *first;
public:
Drzewo() {
first = nullptr;
}
void add(T wartosc) {
if(first == nullptr) {
first = new Element<T>(wartosc);
return;
}
Element<T> *curr = first;
while(true) {
if(wartosc > curr->wartosc) {
if(curr->left_child == nullptr) {
curr->left_child = new Element<T>(wartosc);
return;
}
curr = curr->left_child;
}
else {
if(curr->right_child == nullptr) {
curr->right_child = new Element<T>(wartosc);
return;
}
curr = curr->right_child;
}
}
}
void remove_w(T data) {
if (first == nullptr) {
return;
}
Element<T> *current = first;
Element<T> *parent = nullptr;
while (current != nullptr && current->wartosc != data) {
parent = current;
if (data < current->wartosc) {
current = current->right_child;
} else {
current = current->left_child;
}
}
if (current == nullptr) {
return;
}
// Element nie ma dzieci
if (current->left_child == nullptr && current->right_child == nullptr) {
if (parent == nullptr) {
first = nullptr;
} else {
if (data < parent->wartosc) {
parent->right_child = nullptr;
} else {
parent->left_child = nullptr;
}
}
delete current;
return;
}
// Element ma jedno dziecko
if (current->left_child == nullptr) {
if (parent == nullptr) {
first = current->right_child;
} else {
if (data < parent->wartosc) {
parent->right_child = current->right_child;
} else {
parent->left_child = current->right_child;
}
}
delete current;
return;
} else if (current->right_child == nullptr) {
if (parent == nullptr) {
first = current->left_child;
} else {
if (data < parent->wartosc) {
parent->right_child = current->left_child;
} else {
parent->left_child = current->left_child;
}
}
delete current;
return;
}
// Element ma dwa dzieci
// Znajdujemy następcę usuwanego węzła
Element<T> *successor = current->right_child;
Element<T> *successorParent = current;
while (successor->left_child != nullptr) {
successorParent = successor;
successor = successor->left_child;
}
// Zastępujemy usuwany węzeł następcą
if (successorParent == current) {
current->right_child = successor->right_child;
} else {
successorParent->left_child = successor->right_child;
}
// Kopiujemy dane następcy do usuwanego węzła
current->wartosc = successor->wartosc;
// Usuwamy następcę
delete successor;
}
void preOrder(Element<T> *node) {
if (node == nullptr) {
return;
}
cout << node->wartosc << " ";
preOrder(node->left_child);
preOrder(node->right_child);
}
void inOrder(Element<T> *node) {
if (node == nullptr) {
return;
}
inOrder(node->left_child);
cout << node->wartosc << " ";
inOrder(node->right_child);
}
void postOrder(Element<T> *node) {
if (node == nullptr) {
return;
}
postOrder(node->left_child);
postOrder(node->right_child);
cout << node->wartosc << " ";
}
void preorder_print() {
preOrder(first);
}
void inorder_print() {
inOrder(first);
}
void postorder_print() {
postOrder(first);
}
int longestPath(Element<T> *node) {
if (node == nullptr) {
return 0;
}
int longestPathLeft = longestPath(node->left_child);
int longestPathRight = longestPath(node->right_child);
return max(longestPathLeft, longestPathRight) + 1;
}
int shortestPath(Element<T> *node) {
if (node == nullptr) {
return 0;
}
int shortestPathLeft = shortestPath(node->left_child);
int shortestPathRight = shortestPath(node->right_child);
return min(shortestPathLeft, shortestPathRight) + 1;
}
void shortest() {
cout << shortestPath(first) << " ";
}
void longest() {
cout << longestPath(first) - 1 << " ";
}
};
int main() {
Drzewo<double> drzewo;
int k;
cin >> k;
for(int i =0; i < k; i++) {
double wartosc;
cin >> wartosc;
drzewo.add(wartosc);
}
int l;
cin >> l;
for(int i = 0; i < l; i++) {
double wartosc;
cin >> wartosc;
for(int j = 0; j < 10; j++) {
drzewo.remove_w(wartosc);
}
}
int m;
cin >> m;
switch(m) {
case 0:
drzewo.preorder_print();
break;
case 1:
drzewo.inorder_print();
break;
case 2:
drzewo.postorder_print();
break;
}
drzewo.longest();
drzewo.shortest();
return 1;
}
|