Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:atp_lab_5_6_primer / priklad
.txt#include <iostream>
using namespace std;
int main()
{
int A[6]={5,7,9,10,12,13};
int g1 = 4, g2 = 15, g3 = 11;
struct c {
int n;
c *prev;
c *next;
};
c *begin = NULL;
c *last = NULL;
c *list = NULL;
for (int i=0; i<6; i++)
{ if (begin == NULL)
{
last = new c;
begin = last;
last->n = A[i];
last->prev = NULL;
last->next = NULL;
list = last;
}
else
{
last = new c;
list->next = last;
last->n = A[i];
last->prev = list;
last->next = NULL;
list = last;
}
}
// додаемо елемент в голову списку
list = new c;
list->n = g1;
list->prev = NULL;
list->next = begin;
begin->prev = list;
begin = list;
// додаемо елемент у хвіст
list = last;
last = new c;
list->next = last;
last->n = g2;
last->prev = list;
last->next = NULL;
// вставляємо нове значення після 5 елементу
int i=1;
list = begin;
c *nw = NULL;
while (list)
{
i++;
list = list->next;
if (i==5)
{
nw = new c;
nw->n = g3;
nw->prev = list;
nw->next = list->next;
list->next = nw;
list = list->next;
list = list->next;
list->prev = nw;
break;
}
}
// виводимо список з голови
list = begin;
cout <<"Spisok = \n";
while (list)
{
cout<<list->n<<"\n";
list = list->next;
}
// виводимо список у зворотньому порядку
list = last;
cout <<"Spisok = \n";
while (list)
{
cout<<list->n<<"\n";
list = list->prev;
}
delete list;
delete last;
delete begin;
delete nw;
return 0;
}
Spisok =
4
5
7
9
10
11
12
13
15
Spisok =
15
13
12
11
10
9
7
5
4
Соседние файлы в папке atp_lab_5_6_primer