Created
September 9, 2013 17:06
-
-
Save pstachula-dev/6498572 to your computer and use it in GitHub Desktop.
SDiZO, lab2c (lista jednokierunkowa)
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> | |
| 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 = NULL; | |
| return 1; | |
| } | |
| if(value < head->key){ | |
| list_el *tmp = head; | |
| head = new list_el; | |
| head->key = value; | |
| head->next = tmp; | |
| return 1; | |
| } | |
| if(value == head->key) | |
| return 0; | |
| list_el *curr = head; | |
| while(curr->next && curr->next->key < value) | |
| curr = curr->next; | |
| if(curr->next == NULL){ | |
| curr->next = new list_el; | |
| curr->next->key = value; | |
| curr->next->next = NULL; | |
| return 1; | |
| } else { | |
| 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){ | |
| cout << curr->key << " "; | |
| curr = curr->next; | |
| } | |
| cout << endl; | |
| } | |
| int searchElement(list_el *head) | |
| { | |
| int value, x = 0; | |
| cout << "Podaj wartosc: "; | |
| cin >> value; | |
| while(head){ | |
| if(head->key == value){ | |
| cout << "Szukana wartosc to: " << head->key << endl; | |
| return head->key; | |
| } else { | |
| x = 1; | |
| } | |
| head = head->next; | |
| } | |
| 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 *tmp = head; | |
| head = head->next; | |
| delete tmp; | |
| return; | |
| } | |
| list_el *curr = head; | |
| while(curr){ | |
| 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