Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <fstream>
- #include<windows.h>
- using namespace std;
- struct Lista
- {
- int key;
- double zmiennaDouble;
- char zmiennaChar;
- Lista* nastepny = nullptr;
- };
- void ini(Lista*& wsk)
- {
- wsk = nullptr;
- }
- int addLista(Lista*& wsk, int klucz);
- void deleteLista(Lista*& wsk);
- void deleteElement(Lista*& wsk, int klucz);
- void dodaj(Lista*& wsk, int ile);
- void wyszukiwanie(Lista*& start, int klucz);
- int ileelementow(Lista*& wsk);
- void prezent_Y(Lista*& head, int ile);
- void prezent_Z(Lista*& head, int ile);
- int main()
- {
- Lista* start;
- start = nullptr;
- int x;
- int X, k1, k2, k3, k4, k5;
- double time_spent;
- ifstream we("inlab02.txt");
- if (!we) return -1;
- we >> X >> k1 >> k2 >> k3 >> k4 >> k5;
- we.close();
- srand(time(NULL));
- int startt = GetTickCount(); //--pomiar czasu początek
- ini(start);
- cout << "Wyszukuje klucz nr 1" << endl; wyszukiwanie(start, k1); cout << endl;
- cout << "Wstawienie X elementow do listy" << endl; dodaj(start, X); cout << endl;
- cout << "Liczba wezlow w liscie" << endl; ileelementow(start); cout << endl;
- cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
- cout << "Wstawianie elementu k2" << endl; addLista(start, k2);
- cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
- cout << "Wstawianie elementu k3" << endl; addLista(start, k3);
- cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
- cout << "Wstawianie elementu k4" << endl; addLista(start, k4);
- cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
- cout << "Wstawienie elementu k5 i usuniecie k3" << endl; addLista(start, k5); deleteElement(start, k3);
- cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
- cout << "Usuniecie elementu k2" << endl; deleteElement(start, k2);
- cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
- cout << "Usuniecie elementu k5" << endl; deleteElement(start, k5); cout << endl;
- cout << "Liczba wezlow w liscie" << endl; ileelementow(start); cout << endl;
- cout << "Wyszukuje klucz nr 5" << endl; wyszukiwanie(start, k5); cout << endl;
- cout << "Prezentacja OSTATNICH 11 elementow w liscie: "; prezent_Z(start, 11); cout << endl;
- cout << "Liczba wezlow w liscie" << endl; ileelementow(start); cout << endl;
- cout << "Usuwam wszystkie elementy listy" << endl; deleteLista(start); cout << endl;
- int koniec = GetTickCount(); //-pomiar czasu koniec
- cout << "Czas wykonania programu w sekundach: ";
- cout << (koniec - startt) * 0.001 << endl;
- }
- int addLista(Lista*& start, int klucz)
- {
- Lista* x;
- x = nullptr;
- if (start != nullptr)
- {
- Lista* wsk;
- wsk = start;
- if (wsk->key > klucz)
- {
- x = new Lista;
- x->key = klucz;
- x->zmiennaChar = 'T';
- x->zmiennaDouble = rand() % 1000;
- x->nastepny = start;
- start = x;
- return 0;
- }
- else {
- while (wsk->nastepny != nullptr)
- {
- if (wsk->key == klucz)
- {
- //cout << "Blad: Klucz o tej wartosci juz istnieje." << endl;
- return -1;
- }
- if (wsk->key < klucz && wsk->nastepny->key > klucz)
- {
- break;
- }
- wsk = wsk->nastepny;
- };
- }
- x = new Lista;
- x->key = klucz;
- x->zmiennaChar = 'T';
- x->zmiennaDouble = 99 + rand() % 1000;
- x->nastepny = wsk->nastepny;
- wsk->nastepny = x;
- return 0;
- }
- else
- {
- x = new Lista;
- x->key = klucz;
- x->zmiennaChar = 'T';
- x->zmiennaDouble = 99 + rand() % 1000;
- start = x;
- return 0;
- }
- };
- void wyszukiwanie(Lista*& start, int klucz)
- {
- if (start != nullptr)
- {
- Lista* x;
- x = start;
- if (klucz < start->key) cout << "NIE ISTNIEJEe." << endl;
- for (;;)
- {
- if (x->key == klucz)
- {
- cout << "Szukany wezel to :" << x->key << x->zmiennaChar << " " << x->zmiennaDouble << endl;
- break;
- }
- if (x->nastepny == nullptr)
- {
- cout << "ELEMENT NIE ISTNIEJE " << endl;
- break;
- }
- x = x->nastepny;
- }
- }
- else cout << "Lista jest pusta." << endl;
- }
- void deleteLista(Lista*& start)
- {
- Lista* wsk, * do_usuniecia;
- wsk = start;
- while (wsk != nullptr)
- {
- do_usuniecia = wsk;
- wsk = wsk->nastepny;
- delete do_usuniecia;
- };
- start = nullptr;
- };
- void deleteElement(Lista*& start, int klucz)
- {
- if (start != nullptr)
- {
- Lista* x, * x2 = nullptr;
- if (start->key == klucz)
- {
- x = start->nastepny;
- delete start;
- start = x;
- }
- else
- {
- x = start;
- while (x)
- {
- if (x->key == klucz)
- {
- x2->nastepny = x->nastepny;
- delete x;
- break;
- }
- if (x->key < klucz && x->nastepny->key > klucz)
- {
- cout << "NIE MA TAKIEGO KLUCZA";
- return;
- }
- x2 = x;
- x = x->nastepny;
- };
- }
- }
- else cout << "Lista jest pusta." << endl;
- }
- void dodaj(Lista*& start, int ile)
- {
- int klucz = 0, error;
- for (int i = 0; i < ile; i++)
- {
- klucz = 99 + rand() % 100003;
- error = addLista(start, klucz);
- if (error == -1)
- i--;
- }
- }
- int ileelementow(Lista*& start)
- {
- Lista* wsk = start;
- int i = 0;
- while (wsk != nullptr)
- {
- i++;
- wsk = wsk->nastepny;
- }
- cout << i << endl;
- return i;
- }
- void prezent_Y(Lista*& head, int ile)
- {
- if (head != nullptr)
- {
- Lista* tmp;
- tmp = head;
- for (int i = 0; i < ile; i++)
- {
- cout << tmp->key << " ";
- tmp = tmp->nastepny;
- }
- cout << endl;
- }
- else cout << "Lista jest pusta." << endl;
- }
- void prezent_Z(Lista*& head, int ile)
- {
- int* tab = new int[ile];
- if (head != nullptr)
- {
- Lista* tmp = head;
- int s = ileelementow(head);
- for (int i = 0; i < ile; i++) {
- for (int j = 1; j <= s - i ; j++) {
- if (j == s - i) {
- cout << tmp->key << "| ";
- }
- else tmp = tmp->nastepny;
- }
- tmp = head;
- }
- }
- else cout << "Lista jest pusta." << endl;
- delete[] tab;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement