Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include "ListaJedno.cpp"
- using namespace std;
- int main()
- {
- ListaJedno* myList = new ListaJedno;
- myList->show();
- myList->add(10,10);
- myList->show();
- myList->add(20,0);
- myList->add(30,1);
- myList->add(40,1);
- myList->add(50,4);
- myList->add(60,100);
- myList->show();
- myList->rmv(0);
- myList->rmv(8);
- myList->rmv(1);
- myList->rmv(1);
- myList->rmv(1);
- myList->rmv(1);
- myList->rmv(1);
- myList->show();
- myList->clean();
- return 0;
- }
- //////////////////////////////////////
- PLIK Z LISTA JEDNOKIERUNKOWA
- //////////////////////////////////////
- //////////////////////////////////////
- #include <iostream>
- #include <string>
- using namespace std;
- class ElementL
- {
- public:
- int dane;
- ElementL* next = NULL;
- ElementL(int dane)
- {
- this->dane = dane;
- this->next = NULL;
- }
- };
- class ListaJedno
- {
- public:
- ElementL* pierwszy = NULL;
- ElementL* ostatni = NULL;
- ElementL* getPosition(unsigned long long int pos)
- {
- if(pos < 0 || this->sizee() < pos) return NULL;
- ElementL* temp = this->pierwszy;
- unsigned long long int i = 1;
- while(i++ < pos)
- {
- temp = temp->next;
- }
- return temp;
- }
- void add(int d, unsigned long long int pos)
- {
- ElementL* n = new ElementL(d);
- if(this->sizee() == 0)
- {
- this->pierwszy = n;
- this->ostatni = n;
- }
- else if(pos == 1 || pos == 0)
- {
- n->next = this->pierwszy;
- this->pierwszy = n;
- }
- else if(pos > this->sizee())
- {
- this->ostatni->next = n;
- this->ostatni = n;
- }
- else
- {
- ElementL* temp = getPosition(pos - 1);
- n->next = temp->next;
- temp->next = n;
- }
- }
- void show()
- {
- if(this->pierwszy == NULL)
- {
- cout << "BRAK ELEMENTOW" << endl;
- return;
- }
- if(this->sizee() == 1)
- {
- cout << this->pierwszy->dane << endl;
- return;
- }
- ElementL* temp = this->pierwszy;
- while(temp != NULL)
- {
- cout << temp->dane << endl;
- temp = temp->next;
- }
- }
- bool rmv(unsigned long long int pos)
- {
- if(this->sizee() == 0) return false;
- if(pos == 0 || pos > this->sizee())
- {
- cout << "PODANO ZLA POZYCJE" << endl;
- return false;
- }
- if(pos == 1)
- {
- ElementL* temp = this->pierwszy;
- this->pierwszy = this->pierwszy->next;
- delete temp;
- }
- else if(pos == sizee())
- {
- ElementL* temp = this->getPosition(pos - 1);
- delete temp->next;
- temp->next = NULL;
- this->ostatni = temp;
- }
- else
- {
- ElementL* temp = this->getPosition(pos - 1);
- ElementL* temp1 = this->getPosition(pos);
- temp->next = temp1->next;
- delete temp1;
- }
- return true;
- }
- unsigned long long int sizee()
- {
- ElementL* temp = this->pierwszy;
- int i = 0;
- while(temp != NULL)
- {
- temp = temp->next;
- i+=1;
- }
- return i;
- }
- void clean()
- {
- ElementL* temp = this->pierwszy;
- while(temp != NULL)
- {
- delete temp;
- temp = temp->next;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement