Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Strony{
- public:
- int strona;
- Strony* pierwsza;
- Strony* nastepna;
- Strony();
- void dodajStrony(int);
- void wyswietlStrony();
- bool isEmpty();
- void usunStrony(int);
- bool wyszukajStrone(int);
- };
- Strony::Strony(){
- pierwsza=NULL;
- nastepna = NULL;
- }
- void Strony::wyswietlStrony(){
- Strony *tmp = pierwsza;
- while(tmp!=NULL){
- cout<<tmp->strona<<" ";
- tmp=tmp->nastepna;
- }
- cout << endl;
- }
- bool Strony::isEmpty(){
- Strony *temp = pierwsza;
- if(temp==NULL)
- return true;
- else
- return false;
- }
- bool Strony::wyszukajStrone(int strona){
- Strony *aktWartosc = NULL;
- aktWartosc = pierwsza;
- bool czyZnaleziono = false;
- while(aktWartosc != NULL){
- if(aktWartosc->strona == strona){
- czyZnaleziono = true;
- }
- if(czyZnaleziono==true){
- break;
- }
- aktWartosc = aktWartosc->nastepna;
- }
- if(czyZnaleziono==false)
- return false;
- else
- return true;
- }
- void Strony::usunStrony(int value){
- Strony *aktWartosc = NULL;
- Strony *wczWartosc = NULL;
- aktWartosc = pierwsza;
- wczWartosc = pierwsza;
- while(aktWartosc != NULL && aktWartosc->strona != value){
- wczWartosc = aktWartosc;
- aktWartosc = aktWartosc->nastepna;
- }
- if(aktWartosc==NULL){}
- else{
- if(aktWartosc->strona == value){
- if(aktWartosc == pierwsza){
- pierwsza = aktWartosc->nastepna;
- }
- else if(aktWartosc->nastepna == NULL && aktWartosc->strona==value){
- wczWartosc->nastepna=NULL;
- }
- else
- wczWartosc->nastepna=aktWartosc->nastepna;
- }
- }
- }
- void Strony::dodajStrony(int strona){
- if(!pierwsza){
- Strony *nowa = new Strony;
- nowa->strona=strona;
- pierwsza=nowa;
- }
- else if(pierwsza->strona>strona){
- Strony *nowa = new Strony;
- nowa->strona=strona;
- nowa->nastepna=pierwsza;
- pierwsza=nowa;
- }
- else{
- Strony* temp = pierwsza;
- Strony* temp2 = temp;
- Strony* nowa = new Strony;
- while(temp){
- if(temp->strona == strona){
- break;
- }
- else if(temp->strona > strona){
- temp2->nastepna = nowa;
- nowa->nastepna=temp;
- nowa->strona = strona;
- break;
- }
- else if(temp->nastepna == NULL){
- temp->nastepna = nowa;
- nowa->strona = strona;
- break;
- }
- temp2 = temp;
- temp = temp->nastepna;
- }
- }
- }
- class Etykiety
- {
- public:
- string etykieta;
- Etykiety *pierwsza;
- Etykiety *nastepnaetykieta;
- Strony *poczatekstron;
- Etykiety();
- void dodajEtykiete(string);
- void wyswietlListe();
- void usunStr(string);
- void usunEtykiete(string);
- bool wyszukajEtykiete(string,int);
- void wyszukajAll(string);
- };
- Etykiety::Etykiety(){
- pierwsza=NULL;
- poczatekstron = new Strony;
- nastepnaetykieta = NULL;
- }
- void Etykiety::dodajEtykiete(string etykieta){
- if(!pierwsza){
- Etykiety *nowa = new Etykiety;
- nowa->etykieta=etykieta;
- nowa->poczatekstron = new Strony;
- int liczbastron;
- cin>>liczbastron;
- int strona;
- for(int i=0; i<liczbastron; i++){
- cin>>strona;
- if(strona<=1000)
- nowa->poczatekstron->dodajStrony(strona);
- }
- pierwsza=nowa;
- }
- else{
- Etykiety* temp = pierwsza;
- Etykiety* temp2 = temp;
- Etykiety* nowa = new Etykiety;
- while(temp){
- if(temp->etykieta == etykieta){
- int liczbastron;
- cin>>liczbastron;
- int strona;
- for(int i=0; i<liczbastron; i++){
- cin>>strona;
- if(strona<=1000)
- temp->poczatekstron->dodajStrony(strona);
- }
- break;
- }
- else if(pierwsza->etykieta > etykieta){
- nowa->nastepnaetykieta = pierwsza;
- nowa->etykieta = etykieta;
- nowa->poczatekstron = new Strony;
- int liczbastron;
- cin>>liczbastron;
- int strona;
- for(int i=0; i<liczbastron; i++){
- cin>>strona;
- if(strona<=1000)
- nowa->poczatekstron->dodajStrony(strona);
- }
- pierwsza=nowa;
- break;
- }
- else if(temp->etykieta > etykieta && etykieta > temp2->etykieta){
- temp2->nastepnaetykieta = nowa;
- nowa->nastepnaetykieta=temp;
- nowa->etykieta = etykieta;
- nowa->poczatekstron = new Strony;
- int liczbastron;
- cin>>liczbastron;
- for(int i=0; i<liczbastron; i++){
- int strona;
- cin>>strona;
- if(strona<=1000)
- nowa->poczatekstron->dodajStrony(strona);
- }
- break;
- }
- else if(temp->nastepnaetykieta == NULL){
- temp->nastepnaetykieta = nowa;
- nowa->etykieta = etykieta;
- nowa->poczatekstron = new Strony;
- int liczbastron;
- cin>>liczbastron;
- int strona;
- for(int i=0; i<liczbastron; i++){
- cin>>strona;
- if(strona<=1000)
- nowa->poczatekstron->dodajStrony(strona);
- }
- break;
- }
- temp2 = temp;
- temp = temp->nastepnaetykieta;
- }
- }
- }
- void Etykiety::wyswietlListe(){
- Etykiety *tmp = pierwsza;
- while(tmp!=NULL){
- cout<<tmp->etykieta<<" ";
- tmp->poczatekstron->wyswietlStrony();
- tmp=tmp->nastepnaetykieta;
- }
- }
- void Etykiety::usunStr(string etyk){
- Etykiety* temp = NULL;
- temp = pierwsza;
- while(temp != NULL){
- if(temp->etykieta==etyk){
- int liczbastron;
- cin>>liczbastron;
- int strona;
- for(int i=0; i<liczbastron; i++){
- cin>>strona;
- temp->poczatekstron->usunStrony(strona);
- }
- if(temp->poczatekstron->isEmpty()==1){
- usunEtykiete(temp->etykieta);
- break;
- }
- }
- temp = temp->nastepnaetykieta;
- }
- }
- void Etykiety::usunEtykiete(string etykieta){
- Etykiety *aktWartosc = NULL;
- Etykiety *wczWartosc = NULL;
- aktWartosc = pierwsza;
- wczWartosc = pierwsza;
- while(aktWartosc != NULL && aktWartosc->etykieta != etykieta){
- wczWartosc = aktWartosc;
- aktWartosc = aktWartosc->nastepnaetykieta;
- }
- if(aktWartosc->etykieta == etykieta){
- if(aktWartosc == pierwsza){
- pierwsza = aktWartosc->nastepnaetykieta;
- }
- else if(aktWartosc->nastepnaetykieta == NULL && aktWartosc->etykieta==etykieta){
- wczWartosc->nastepnaetykieta=NULL;
- }
- else
- wczWartosc->nastepnaetykieta=aktWartosc->nastepnaetykieta;
- }
- }
- bool Etykiety::wyszukajEtykiete(string etykieta, int strona)
- {
- bool flagExist = false;
- Etykiety *aktWartosc = NULL;
- aktWartosc = pierwsza;
- while(aktWartosc != NULL)
- {
- if(aktWartosc->etykieta == etykieta)
- {
- if(aktWartosc->poczatekstron->wyszukajStrone(strona)==true)
- return true;
- else
- return false;
- }else
- aktWartosc = aktWartosc->nastepnaetykieta;
- }
- return false;
- }
- void Etykiety::wyszukajAll(string etykieta) {
- Etykiety *aktWartosc = NULL;
- aktWartosc = pierwsza;
- bool czyZnaleziono = false;
- while(aktWartosc != NULL){
- if(aktWartosc->etykieta == etykieta){
- cout << etykieta << " ";
- aktWartosc->poczatekstron->wyswietlStrony();
- czyZnaleziono=true;
- }
- if(czyZnaleziono==true)
- break;
- aktWartosc = aktWartosc->nastepnaetykieta;
- }
- if(czyZnaleziono==false)
- cout << "-1" << endl;
- }
- int main()
- {
- Etykiety *lista = new Etykiety();
- int ile_operacji;
- char polecenie;
- cin >> ile_operacji;
- for(int i=0; i<ile_operacji; i++)
- {
- cin >> polecenie;
- switch (polecenie){
- case 'I':{
- string etykieta;
- cin>>etykieta;
- lista->dodajEtykiete(etykieta);
- break;
- }
- case 'D':{
- string etykieta;
- cin>>etykieta;
- lista->usunStr(etykieta);
- break;
- }
- case 'F':{
- string etykieta;
- int strona;
- cin>>etykieta;
- cin>>strona;
- cout << lista->wyszukajEtykiete(etykieta,strona)<<endl;
- break;
- }
- case 'A':{
- string etykieta;
- cin >> etykieta;
- lista->wyszukajAll(etykieta);
- break;
- }
- case 'P':{
- lista->wyswietlListe();
- break;
- }
- }
- }
- delete lista;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement