Lista jednokierunkowa

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
5.5 kB
1
Indexable
Never
#include <iostream>
using namespace std;

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;
}

int main(){
    single_list list;
    int choice;
    while (choice != 0){
        cout << "-----MENU-----\n"
            << "1. Add head\n"
            << "2. Add pos\n"
            << "3. Add tail\n"
            << "4. Delete head\n"
            << "5. Delete pos\n"
            << "6. Delete tail\n"
            << "7. Print position\n"
            << "8. Print list\n"
            << "9. Delete list\n"
            << "0. exit\n";
        cout << "Option -> "; cin >> choice;
        switch(choice){
            case 1:
                int val;
                cout << "val" << endl;
                cin >> val;
                add_head(list, val);
            break;
            
            case 2:
                int pos;
                cout << "val , pos" << endl;
                cin >> val >> pos;
                add_to_pos(list, val, pos);
            break;
            
            case 3:
                int value;
                cout << "val" << endl;
                cin >> value;
                add_tail(list, value);
            break;
            
            case 4:
                delete_head(list);
            break;
            
            case 5:
                cout << "pos" << endl;
                cin >> pos;
                delete_from_pos(list, pos);
            break;
            
            case 6:
                delete_tail(list);
            break;
            
            case 7:
                cout << "pos" << endl;
                cin >> pos;
                find_pos(list, pos);
            break;
            
            case 8:
                print_single_list(list);
            break;
            
            case 9:
                delete_list(list);
            break;
            
            case 0:
                cout << "EXIT" << endl;
            break;

            default:
                cout << "Wrong number, chose again" << endl;
            break;
        }
    }
    return 0;
}