Laboratorijas darbs 21. Vienvirziena saistītais saraksts

  1. Atvērt failu lab21.cpp (nepieciešamības gadījumā pievienot rindiņu systempause”);), nokompilēt un darbināt.

Programmas darbības rezultātā lietotājam tiek dota iespēja ievadīt veselus skaitļus, ievadīšanu beidzot ar 0, no ievadītajiem skaitļiem (neskaitot 0) tiek izveidots vienvirziena saistītais saraksts, pēc tam tā saturs tiek izdrukāts uz ekrāna:

1

3

5

7

6

0

1

3

5

7

6

Ievērojiet (funkcijā main), ka sarakstu reprezentē divas norādes – gan norāde uz pirmo, gan pēdējo elementu.

2.      Papildināt programmu šādi:

·        pievienot funkciju:

        elem *get_element_by_num (elem *first, int num)

kas pēc elementa vērtības atrod norādi uz pirmo elementu sarakstā ar šādu vērtību. Ja elementa ar šādu vērtību sarakstā nav, tiek atgriezta tukšā norāde NULL.

·        esošo funkciju main nomainīt uz:

int main ()

{

    elem *first=NULL, *last=NULL, *p;

    int i;

    cin >> i;

    while (i != 0)

    {

        add_element (first, last, i);

        cin >> i;

    };

    cout << "The list: " << endl;

    print_list (first);

    cout << "Input the value of an element: ";

    cin >> i;

    p = get_element_by_num (first, i);

    if (p == NULL) cout << "No such element: " << i << endl;

    else

    {

        cout << "The list, starting with the element: " << i << endl;

        print_list (p);

    };

    delete_list (first);

    return 0;

}

Programmas darbības rezultātā (tāpat kā iepriekš) lietotājam tiek dota iespēja ievadīt veselus skaitļus, ievadīšanu beidzot ar 0, pēc tam tā saturs tiek izdrukāts uz ekrāna. Pēc tam tiek paprasīta skaitļa vērtība un atrasts elements ar šo vērtību. Tad, lai nodemonstrētu, ka elements ir atrasts, tiek izdrukāts saraksts, sākot ar atrasto elementu.

1

3

5

7

6

0

The list:

1

3

5

7

6

Input the value of an element: 5

The list, starting with the element: 5

5

7

6

Ja tāds elements sarakstā nav sastopams, tad, tiek izdrukāts attiecīgs paziņojums:

1

3

5

7

6

0

The list:

1

3

5

7

6

Input the value of an element: 8

No such element: 8

Pareizais variants failā lab21a.cpp

 

3.      Papildināt programmu šādi:

·        pievienot vēl vienu funkciju:

        void insert_after (elem*&first, elem*&last, elem *e, int num)

kas pēc elementa, uz kuru norāda e sarakstā, kuru identificē first un last, ievieto jaunu elementu ar vērtību num. Ja norāde e satur NULL, tad jaunu elementu pievienot saraksta sākumā.

·        esošo funkciju main nomainīt uz:

int main ()

{

    elem *first=NULL, *last=NULL, *p;

    int i;

    cin >> i;

    while (i != 0)

    {

        add_element (first, last, i);

        cin >> i;

    };

    cout << "The list: " << endl;

    print_list (first);

    cout << "Input the value of an element: ";

    cin >> i;

    p = get_element_by_num (first, i);

    if (p == NULL) cout << "No such element: " << i << endl;

    else

    {

        cout << "The list, starting with the element: " << i << endl;

        print_list (p);

    };

    cout << "Input the value of an element to insert after the previously named one: ";

    cin >> i;

    insert_after (first, last, p, i);

    cout << "The new list: " << endl;

    print_list (first);

    delete_list (first);

    return 0;

}

Programma pēc otrās izmaiņas darbībā:

1

3

5

7

6

0

The list:

1

3

5

7

6

Input the value of an element: 5

The list, starting with the element: 5

5

7

6

Input the value of an element to insert after the previously named one: 8

The new list:

1

3

5

8

7

6

 

Pareizais variants failā lab21b.cpp