Консультация № 200143
23.01.2021, 11:57
0.00 руб.
0 2 1
Здравствуйте! Прошу помощи в следующем вопросе:

Мне надо поменять функцию так чтобы в ней создавался новый список с копией элементов исходного списка, у которых совпадает издатель.

Пишу в Visual Studio

Приложение:
struct Journal
{
int index; // Номер в справочнике
string name; // Название книги
int counts; // Тираж
int period; // срок подписки
int time; // период выхода
int price; // цена
string publisher; // издатель
string priviliges; // льготы
};
struct List_elem //Список.
{
Journal jrnl;
List_elem* next;
};

List_elem* begin = nullptr;
List_elem* end = nullptr;

void showAllByPublisher(List_elem* begin)
{
string Letter;

cout << "Input Search Name:" << endl;
cin >> Letter;

List_elem* ptr = begin;

while (ptr != nullptr)
{
if (ptr->jrnl.publisher == Letter)
{
cout << " _____________________________" << endl;
cout << "|ID:" << ptr->jrnl.index << endl;
cout << "|Name:" << ptr->jrnl.name << endl;
cout << "|Counts:" << ptr->jrnl.counts << endl;
cout << "|Period:" << ptr->jrnl.period << endl;
cout << "|Time:" << ptr->jrnl.time << endl;
cout << "|Price:" << ptr->jrnl.price << "$" << endl;
cout << "|Publisher:" << ptr->jrnl.publisher << endl;
cout << "|Priviliges:" << ptr->jrnl.priviliges << endl;
cout << "|____________________________" << endl;
cout << "\n";
}

ptr = ptr->next;
}
}

Обсуждение

давно
Старший Модератор
312929
1973
30.01.2021, 05:48
общий
это ответ
Здравствуйте, KoreanLamer!

Вот примерный вариант нужной функции:
[code lang=C#]
void copyListByPublisher(List_elem* begin, List_elem* & copy)
{
string Letter;

cout << "Input Search Name:" << endl;
cin >> Letter;

List_elem *ptr = begin, *copy_ptr = copy;

while (ptr != nullptr)
{
if (ptr->jrnl.publisher == Letter)
{
if (copy_ptr==nullptr) copy_ptr = copy = new List_elem;
else copy_ptr = copy_ptr->next = new List_elem;
*copy_ptr = *ptr;
copy_ptr->next = nullptr;
}
ptr = ptr->next;
}
}[/code]
5
давно
Посетитель
403724
28
30.01.2021, 11:21
общий
Адресаты:
Большое спасибо
Форма ответа