Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
3.4 kB
1
Indexable
Never
struct element{
    int data;
    element *next = nullptr;
};
struct single_list{
    element *head = nullptr;
    element *tail = nullptr;
    int counter = 0;
};

// dodanie elementu na poczatek listy
void add_head(single_list &list, int value){
    element *el = new element;
    el->data = value;
    el->next = list.head;
    if( list.head == nullptr ){
        list.tail = el;
    }
    list.head = el;
    list.counter++;
}

// dodanie elementu na koniec listy
void add_tail(single_list &list, int value){
    element *el = new element;
    el->data = value;
    if(list.tail != nullptr){
        list.tail->next = el;
    }
    else{
        list.head = el;
    }
    list.tail = el;
    list.counter++;
}

// dodanie elementu na wskazana pozycje
void add_to_pos(single_list &list, int val, int pos){
    if( list.head == nullptr ){
        cout << "List empty\n" << endl;
        return;
    }
    if ( pos == 0){
        add_head(list, val);
    }
    else if( pos == list.counter ){
        add_tail(list, val);
    }
    else{
        element *iter = list.head;
        for( int i=0; i<pos-1; i++ ){
            iter=iter->next;
        }
    }
}


// usuniecie elementu z poczatku listy
void delete_head(single_list &list){
    element *temp = list.head;
    list.head = list.head->next;
    delete temp;
    if(list.head == nullptr){
        list.tail = nullptr;
    }
    list.counter--;
}

// usuniecie elementu z konca listy
void delete_tail(single_list &list){
    if( list.tail == nullptr ){
        cout << "List is empty" << endl;
        return;
    }
    element *iter = list.head;
    if( iter == list.tail ){
        delete list.tail;
        list.head = nullptr;
        list.tail = nullptr;
    }
    else{
        while( iter->next != list.tail ){
            iter = iter->next;
        }
        delete list.tail;
        list.tail = iter;
        list.tail->next = nullptr;
    }
    list.counter--;
}

// usuniecie elementu z wskazanej pozycji
void delete_from_pos(single_list &list, int pos){
    if( pos >= list.counter || pos < 0 ){
        cout << "Bledny numer pozycji" << endl;
        return;
    }
    if( pos == 0 ){
        delete_head(list);
    }
    else if( pos == list.counter-1 ){
        delete_tail(list);
    }
    else{
        element *iter = list.head;
        for(int i=0; i<pos-1; i++){
            iter = iter->next;
        }
        element *buf = iter->next;
        iter->next = buf->next;
        delete buf;
        list.counter--;
    }
}

// wyswietlenie wartosci elementu z danej pozycji
element *find_pos(single_list &list, int pos){
    if( pos >= list.counter || pos < 0 ){
        cout << "Wrong position" << endl;
    }
    element *iter = list.head;
    for(int i=0; i<pos; i++){
        iter = iter->next;
    }
    cout << iter->data << endl;
}

// wyswietlenie calej listy
void print_single_list(single_list list){
    element *counter = list.head;
    while( list.head != nullptr ){
        cout << counter->data << "\t";
        counter = counter->next;
    }
}

// usuniecie calej listy
void delete_list(single_list &list){
    while( list.head != nullptr ){
        element *buf = list.head;
        list.head = list.head->next;
        delete buf;
    }
    list.tail = nullptr;
    list.counter = 0;
}