#include <iostream>
#include <string>
using namespace std;
// Struktura Aktor przechowujaca dane aktora - lista aktorów.
struct Aktor {
string nazwisko; // Imię i nazwisko razem - dla ułatwienia.
int lf; // Liczba fanów, którzy przestaną oglądać serial po odejściu aktora.
Aktor *next; // Wskaźnik na następny element listy.
Aktor(string nazwisko, int lf);
};
struct Lista {
Aktor *first; // wskaźnik na poczatek listy
Lista();
void insert_aktor(string nazwisko, int lf);
void find_aktor(int p);
Aktor * AktorSearch(int lfs);
};
Aktor::Aktor(string nazwisko, int lf) {
next = NULL;
this->nazwisko = nazwisko;
this->lf = lf;
}
Lista::Lista() {
first = NULL;
}
void Lista::insert_aktor(string nazwisko, int lf) {
Aktor *nowy = new Aktor(nazwisko, lf);
Aktor *pomoc = first, *pomoc2 = NULL; // Wskazniki pomocnicze do poruszania się po liscie.
if (first == NULL) {
nowy->next = NULL;
first = nowy;
cout << "TAK" << endl;
}
else {
while ((pomoc != NULL) && ((pomoc->lf) < (nowy->lf))) { // Dopoki nie wyszlismy za liste i aktor na liscie jest przed nowym aktorem, poruszamy sie dalej.
pomoc2 = pomoc; // pomoc2 zawsze wskazuje poprzednika pomoc.
pomoc = pomoc->next;
}
if (pomoc == NULL) {
nowy->next = NULL;
pomoc2->next = nowy;
cout << "TAK" << endl;
}
else if ((pomoc->lf) == (nowy->lf)) {
if (((pomoc->nazwisko) == (nowy->nazwisko))){ // Aktor z podanym nazwiskiem juz istnieje.
cout << "NIE" << endl;
delete nowy; // Usuniecie nowego obiektu aktora
}
else if (((pomoc->nazwisko) < (nowy->nazwisko))) {
//jak nowe nazwisko wieksze:
if (pomoc->next == NULL) {
nowy->next = NULL;
pomoc->next = nowy;
cout << "TAK" << endl;
}
else {
nowy->next = pomoc->next;
pomoc->next = nowy;
cout << "TAK" << endl;
}
}
else {
//jak nowe nazwisko mniejsze:
if (pomoc == first) {
nowy->next = first;
first = nowy;
cout << "TAK" << endl;
}
else{
nowy->next = pomoc;
pomoc2->next = nowy;
cout << "TAK" << endl;
}
}
/*else {
if (pomoc->nazwisko > nowy->nazwisko) {
nowy->next = pomoc->next;
pomoc->next = nowy;
cout << "TAK" << endl;
}
else {
if (pomoc == first) {
nowy->next = pomoc;
first = nowy;
cout << "TAK" << endl;
}
else {
nowy->next = pomoc;
pomoc2->next = nowy;
cout << "TAK" << endl;
}
}
}*/
}
else if ((pomoc->lf) < (nowy->lf)) {
// nowy->lf wiekszy:
if (pomoc->next == NULL) {
nowy->next = NULL;
pomoc->next = nowy;
cout << "TAK" << endl;
}
else {
nowy->next = pomoc->next;
pomoc->next = nowy;
cout << "TAK" << endl;
}
}
else {
//jak nowe->lf mniejsze:
if (pomoc == first) {
nowy->next = first;
first = nowy;
cout << "TAK" << endl;
}
else{
nowy->next = pomoc;
pomoc2->next = nowy;
cout << "TAK" << endl;
}
}
}
}
void Lista::find_aktor(int p){
Aktor *pomoc = first, *pomoc2 = NULL, *pom, *pom2, *out;
bool tmp = true;
if (pomoc == NULL){
cout << "NIE" << endl;
}
else{
while ((pomoc != NULL )) {
if (p <= pomoc->lf){
pom = pomoc;
tmp = true;
break;
}
else{
tmp = false;
}
if (pomoc->next == NULL || pomoc->next->lf > p) {
pom2 = pomoc2;
}
pomoc2 = pomoc;
pomoc = pomoc->next;
}
if (tmp == true){
if (pom->lf == p) {
if (pom == first) {
cout << pom->nazwisko << endl;
first = first->next;
}
else if (pomoc->next == NULL) {
//koniec listy:
cout << pom->nazwisko << endl;
pomoc2->next = NULL;
}
else {
//w srodku:
cout << pom->nazwisko << endl;
pomoc2->next = pom->next;
}
}
else if (pomoc->lf < p) {
cout << pomoc2->nazwisko << endl;
if (pomoc == first) {
pomoc->next = NULL;
first = NULL;
}
else {
pomoc2->next = NULL;
}
}
else if (p < pom->lf){
if (pom == first) {
cout << "NIE" << endl;
}
else {
cout << pomoc2->nazwisko << endl;
if (pomoc2 == first) {
first = pomoc;
}
else {
pom2->next = pomoc;
}
}
}
}
else if (pomoc2->lf < p) {
cout << pomoc2->nazwisko << endl;
if (pomoc2 == first) {
pomoc2->next = NULL;
first = NULL;
}
else {
pom2->next = NULL;
}
}
else{
cout << "NIE" << endl;
}
}
}
Aktor * Lista::AktorSearch(int lfs) {
Aktor *x = first;
while (x != NULL && x->lf != lfs) {
x = x->next;
}
return x;
}
int main() {
int n; // Liczba operacji do wykonania.
int op; // Rodzaj operacji do wykonania.
int p; // Maksymalna liczba fanów, na której stratę można sobie pozwolić.
int lf;
string nazwisko;
cin >> n;
Lista *lista = new Lista;
for (int i = 0; i < n; i++) {
cin >> op; // Wczytanie rodzaju operacji.
if (op == 1){
cin.ignore();
getline(cin, nazwisko);
cin >> lf;
lista->insert_aktor(nazwisko, lf);
}
if (op == 2){ // Wyszukanie aktora do roli.
cin.ignore();
cin >> p; // Podaj liczbę fanów, na której stratę możesz sobie pozwolić.
lista->find_aktor(p);
}
}
/*lista->insert_aktor("Jurek", 200);
lista->insert_aktor("antek", 100);
lista->insert_aktor("tomek", 400);
lista->insert_aktor("zenek", 200);
lista->insert_aktor("juntek", 300);
lista->insert_aktor("marek", 600);
lista->find_aktor(100);
lista->insert_aktor("jozin", 100);
cout << lista->first->nazwisko << endl;
cout << lista->first->next->nazwisko << endl;
cout << lista->first->next->next->nazwisko << endl;
cout << lista->first->next->next->next->nazwisko << endl;
cout << lista->first->next->next->next->next->nazwisko << endl;*/
//system("pause");
return 0;
}