Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdio.h>
- using namespace std;
- struct element {
- int data;
- element* next;
- };
- struct lista {
- element* head;
- element* tail;
- int el_count;
- };
- lista create_list ();
- void add_tail(lista &l, int r_d);
- void add_head(lista &l, int r_d);
- void add_position(lista &l, int r_d, int posit);
- void delete_tail(lista &l);
- void delete_head(lista &l);
- void delete_position(lista &l, int posit);
- void delete_list(lista &l);
- void show_list(lista l);
- void show_on_position(lista l, int posit);
- int main()
- {
- lista l = create_list();
- int wybor, posit;
- cout << "Podaj pozycje" << endl;
- cin >> posit;
- srand(time(NULL));
- do{
- cout << "Podaj wybor:" << endl;
- cout << "1. Dodawanie elementu o losowej wartosci na koniec listy " << endl;
- cout << "2. Dodawanie elementu o losowej wartosci na poczatek listy " << endl;
- cout << "3. Dodawanie elementu o losowej wartosci na pozycje o podanym numerze " << endl;
- cout << "4. Usuniecie elementu z konca listy " << endl;
- cout << "5. Usuniecie elementu z poczatku listy" << endl;
- cout << "6. Usuniecie elementu z podanej pozycji" << endl;
- cout << "7. Wyswietlenie elementu o podanym numerze" << endl;
- cout << "8. Wyswietlenie calej listy" << endl;
- cout << "9. Usuniecie wszystkich elementow listy wraz ze zwolnieniem pamieci" << endl;
- cout << "10. Wyjscie" << endl;
- cout << "Odpowiedz:" << endl;
- cin >> wybor;
- switch(wybor){
- case 1: add_tail(l, rand()); break;
- case 2: add_head(l, rand()); break;
- case 3: add_position(l, rand(), posit); break;
- case 4: delete_tail(l); break;
- case 5: delete_head(l); break;
- case 6: delete_position(l, posit); break;
- case 7: show_on_position(l, posit); break;
- case 8: show_list(l); break;
- case 9: delete_list(l); break;
- case 10: cout << "Wyszedles z progrmau" << endl; break;
- default : cout << "bledny wybor" << endl; break;
- }
- } while (wybor != 10);
- return 0;
- }
- lista create_list (){
- lista new_list;
- new_list.head = nullptr;
- new_list.tail = nullptr;
- new_list.el_count = 0;
- return new_list;}
- void add_tail(lista &l, int r_d){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- cout << "Numer ktory zostanie dodany: " << r_d << endl;
- element* n_tail = new element;
- n_tail -> data = r_d;
- n_tail -> next = nullptr;
- if(l.tail != nullptr)
- l.tail -> next = n_tail;
- else
- l.head = n_tail;
- l.tail = n_tail;
- l.el_count ++;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void add_head(lista &l, int r_d){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- cout << "Numer ktory zostanie dodany: " << r_d << endl;
- element* n_head = new element;
- n_head -> data = r_d;
- n_head -> next = l.head;
- l.head = n_head;
- if(l.tail == nullptr)
- l.tail == n_head;
- l.el_count ++;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void add_position(lista &l, int r_d, int posit){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- cout << "Numer ktory zostanie dodany: " << r_d << endl;
- if(posit <0 or posit > l.el_count) return;
- if(posit == 0)
- {
- add_head(l, r_d);
- return;
- }
- if(posit == l.el_count)
- {
- add_tail(l, r_d);
- return;
- }
- element* temp = l.head;
- for(int i=0; i < posit -1; i++)
- temp = temp -> next;
- element* n_e = new element;
- n_e -> data = r_d;
- n_e -> next = temp -> next;
- temp -> next = n_e;
- l.el_count ++;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_tail(lista &l){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- if(l.tail == nullptr) return;
- element* temp = l.tail;
- if(l.el_count == 1)
- {
- l.tail == nullptr;
- l.head == nullptr;
- }
- else{
- element * tempor = l.head;
- for (int i =1; i< l.el_count -1; i++)
- {
- tempor = tempor -> next;
- }
- l.tail = tempor;
- l.tail -> next = nullptr;
- }
- l.el_count --;
- delete temp;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_head(lista &l){
- if(l.tail == nullptr) return;
- cout << "Lista biezaca:" << endl;
- show_list(l);
- element* temp = l.head;
- l.head = l.head -> next;
- delete temp;
- if(l.head == nullptr)
- l.tail = nullptr;
- cout << "Element z poczatku zostal usuniety" << endl;
- l.el_count--;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_position(lista &l, int posit){
- if (posit < 0 or posit > l.el_count)
- {
- cout << "Podano niewlasciwa pozycje" << endl;
- return;
- }
- cout << "Lista biezaca:" << endl;
- show_list(l);
- if (posit == 0)
- {
- delete_head(l);
- return;
- }
- if(posit == l.el_count)
- {
- delete_tail(l);
- return;
- }
- element* temp = l.head;
- for (int i =1; i< l.el_count -1; i++)
- {
- temp = temp -> next;
- }
- element* temp_delete = temp -> next;
- temp -> next = temp_delete -> next;
- delete temp_delete;
- l.el_count --;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_list(lista &l){
- while(l.head != nullptr){
- delete_head(l);
- }
- }
- void show_list(lista l){
- element* temp = l.head;
- while (temp)
- {
- cout << temp -> data << endl;
- temp = temp -> next;
- l.el_count --;
- }
- }
- void show_on_position(lista l, int posit)
- {
- int c = 0;
- if (posit < 0 or posit > l.el_count)
- {
- cout << "Podano niewlasciwa pozycje" << endl;
- return;
- }
- element* temp = l.head;
- while(l.el_count != 0){
- c++;
- if (c == posit) cout << temp -> data << endl;
- temp = temp -> next;
- l.el_count --;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement