Skip to content

Instantly share code, notes, and snippets.

@pstachula-dev
Last active December 22, 2015 16:19
Show Gist options
  • Select an option

  • Save pstachula-dev/6498555 to your computer and use it in GitHub Desktop.

Select an option

Save pstachula-dev/6498555 to your computer and use it in GitHub Desktop.
SDiZO, lab2b (lista dwukierunkowa)
#include <iostream>
#include <time.h>
using namespace std;
struct list_el
{
int key;
list_el *next;
list_el *prev;
};
int add(list_el *&head, int value)
{
if(head == NULL){
head = new list_el;
head->key = value;
head->next = NULL;
head->prev = NULL;
return 1;
}
if(value < head->key){
list_el *tmp = head;
head = new list_el;
head->key = value;
head->next = tmp;
head->prev = NULL;
tmp->prev = head;
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;
curr->next->prev = curr;
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;
curr->next->prev = curr;
tmp->prev = curr->next;
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() % 200))
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;
}
void printAll2(list_el *head)
{
if(!head){
cout << "Lista pusta\n";
return;
}
list_el *curr = head;
while(curr->next)
curr = curr->next;
while(curr){
cout << curr->key << " ";
curr = curr->prev;
}
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;
head->prev = NULL;
delete tmp;
return;
}
list_el *curr = head;
while(curr->next){
if(curr->next->key == value){
list_el *tmp = curr->next;
curr->next = curr->next->next;
if(curr->next)
curr->next->prev = curr;
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. Pokaz wszystko od konca \n7. 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: printAll2(lista); break;
case 7: 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