Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- //**************************************************************************************************
- class Kontenerek //Tworze kontenerek który będzie częscią mojej listy
- {
- public:
- int wartocsc;
- Kontenerek *nastepny;
- Kontenerek *poprzedni;
- Kontenerek();
- };
- Kontenerek::Kontenerek()
- {
- nastepny = 0;
- poprzedni = 0;
- }
- //**************************************************************************************************
- class Lista
- {
- public:
- Kontenerek *pierwszy;
- void dodajElment(int wartosc);
- void usunElement(int ktory);
- Lista();//Konstruktor
- };
- Lista::Lista()
- {
- pierwszy = 0;
- }
- void Lista::dodajElment(int wartosc)
- {
- Kontenerek *nowaLiczba = new Kontenerek;
- nowaLiczba->wartocsc = wartosc;
- if (pierwszy == 0)
- {
- pierwszy = nowaLiczba;
- }
- else
- {
- Kontenerek *tempo = pierwszy;
- while (tempo->nastepny != 0)
- {
- tempo = tempo->nastepny;
- }
- tempo->nastepny = nowaLiczba;
- nowaLiczba->poprzedni = tempo;
- }
- }
- void Lista::usunElement(int ktory)
- {
- if (pierwszy->wartocsc == ktory)
- {
- pierwszy = pierwszy->nastepny;
- pierwszy->poprzedni = 0;
- }
- else
- {
- Kontenerek *temp = pierwszy;
- while (temp != 0)
- {
- if (ktory == temp->nastepny->wartocsc)
- {
- break;
- }
- temp = temp->nastepny;
- }
- if (temp->nastepny->nastepny == 0)
- {
- temp->nastepny = 0;
- }
- else
- {
- temp->nastepny = temp->nastepny->nastepny;
- temp->nastepny->poprzedni = temp;
- }
- }
- }
- //**************************************************************************************************
- int main()
- {
- Lista *listaLiczb = new Lista;
- listaLiczb->dodajElment(12);
- listaLiczb->dodajElment(14);
- listaLiczb->dodajElment(11);
- listaLiczb->dodajElment(21);
- listaLiczb->dodajElment(18);
- cout << listaLiczb->pierwszy->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->nastepny->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->nastepny->wartocsc << endl;
- listaLiczb->usunElement(12);
- cout << endl;
- cout << listaLiczb->pierwszy->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->nastepny->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->wartocsc << endl;
- cout << endl;
- cout << listaLiczb->pierwszy->nastepny->poprzedni->wartocsc << endl;
- cout << listaLiczb->pierwszy->nastepny->nastepny->poprzedni->wartocsc << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment