Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- class List;
- class ListNode
- {
- int x;
- double y;
- char z;
- ListNode* next;
- public:
- ListNode(int); // <---- konstruktor!!!!!
- void display();
- friend class List; // <----- klasa List ma dostęp do wszystkich składników prywatnych klasy ListNode
- };
- class List
- {
- int size;
- ListNode* head; // pierwszy element listy
- public:
- List();
- ~List(); // <----- destruktor
- void addNode(int);
- void fillGoodInCorporation(int);
- ListNode* searchNode(int);
- void deleteNode(int);
- void showFirstNodes(int);
- void showLastNodes(int);
- void showNodes();
- void clear();
- int getSize();
- };
- // ciało konstruktora
- ListNode::ListNode(int x)
- {
- this->x = x;
- this->y = 1 + rand() % 100;
- this->z = 'T';
- this->next = NULL;
- }
- void ListNode::display() //wyswietlanie konkretnego ListNoda
- {
- cout << x << " " << y << " " << z << endl;
- }
- List::List()
- {
- this->size = 0;
- this->head = NULL;
- }
- List::~List()
- {
- this->clear();
- }
- void List::addNode(int x)
- {
- if (head == NULL) {
- this->head = new ListNode(x);
- this->size++;
- return;
- }
- // Lecimy po liście, dopóki nie natrafimy na ostatni element
- ListNode* temp = head;
- ListNode* next = temp->next;
- // Czy dodawany element jest mniejszy od pierwszego
- // W tym wypadku nowy element staje się pierwszym
- if (temp->x > x) {
- ListNode* newNode = new ListNode(x);
- newNode->next = temp;
- head = newNode;
- this->size++;
- return;
- }
- while (next) {
- // Jeżeli dodajemy elemnt większy od ogona, to ten warunek nigdy nie zajdzie
- // Jeżeli tak nie jest, to pętla zatrzyma się gdzieś w środku
- if (next->x > x) {
- break;
- }
- temp = temp->next;
- next = temp->next;
- }
- // aktualizacj listy
- ListNode* newNode = new ListNode(x);
- newNode->next = next;
- temp->next = newNode;
- this->size++;
- }
- void List::fillGoodInCorporation(int X)
- {
- for (int i = 0; i < X; i++) {
- this->addNode(99 + rand() % 99001);
- }
- }
- ListNode* List::searchNode(int x)
- {
- ListNode* temp = head;
- while (temp) {
- if (temp->x == x) {
- break;
- }
- temp = temp->next;
- }
- return temp;
- }
- void List::deleteNode(int x)
- {
- if (head == NULL) {
- cout << "List is empty!" << endl;
- return;
- }
- ListNode* temp = head;
- // jeśli lista ma tylko jeden element, to usuwamy head, setujemy na nulla, zerujemy size i tyle
- if (head->x == x) {
- head = temp->next;
- delete temp;
- this->size--;
- return;
- }
- //potrzeba informacji o poprzednim elemencie by posklejac liste
- ListNode* prev = temp;
- temp = temp->next;
- while (temp) {
- if (temp->x == x) {
- break;
- }
- prev = temp;
- temp = temp->next;
- }
- if (temp == NULL) {
- cout << "Node not found!" << endl;
- return;
- }
- prev->next = temp->next;
- delete temp;
- this->size--;
- }
- void List::showFirstNodes(int Y) {
- ListNode* temp = head;
- for (int i = 0; i < Y; i++) {
- if (temp == NULL) {
- break;
- }
- cout << i + 1 << ": ";
- temp->display();
- temp = temp->next;
- }
- cout << endl;
- }
- void List::showLastNodes(int Z) {
- ListNode* temp = head;
- if (this->size < Z) {
- this->showNodes();
- return;
- }
- // obliczamy liczbę elemntów z przodu
- int firstSize = size - Z;
- // przesuwamy się na Z-ty od końca elemnt
- for (int i = 0; i < firstSize; i++) {
- temp = temp->next;
- }
- // wyświetlamy Z ostatnich elemntów
- for (int i = 0; i < Z; i++) {
- cout << i + 1 << ": ";
- temp->display();
- temp = temp->next;
- }
- cout << endl;
- }
- void List::showNodes()
- {
- ListNode* temp = head;
- int i = 0;
- while (temp) {
- cout << i++ + 1 << ": ";
- temp->display();
- temp = temp->next;
- }
- cout << endl;
- }
- void List::clear()
- {
- ListNode* temp = head;
- while (temp) {
- ListNode* next = temp->next;
- delete temp;
- temp = next;
- }
- this->head = NULL;
- this->size = 0;
- }
- int List::getSize()
- {
- return this->size;
- }
- int main()
- {
- srand((unsigned) time(NULL));
- // File handling bitch
- FILE* file = fopen("inlab02.txt", "r");
- if (!file) {
- cerr << "File not found!!" << endl << endl;
- exit(EXIT_FAILURE);
- }
- int X, k1, k2, k3, k4, k5;
- fscanf(file, "%d %d %d %d %d %d", &X, &k1, &k2, &k3, &k4, &k5);
- fclose(file);
- clock_t begin, end;
- double time_spent;
- begin = clock();
- List* list = new List();
- list->searchNode(k1);
- list->fillGoodInCorporation(X);
- cout << list->getSize() << endl << endl;
- list->showFirstNodes(20);
- list->addNode(k2);
- list->showFirstNodes(20);
- list->addNode(k3);
- list->showFirstNodes(20);
- list->addNode(k4);
- list->showFirstNodes(20);
- list->addNode(k5);
- list->deleteNode(k3);
- list->showFirstNodes(20);
- list->deleteNode(k2);
- list->showFirstNodes(20);
- list->deleteNode(k5);
- cout << list->getSize() << endl << endl;
- list->searchNode(k5);
- list->showLastNodes(11);
- cout << list->getSize() << endl << endl;
- list->clear();
- end = clock();
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- delete list;
- cout << "Total time: " << time_spent << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement