Last active
December 22, 2015 16:19
-
-
Save pstachula-dev/6498527 to your computer and use it in GitHub Desktop.
SDiZO, lab2a (lista cykliczna)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <time.h> | |
| // Lista cykliczna | |
| using namespace std; | |
| struct list_el | |
| { | |
| int key; | |
| list_el *next; | |
| }; | |
| int add(list_el *&head, int value) | |
| { | |
| if(head == NULL){ | |
| head = new list_el; | |
| head->key = value; | |
| head->next = head; | |
| // Przypadek w ktorym lista jest pusta | |
| return 1; | |
| } | |
| if(value < head->key){ | |
| list_el *last_el = head; | |
| while(last_el->next != head) | |
| last_el = last_el->next; | |
| // Wyszukuje ostatni element, aby umiescic tam wskaznik na "glowe" | |
| list_el *tmp = head; | |
| head = new list_el; | |
| head->key = value; | |
| head->next = tmp; | |
| last_el->next = head; | |
| return 1; | |
| } | |
| if(value == head->key) | |
| return 0; | |
| list_el *curr = head; | |
| while(curr->next != head && curr->next->key < value) | |
| curr = curr->next; | |
| if(curr->next == head){ | |
| // Przypadek gdy nowy element jest na koncu listy | |
| curr->next = new list_el; | |
| curr->next->key = value; | |
| curr->next->next = head; | |
| // Laduje do wskaznika wskaznik na "glowe" listy | |
| return 1; | |
| } else { | |
| // Przypadek gdy nowy element jest w srodku listy | |
| if(value == curr->next->key) | |
| return 0; | |
| list_el* tmp = curr->next; | |
| curr->next = new list_el; | |
| curr->next->key = value; | |
| curr->next->next = tmp; | |
| return 1; | |
| } | |
| } | |
| void randomElements(list_el *&head) | |
| { | |
| int size; | |
| cout << "Ile elementow wylosowac? "; | |
| cin >> size; | |
| for(int i = 0; i < size; ) | |
| if(add(head, rand() % 20)) | |
| i++; | |
| } | |
| void printAll(list_el *head) | |
| { | |
| if(!head){ | |
| cout << "Lista pusta\n"; | |
| return; | |
| } | |
| list_el *curr = head; | |
| while(curr->next != head){ | |
| cout << curr->key << " "; | |
| curr = curr->next; | |
| } | |
| cout << curr->key << endl; | |
| // Wyswietlam ostatni element listy, gdyz warunek nie pozwala wyswietlic ostatniego | |
| } | |
| void searchElement(list_el *head) | |
| { | |
| int value, x = 0; | |
| cout << "Podaj wartosc: "; | |
| cin >> value; | |
| list_el *tmp = head; | |
| if(!tmp){ | |
| cout << "Lista pusta\n"; | |
| return; | |
| } | |
| while(tmp->next != head){ | |
| if(tmp->key == value){ | |
| cout << "Szukana wartosc to: " << tmp->key << endl; | |
| return; | |
| } else { | |
| x = 1; | |
| } | |
| tmp = tmp->next; | |
| } | |
| if(tmp->key == value) | |
| cout << "Szukana wartosc to: " << tmp->key << endl; | |
| if(x) cout << "Brak wskazanego elementu\n"; | |
| } | |
| void deleteElement(list_el *&head, int value) | |
| { | |
| if(!head){ | |
| cout << "Lista pusta\n"; | |
| return; | |
| } | |
| if(head->key == value){ | |
| list_el *last_el = head; | |
| while(last_el->next != head) | |
| last_el = last_el->next; | |
| // Wyszukiwanie ostatniego elementu | |
| list_el *tmp = head; | |
| head = head->next; | |
| last_el->next = head; | |
| delete tmp; | |
| return; | |
| } | |
| list_el *curr = head; | |
| while(curr->next != head){ | |
| if(curr->next->key == value){ | |
| list_el *tmp = curr->next; | |
| curr->next = curr->next->next; | |
| delete tmp; | |
| return; | |
| } | |
| curr = curr->next; | |
| } | |
| } | |
| int main() | |
| { | |
| srand((unsigned int)time(NULL)); | |
| list_el *lista = NULL; | |
| int i, k, value; | |
| while(k){ | |
| cout << "1. Dodaj losowe\n2. Dodaj element\n3. Znajdz elment\n4. Usun element\n5. Pokaz wszystko\n6. Wyjdz\n"; | |
| cin >> i; | |
| switch(i){ | |
| case 1: randomElements(lista); break; | |
| case 2: cout << "Podaj wartosc: "; cin >> value; add(lista, value); break; | |
| case 3: searchElement(lista); break; | |
| case 4: cout << "Podaj wartosc: "; cin >> value; deleteElement(lista, value); break; | |
| case 5: printAll(lista); break; | |
| case 6: k = 0; break; | |
| default: cout << "Zly wybor\n"; break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment